The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 13:15 UTC
fix(mobile): photo-library PNGs no longer fail after resizing (#26522)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
shivam committed
Oct 29, 2026 at 10:14 UTC
7632c3dc7039953cc50282e1bc339e1bf11f2ffc
2 files changed
+98
-21
apps/mobile/src/lib/composerFiles.test.ts
+53
-7
@@ -124,28 +124,35 @@ describe("composer file attachments", () => {
124
width: 4032,
125
height: 3024,
126
};
127
+ type Size = { width: number; height: number };
128
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[],
131
saved: rendered as { uri: string; base64?: string },
132
+ encode: null as ((size: Size, format: string) => { uri: string; base64?: string }) | null,
133
};
134
135
beforeEach(() => {
136
native.size = { width: 1, height: 1 };
137
native.resizes = [];
138
native.saved = rendered;
139
+ native.encode = null;
140
mocks.manipulate.mockReset();
141
mocks.release.mockReset();
139
- mocks.manipulate.mockImplementation(() => {
142
+ mocks.manipulate.mockImplementation((source: string | Size) => {
143
+ let size = typeof source === "string" ? native.size : source;
144
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;
148
return context;
149
},
150
renderAsync: async () => ({
146
- ...native.size,
151
+ width: size.width,
152
+ height: size.height,
153
release: mocks.release,
148
- saveAsync: async () => native.saved,
154
+ saveAsync: async (options: { format: string }) =>
155
+ native.encode?.(size, options.format) ?? native.saved,
156
}),
157
};
158
return context;
@@ -210,6 +217,45 @@ describe("composer file attachments", () => {
217
]);
218
});
219
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
+
259
it("measures the picker file instead of trusting the reported size", async () => {
260
mocks.pickMedia.mockResolvedValue({
261
canceled: false,
apps/mobile/src/lib/composerImageFit.ts
+45
-14
@@ -1,3 +1,4 @@
1
+import { PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts";
2
import {
3
downscalableImageMimeType,
4
fitImageAttachmentSize,
@@ -6,8 +7,24 @@ import {
7
IMAGE_DIMENSIONS_HEADER_BYTES,
8
readImageDimensions,
9
} from "@t3tools/shared/imageDimensions";
10
+import { estimateBase64ByteSize } from "./base64";
11
12
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
+}
28
29
export async function renderPhoto(
30
uri: string,
@@ -16,21 +33,35 @@ export async function renderPhoto(
33
const { ImageManipulator, SaveFormat } = await import("expo-image-manipulator");
34
let image = await ImageManipulator.manipulate(uri).renderAsync();
35
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
+ }
64
}
33
- return { base64: saved.base64, uri: saved.uri };
65
} finally {
66
image.release();
67
}