Skip to content

Commit 7632c3d

Browse files
shivamhwpPR Batch Testerclaude
authored
fix(mobile): photo-library PNGs no longer fail after resizing (pingdotgg#26522)
Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent fe219c4 commit 7632c3d

2 files changed

Lines changed: 98 additions & 21 deletions

File tree

‎apps/mobile/src/lib/composerFiles.test.ts‎

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,28 +124,35 @@ describe("composer file attachments", () => {
124124
width: 4032,
125125
height: 3024,
126126
};
127+
type Size = { width: number; height: number };
127128
const native = {
128-
size: { width: 1, height: 1 },
129-
resizes: [] as Array<{ width?: number | null; height?: number | null }>,
129+
size: { width: 1, height: 1 } as Size,
130+
resizes: [] as Size[],
130131
saved: rendered as { uri: string; base64?: string },
132+
encode: null as ((size: Size, format: string) => { uri: string; base64?: string }) | null,
131133
};
132134

133135
beforeEach(() => {
134136
native.size = { width: 1, height: 1 };
135137
native.resizes = [];
136138
native.saved = rendered;
139+
native.encode = null;
137140
mocks.manipulate.mockReset();
138141
mocks.release.mockReset();
139-
mocks.manipulate.mockImplementation(() => {
142+
mocks.manipulate.mockImplementation((source: string | Size) => {
143+
let size = typeof source === "string" ? native.size : source;
140144
const context = {
141-
resize(size: { width?: number | null; height?: number | null }) {
142-
native.resizes.push(size);
145+
resize(next: Size) {
146+
native.resizes.push(next);
147+
size = next;
143148
return context;
144149
},
145150
renderAsync: async () => ({
146-
...native.size,
151+
width: size.width,
152+
height: size.height,
147153
release: mocks.release,
148-
saveAsync: async () => native.saved,
154+
saveAsync: async (options: { format: string }) =>
155+
native.encode?.(size, options.format) ?? native.saved,
149156
}),
150157
};
151158
return context;
@@ -210,6 +217,45 @@ describe("composer file attachments", () => {
210217
]);
211218
});
212219

220+
it("shrinks a fitted PNG further when re-encoding it exceeds the image limit", async () => {
221+
const header = Buffer.alloc(24);
222+
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(header);
223+
header.write("IHDR", 12);
224+
header.writeUInt32BE(1668, 16);
225+
header.writeUInt32BE(2388, 20);
226+
native.size = { width: 1668, height: 2388 };
227+
// Native PNG encoding of the 2000px resample is larger than the 7 MB original.
228+
native.encode = (size, format) => ({
229+
uri: `file:///cache/fitted-${size.width}x${size.height}.${format}`,
230+
base64: Buffer.alloc(size.width * size.height * 4).toString("base64"),
231+
});
232+
mocks.size.mockReturnValue(7 * 1024 * 1024);
233+
mocks.readBase64.mockResolvedValue(header.toString("base64"));
234+
mocks.pickMedia.mockResolvedValue({
235+
canceled: false,
236+
assets: [{ ...photo, fileName: "IMG_9280.png", mimeType: "image/png" }],
237+
});
238+
239+
const result = await pickComposerImages({ existingCount: 0 });
240+
241+
expect(result.error).toBeNull();
242+
const [image] = result.images;
243+
const last = native.resizes.at(-1)!;
244+
expect(native.resizes[0]).toEqual({ width: 1397, height: 2000 });
245+
expect(Math.max(last.width, last.height)).toBeLessThan(2000);
246+
expect(image).toEqual(
247+
expect.objectContaining({
248+
name: "IMG_9280.png",
249+
mimeType: "image/png",
250+
sizeBytes: last.width * last.height * 4,
251+
previewUri: `file:///cache/fitted-${last.width}x${last.height}.png`,
252+
}),
253+
);
254+
expect(image!.sizeBytes).toBeLessThanOrEqual(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES);
255+
expect(mocks.delete).toHaveBeenCalledWith("file:///cache/fitted-1397x2000.png");
256+
expect(mocks.delete).not.toHaveBeenCalledWith(image!.previewUri);
257+
});
258+
213259
it("measures the picker file instead of trusting the reported size", async () => {
214260
mocks.pickMedia.mockResolvedValue({
215261
canceled: false,

‎apps/mobile/src/lib/composerImageFit.ts‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts";
12
import {
23
downscalableImageMimeType,
34
fitImageAttachmentSize,
@@ -6,8 +7,24 @@ import {
67
IMAGE_DIMENSIONS_HEADER_BYTES,
78
readImageDimensions,
89
} from "@t3tools/shared/imageDimensions";
10+
import { estimateBase64ByteSize } from "./base64";
911

1012
const PHOTO_JPEG_QUALITY = 0.85;
13+
const MAX_BYTE_LIMIT_SHRINKS = 3;
14+
15+
function shrinkToByteLimit(
16+
size: { readonly width: number; readonly height: number },
17+
bytes: number,
18+
): { width: number; height: number } | null {
19+
if (bytes <= PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) return null;
20+
// Encoded size tracks pixel count, so scale each edge by the square root with some headroom.
21+
const scale = Math.sqrt(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / bytes) * 0.9;
22+
const next = {
23+
width: Math.max(1, Math.floor(size.width * scale)),
24+
height: Math.max(1, Math.floor(size.height * scale)),
25+
};
26+
return next.width < size.width || next.height < size.height ? next : null;
27+
}
1128

1229
export async function renderPhoto(
1330
uri: string,
@@ -16,21 +33,35 @@ export async function renderPhoto(
1633
const { ImageManipulator, SaveFormat } = await import("expo-image-manipulator");
1734
let image = await ImageManipulator.manipulate(uri).renderAsync();
1835
try {
19-
const fittedSize = fitImageAttachmentSize(image);
20-
if (fittedSize !== null) {
21-
const resized = await ImageManipulator.manipulate(image).resize(fittedSize).renderAsync();
22-
image.release();
23-
image = resized;
24-
}
25-
const saved = await image.saveAsync({
26-
format: format === "png" ? SaveFormat.PNG : SaveFormat.JPEG,
27-
compress: PHOTO_JPEG_QUALITY,
28-
base64: true,
29-
});
30-
if (!saved.base64) {
31-
throw new Error("The rendered photo has no bytes.");
36+
let size = fitImageAttachmentSize(image);
37+
for (let shrinks = 0; ; shrinks += 1) {
38+
if (size !== null) {
39+
const resized = await ImageManipulator.manipulate(image).resize(size).renderAsync();
40+
image.release();
41+
image = resized;
42+
}
43+
const saved = await image.saveAsync({
44+
format: format === "png" ? SaveFormat.PNG : SaveFormat.JPEG,
45+
compress: PHOTO_JPEG_QUALITY,
46+
base64: true,
47+
});
48+
if (!saved.base64) {
49+
throw new Error("The rendered photo has no bytes.");
50+
}
51+
// PNG ignores compress, so a resampled PNG can encode larger than its original.
52+
size =
53+
shrinks < MAX_BYTE_LIMIT_SHRINKS
54+
? shrinkToByteLimit(image, estimateBase64ByteSize(saved.base64))
55+
: null;
56+
if (size === null) return { base64: saved.base64, uri: saved.uri };
57+
try {
58+
const { File } = await import("expo-file-system");
59+
const discarded = new File(saved.uri);
60+
if (discarded.exists) discarded.delete();
61+
} catch (error) {
62+
console.warn("[composer-images] could not remove discarded render", error);
63+
}
3264
}
33-
return { base64: saved.base64, uri: saved.uri };
3465
} finally {
3566
image.release();
3667
}

0 commit comments

Comments
 (0)