:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(workflows): resize oversized email images on upload (#129950)
Daniel Marchuk1fe5891b4761
diff --git a/frontend/src/scenes/hog-functions/email-templater/emailMediaLibrary.test.ts b/frontend/src/scenes/hog-functions/email-templater/emailMediaLibrary.test.tsindex b7a4b25e9b5a..a1464075de99 100644--- a/frontend/src/scenes/hog-functions/email-templater/emailMediaLibrary.test.ts+++ b/frontend/src/scenes/hog-functions/email-templater/emailMediaLibrary.test.ts@@ -88,6 +88,108 @@ describe('registerEmailMediaLibrary', () => {expect(done).toHaveBeenLastCalledWith({ error: expectedError })})+ describe('images over the upload limit', () => {+ const OVER_LIMIT_BYTES = 4 * 1024 * 1024 + 1+ const overLimitFile = (name: string, type: string): File =>+ new File([new Uint8Array(OVER_LIMIT_BYTES)], name, { type })+ const RESIZE_FAILED =+ "Couldn't resize this image automatically, and it's over the 4 MB limit. Use a smaller image and try again."++ function mockImageElement(size: { width: number; height: number } | 'unreadable'): void {+ URL.createObjectURL = jest.fn(() => 'blob:test')+ URL.revokeObjectURL = jest.fn()+ ;(globalThis as any).Image = class {+ onload = (): void => {}+ onerror = (): void => {}+ naturalWidth = size === 'unreadable' ? 0 : size.width+ naturalHeight = size === 'unreadable' ? 0 : size.height+ set src(_url: string) {+ if (size === 'unreadable') {+ this.onerror()+ } else {+ this.onload()+ }+ }+ }+ }++ function mockBrowserImageApis(alpha: number, encodedSizes: number[]): jest.Mock {+ mockImageElement({ width: 3000, height: 1500 })+ ;(globalThis as any).createImageBitmap = jest.fn(async () => ({+ width: 3000,+ height: 1500,+ close: jest.fn(),+ }))+ jest.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({+ drawImage: jest.fn(),+ getImageData: () => ({ data: new Uint8ClampedArray([0, 0, 0, alpha]) }),+ } as any)+ const toBlob = jest.fn()+ encodedSizes.forEach((size) =>+ toBlob.mockImplementationOnce((callback: BlobCallback, type: string) =>+ callback(new Blob([new Uint8Array(size)], { type }))+ )+ )+ HTMLCanvasElement.prototype.toBlob = toBlob+ return toBlob+ }++ function useUploadCapture(): { name: string; type: string; size: number }[] {+ const uploads: { name: string; type: string; size: number }[] = []+ useMocks({+ post: {+ '/api/projects/:team_id/uploaded_media/': async ({ request }) => {+ const image = (await request.formData()).get('image') as File+ uploads.push({ name: image.name, type: image.type, size: image.size })+ return [201, { id: 'media-1', image_location: 'http://localhost/m', name: image.name }]+ },+ },+ })+ return uploads+ }++ const originalImage = globalThis.Image++ afterEach(() => {+ jest.restoreAllMocks()+ globalThis.Image = originalImage+ delete (globalThis as any).createImageBitmap+ })++ it.each([+ ['keeps transparency as a PNG', 0, 'logo.png', 'image/png', 'logo.png', 'image/png'],+ ['uses a JPEG for an opaque PNG', 255, 'photo.png', 'image/png', 'photo.jpg', 'image/jpeg'],+ ])('%s', async (_name, alpha, inputName, inputType, outputName, outputType) => {+ const toBlob = mockBrowserImageApis(alpha, [OVER_LIMIT_BYTES, 1024])+ const uploads = useUploadCapture()+ const done = jest.fn()++ await registerOnFakeEditor()['image']({ attachments: [overLimitFile(inputName, inputType)] }, done)++ expect(toBlob).toHaveBeenCalledTimes(2)+ expect(uploads).toEqual([{ name: outputName, type: outputType, size: 1024 }])+ expect(done).toHaveBeenLastCalledWith(expect.objectContaining({ progress: 100 }))+ })++ it.each([+ ['a GIF, which would lose its animation', 'animated.gif', 'image/gif', { width: 800, height: 600 }],+ ['an image the browser cannot read', 'broken.png', 'image/png', 'unreadable' as const],+ ['an image too large to decode safely', 'huge.png', 'image/png', { width: 12000, height: 12000 }],+ ])('explains the failure without decoding or uploading %s', async (_name, fileName, type, size) => {+ mockImageElement(size)+ const decode = jest.fn()+ ;(globalThis as any).createImageBitmap = decode+ const uploads = useUploadCapture()+ const done = jest.fn()++ await registerOnFakeEditor()['image']({ attachments: [overLimitFile(fileName, type)] }, done)++ expect(decode).not.toHaveBeenCalled()+ expect(uploads).toEqual([])+ expect(done).toHaveBeenLastCalledWith({ error: RESIZE_FAILED })+ })+ })+it.each([['removed now', 204, true],['already removed', 404, true],diff --git a/frontend/src/scenes/hog-functions/email-templater/emailMediaLibrary.ts b/frontend/src/scenes/hog-functions/email-templater/emailMediaLibrary.tsindex db2529e7f1ad..1356e7e834ad 100644--- a/frontend/src/scenes/hog-functions/email-templater/emailMediaLibrary.ts+++ b/frontend/src/scenes/hog-functions/email-templater/emailMediaLibrary.ts@@ -9,6 +9,14 @@ import { UploadedMediaListPurpose } from '~/generated/core/api.schemas'import type { UploadedMediaApi } from '~/generated/core/api.schemas'const UPLOAD_FAILED_MESSAGE = "Couldn't upload this image. Try again."+const RESIZE_FAILED_MESSAGE =+ "Couldn't resize this image automatically, and it's over the 4 MB limit. Use a smaller image and try again."+const MAX_UPLOAD_BYTES = 4 * 1024 * 1024+// The last size always fits: 800x800 is under 4 MB even with no compression at all.+const RESIZE_LONGEST_SIDES_PX = [2000, 1600, 1200, 800]+const RESIZED_JPEG_QUALITY = 0.85+// A decoded image takes 4 bytes per pixel, so this keeps resizing under about 400 MB of memory.+const MAX_DECODED_PIXELS = 100_000_000// Unlayer passes `page` but no page size, so the provider decides how many images a page holds.const UPLOADS_PAGE_SIZE = 20@@ -66,6 +74,80 @@ function uploadErrorMessage(error: unknown): string {return error instanceof ApiError && error.detail ? error.detail : UPLOAD_FAILED_MESSAGE}+function hasTransparency(context: CanvasRenderingContext2D, width: number, height: number): boolean {+ try {+ const { data } = context.getImageData(0, 0, width, height)+ for (let alpha = 3; alpha < data.length; alpha += 4) {+ if (data[alpha] !== 255) {+ return true+ }+ }+ return false+ } catch {+ // Privacy-focused browsers can block reading pixels. PNG is safe for either case.+ return true+ }+}++// An image element reports its size once loaded, before the browser decodes its pixels.+async function readPixelCount(image: File): Promise<number> {+ const url = URL.createObjectURL(image)+ try {+ return await new Promise<number>((resolve, reject) => {+ const element = new Image()+ element.onload = () => resolve(element.naturalWidth * element.naturalHeight)+ element.onerror = () => reject(new Error('Image could not be read'))+ element.src = url+ })+ } finally {+ URL.revokeObjectURL(url)+ }+}++async function assertCanBeResized(image: File): Promise<void> {+ // Drawing a GIF to a canvas keeps only its first frame.+ if (image.type === 'image/gif') {+ throw new Error('GIFs cannot be resized')+ }+ if ((await readPixelCount(image)) > MAX_DECODED_PIXELS) {+ throw new Error('Image has too many pixels to resize safely')+ }+}++async function shrinkToUploadLimit(image: File): Promise<File> {+ await assertCanBeResized(image)+ const bitmap = await createImageBitmap(image)+ try {+ const canvas = document.createElement('canvas')+ const context = canvas.getContext('2d')+ if (!context) {+ throw new Error('Canvas is not available')+ }+ const originalLongestSide = Math.max(bitmap.width, bitmap.height)+ let outputType: string | undefined+ for (const longestSide of RESIZE_LONGEST_SIDES_PX) {+ const scale = Math.min(1, longestSide / originalLongestSide)+ canvas.width = Math.round(bitmap.width * scale)+ canvas.height = Math.round(bitmap.height * scale)+ context.drawImage(bitmap, 0, 0, canvas.width, canvas.height)+ // JPEG would turn transparent areas black, so it's only for images without any.+ outputType ??=+ image.type !== 'image/jpeg' && hasTransparency(context, canvas.width, canvas.height)+ ? 'image/png'+ : 'image/jpeg'+ const type = outputType+ const blob = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, type, RESIZED_JPEG_QUALITY))+ if (blob && blob.size <= MAX_UPLOAD_BYTES) {+ const name = image.name.replace(/(\.[^.]+)?$/, type === 'image/png' ? '.png' : '.jpg')+ return new File([blob], name, { type })+ }+ }+ } finally {+ bitmap.close()+ }+ throw new Error('Image is still over the upload limit after resizing')+}+/*** Backs Unlayer's Uploads tab with the team's email media library, so uploads land in our storage* and every template on the team can reuse them.@@ -111,10 +193,29 @@ export function registerEmailMediaLibrary(editor: Editor, projectId: string): voreturn}done({ progress: 10 })+ let imageToUpload = image+ if (image.size > MAX_UPLOAD_BYTES) {+ try {+ imageToUpload = await shrinkToUploadLimit(image)+ } catch {+ posthog.capture('email media library image uploaded', {+ success: false,+ error_code: 'resize_failed',+ })+ done({ error: RESIZE_FAILED_MESSAGE })+ return+ }+ }try {- const media = await uploadedMediaCreate(projectId, { image, purpose: UploadedMediaListPurpose.Email })+ const media = await uploadedMediaCreate(projectId, {+ image: imageToUpload,+ purpose: UploadedMediaListPurpose.Email,+ })// pinned: analytics event name and properties, renaming breaks dashboards- posthog.capture('email media library image uploaded', { success: true })+ posthog.capture('email media library image uploaded', {+ success: true,+ resized: imageToUpload !== image,+ })done({progress: 100,url: media.image_location,