Downscale pasted PNG images based on metadata (#29123)
Some images like MacOS screenshots contain [pHYs](http://www.libpng.org/pub/png/book/chapter11.html#png.ch11.div.8) data which we can use to downscale uploaded images so they render in the same dppx ratio in which they were taken. Before: <img width="584" alt="image" src="50979e3a
-5d5a-40dc-a0a4-36eb6e28f14a"> After: <img width="329" alt="image" src="0690902a
-f2fe-4c6b-97b3-6fdd67c21bad"> (cherry picked from commit 5e72526da4e915791f03af056890e16821bde052)
This commit is contained in:
parent
bb911b2d5f
commit
b3f2447bc4
3 changed files with 93 additions and 3 deletions
47
web_src/js/utils/image.js
Normal file
47
web_src/js/utils/image.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
export async function pngChunks(blob) {
|
||||
const uint8arr = new Uint8Array(await blob.arrayBuffer());
|
||||
const chunks = [];
|
||||
if (uint8arr.length < 12) return chunks;
|
||||
const view = new DataView(uint8arr.buffer);
|
||||
if (view.getBigUint64(0) !== 9894494448401390090n) return chunks;
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
let index = 8;
|
||||
while (index < uint8arr.length) {
|
||||
const len = view.getUint32(index);
|
||||
chunks.push({
|
||||
name: decoder.decode(uint8arr.slice(index + 4, index + 8)),
|
||||
data: uint8arr.slice(index + 8, index + 8 + len),
|
||||
});
|
||||
index += len + 12;
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
// decode a image and try to obtain width and dppx. If will never throw but instead
|
||||
// return default values.
|
||||
export async function imageInfo(blob) {
|
||||
let width = 0; // 0 means no width could be determined
|
||||
let dppx = 1; // 1 dot per pixel for non-HiDPI screens
|
||||
|
||||
if (blob.type === 'image/png') { // only png is supported currently
|
||||
try {
|
||||
for (const {name, data} of await pngChunks(blob)) {
|
||||
const view = new DataView(data.buffer);
|
||||
if (name === 'IHDR' && data?.length) {
|
||||
// extract width from mandatory IHDR chunk
|
||||
width = view.getUint32(0);
|
||||
} else if (name === 'pHYs' && data?.length) {
|
||||
// extract dppx from optional pHYs chunk, assuming pixels are square
|
||||
const unit = view.getUint8(8);
|
||||
if (unit === 1) {
|
||||
dppx = Math.round(view.getUint32(0) / 39.3701) / 72; // meter to inch to dppx
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
return {width, dppx};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue