Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/prd/conversion-engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Prefer the original file when output would be larger (unless forced), on unsuppo
- MIME set filterable via `cimo.imageConverter.mimeTypes`
- Prepare hook: `cimo.convertImage.prepare` (Premium HEIC → PNG)
- Optimize hook: `cimo.imageConverter.optimize` (Premium Smart Optimization); else `convert()`
- Animated GIF detection: do not corrupt animations
- Animated GIFs convert to animated WebP, and animated WebP uploads are optimized
- Progress: support determinate progress for UI when conversion is non-trivial

### Premium converters (via filters)
Expand Down
Binary file added e2e/fixtures/sample-animated-transparent.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added e2e/fixtures/sample-animated.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added e2e/fixtures/sample-animated.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added e2e/fixtures/sample-still.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions e2e/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ WP_PASSWORD=password
| General | Thumbnail generation on/off (intermediate sizes) |
| Quality | Lower WebP quality → smaller file than higher quality |
| Already-WebP | Re-upload stays `image/webp` |
| Animated WebP | GIF → animated WebP; still GIF → still WebP; animated WebP stays animated; transparent GIF keeps alpha |
| Media Library grid | Drop → WebP |
| Media modal | Select Files → WebP |
| PNG / multi-file | Convert to WebP |
Expand Down
6 changes: 6 additions & 0 deletions e2e/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export {
SAMPLE_OVERSIZED_JPG,
SAMPLE_SVG,
SAMPLE_HEIC,
SAMPLE_STILL_GIF,
SAMPLE_ANIMATED_GIF,
SAMPLE_ANIMATED_WEBP,
SAMPLE_ANIMATED_TRANSPARENT_GIF,
dropFile,
dropFiles,
waitForCimoReady,
Expand All @@ -25,6 +29,8 @@ export {
uploadSampleViaMediaNew,
waitForOptimizedUploadMeta,
fetchUrlByteLength,
fetchUrlBytes,
isAnimatedWebp,
openPopupAndGetByteLength,
getMediaFileByteLength,
expectCimoSidebarStats,
Expand Down
64 changes: 64 additions & 0 deletions e2e/test-utils/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ export const SAMPLE_SVG = path.resolve( __dirname, '../fixtures/sample.svg' )
/** HEIC fixture for premium HEIC → WebP upload tests. */
export const SAMPLE_HEIC = path.resolve( __dirname, '../fixtures/sample.heic' )

/** Single-frame GIF for still-image conversion. */
export const SAMPLE_STILL_GIF = path.resolve(
__dirname,
'../fixtures/sample-still.gif'
)

/** Two-frame photographic GIF for animated WebP conversion. */
export const SAMPLE_ANIMATED_GIF = path.resolve(
__dirname,
'../fixtures/sample-animated.gif'
)

/** Two-frame animated WebP for re-optimization. */
export const SAMPLE_ANIMATED_WEBP = path.resolve(
__dirname,
'../fixtures/sample-animated.webp'
)

/** Two-frame GIF with transparent margins for alpha-preserving conversion. */
export const SAMPLE_ANIMATED_TRANSPARENT_GIF = path.resolve(
__dirname,
'../fixtures/sample-animated-transparent.gif'
)

type DropFileSpec = {
path: string;
mimeType?: string;
Expand Down Expand Up @@ -410,6 +434,46 @@ export async function fetchUrlByteLength( page: Page, url: string ) {
}, url )
}

/**
* Same-origin file bytes for container checks (animated WebP ANIM/VP8X).
*/
export async function fetchUrlBytes( page: Page, url: string ) {
const data = await page.evaluate( async ( targetUrl ) => {
const response = await fetch( targetUrl, { credentials: 'same-origin' } )
if ( ! response.ok ) {
throw new Error( `Failed to fetch ${ targetUrl }: ${ response.status }` )
}
return Array.from( new Uint8Array( await response.arrayBuffer() ) )
}, url )

return Buffer.from( data )
}

/**
* True when a WebP RIFF container includes an ANIM chunk.
*/
export function isAnimatedWebp( data: Buffer ) {
if (
data.length < 12 ||
data.subarray( 0, 4 ).toString() !== 'RIFF' ||
data.subarray( 8, 12 ).toString() !== 'WEBP'
) {
return false
}

let offset = 12
while ( offset + 8 <= data.length ) {
const fourcc = data.subarray( offset, offset + 4 ).toString()
const chunkLength = data.readUInt32LE( offset + 4 )
if ( fourcc === 'ANIM' ) {
return true
}
offset += 8 + chunkLength + ( chunkLength % 2 )
}

return false
}

/**
* Click a control that opens a new tab/window and return that document's byte size.
*/
Expand Down
176 changes: 176 additions & 0 deletions e2e/tests/animated-webp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import fs from 'fs'

import {
test,
expect,
SAMPLE_STILL_GIF,
SAMPLE_ANIMATED_GIF,
SAMPLE_ANIMATED_WEBP,
SAMPLE_ANIMATED_TRANSPARENT_GIF,
saveCimoOptions,
uploadSampleViaMediaNew,
getMediaFileByteLength,
fetchUrlBytes,
isAnimatedWebp,
reloadCimoRuntime,
} from '../test-utils'

test.describe.configure( { timeout: 180_000 } )

async function sampleImageAlpha(
page: import( '@playwright/test' ).Page,
url: string,
x: number,
y: number
) {
return await page.evaluate( async ( { src, sampleX, sampleY } ) => {
const image = new Image()
image.crossOrigin = 'anonymous'
image.src = src
await image.decode()
const canvas = document.createElement( 'canvas' )
canvas.width = image.naturalWidth
canvas.height = image.naturalHeight
const context = canvas.getContext( '2d' )
if ( ! context ) {
throw new Error( 'Canvas 2D is unavailable' )
}
context.clearRect( 0, 0, canvas.width, canvas.height )
context.drawImage( image, 0, 0 )
return context.getImageData( sampleX, sampleY, 1, 1 ).data[ 3 ]
}, { src: url, sampleX: x, sampleY: y } )
}

test.describe( 'Animated WebP', () => {
test.beforeEach( async ( { requestUtils } ) => {
await requestUtils.deleteAllMedia()
await saveCimoOptions( requestUtils, {
webp_quality: 80,
max_image_dimension: 0,
disable_wp_scaling: 1,
disable_thumbnail_generation: 1,
smart_optimization: 0,
} )
} )

test.afterEach( async ( { requestUtils } ) => {
await requestUtils.deleteAllMedia()
await saveCimoOptions( requestUtils, {
webp_quality: 80,
max_image_dimension: 0,
disable_thumbnail_generation: 0,
} )
} )

test( 'converts an animated GIF to animated WebP', async ( {
page,
requestUtils,
} ) => {
await reloadCimoRuntime( page )

const media = await uploadSampleViaMediaNew(
page,
requestUtils,
SAMPLE_ANIMATED_GIF,
'image/gif'
)

expect( media.mime_type ).toBe( 'image/webp' )
expect( media.source_url ).toMatch( /\.webp(\?|$)/i )
expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( true )
expect( await getMediaFileByteLength( page, requestUtils, media.id ) ).toBeGreaterThan( 0 )
} )

test( 'converts a still GIF to still WebP', async ( {
page,
requestUtils,
} ) => {
await reloadCimoRuntime( page )

const media = await uploadSampleViaMediaNew(
page,
requestUtils,
SAMPLE_STILL_GIF,
'image/gif'
)

expect( media.mime_type ).toBe( 'image/webp' )
expect( media.source_url ).toMatch( /\.webp(\?|$)/i )
expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( false )
} )

test( 'optimizes an animated WebP and keeps it animated', async ( {
page,
requestUtils,
} ) => {
await reloadCimoRuntime( page )
const originalSize = fs.statSync( SAMPLE_ANIMATED_WEBP ).size

const media = await uploadSampleViaMediaNew(
page,
requestUtils,
SAMPLE_ANIMATED_WEBP,
'image/webp',
{
expectedMime: 'image/webp',
urlPattern: /\.webp(\?|$)/i,
}
)

expect( media.mime_type ).toBe( 'image/webp' )
expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( true )

const uploadedSize = await getMediaFileByteLength( page, requestUtils, media.id )
expect( uploadedSize ).toBeGreaterThan( 0 )
expect( uploadedSize ).toBeLessThanOrEqual( originalSize * 1.25 )
} )

test( 'converts a transparent animated GIF to animated WebP and keeps the background transparent', async ( {
page,
requestUtils,
} ) => {
await reloadCimoRuntime( page )

const media = await uploadSampleViaMediaNew(
page,
requestUtils,
SAMPLE_ANIMATED_TRANSPARENT_GIF,
'image/gif'
)

expect( media.mime_type ).toBe( 'image/webp' )
expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( true )

const width = media.media_details?.width ?? 0
const height = media.media_details?.height ?? 0
expect( width ).toBeGreaterThan( 1 )
expect( height ).toBeGreaterThan( 1 )
expect( await sampleImageAlpha( page, media.source_url, 0, 0 ) ).toBe( 0 )
expect( await sampleImageAlpha( page, media.source_url, width - 1, 0 ) ).toBe( 0 )
expect( await sampleImageAlpha( page, media.source_url, 80, 80 ) ).toBeGreaterThan( 200 )
} )

test( 'resizes an animated GIF without flattening it', async ( {
page,
requestUtils,
} ) => {
await saveCimoOptions( requestUtils, {
max_image_dimension: 80,
} )
await reloadCimoRuntime( page )

const media = await uploadSampleViaMediaNew(
page,
requestUtils,
SAMPLE_ANIMATED_GIF,
'image/gif'
)

expect( media.mime_type ).toBe( 'image/webp' )
expect( Math.max(
media.media_details?.width ?? 0,
media.media_details?.height ?? 0
) ).toBeLessThanOrEqual( 80 )
expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( true )
} )
} )
48 changes: 48 additions & 0 deletions e2e/tests/quality-and-webp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,41 @@ import {
expect,
SAMPLE_LARGE_JPG,
SAMPLE_LARGE_WEBP,
expectNewMediaMime,
getMaxMediaId,
saveCimoOptions,
uploadSampleViaMediaNew,
getMediaFileByteLength,
reloadCimoRuntime,
waitForCimoReady,
} from '../test-utils'

const SMALL_ANIMATED_GIF = Buffer.from(
'R0lGODlhAQABAIAAAP8AAAD/ACH/C05FVFNDQVBFMi4wAwEAAAAh+QQACgAAACwAAAAAAQABAAACAkQBACH5BAAKAAAALAAAAAABAAEAAAICTAEAOw==',
'base64'
)

async function uploadBufferViaMediaNew(
page: import( '@playwright/test' ).Page,
requestUtils: import( '@wordpress/e2e-test-utils-playwright' ).RequestUtils,
file: { name: string, mimeType: string, buffer: Buffer },
expectedMime = 'image/webp'
) {
await page.goto( '/wp-admin/media-new.php' )
await waitForCimoReady( page )
const afterId = await getMaxMediaId( requestUtils )
const fileInput = page.locator(
'.media-upload-form input[type="file"], #async-upload, input[name="async-upload"]'
).first()
await expect( fileInput ).toBeAttached( { timeout: 15_000 } )
await fileInput.setInputFiles( file )

return await expectNewMediaMime( requestUtils, afterId, expectedMime, {
timeout: 120_000,
urlPattern: new RegExp( `\\.${ expectedMime === 'image/webp' ? 'webp' : 'gif' }(\\?|$)`, 'i' ),
} )
}

test.describe.configure( { timeout: 180_000 } )

test.describe( 'WebP quality and already-WebP uploads', () => {
Expand Down Expand Up @@ -138,4 +167,23 @@ test.describe( 'WebP quality and already-WebP uploads', () => {
expect( media.media_details?.width ?? 0 ).toBeGreaterThan( 800 )
expect( await getMediaFileByteLength( page, requestUtils, media.id ) ).toBe( originalSize )
} )

test( 'keeps an animated GIF when its WebP conversion would be larger', async ( {
page,
requestUtils,
} ) => {
await saveCimoOptions( requestUtils, {
webp_quality: 100,
smart_optimization: 0,
disable_thumbnail_generation: 1,
} )
await reloadCimoRuntime( page )

const retainedGif = await uploadBufferViaMediaNew( page, requestUtils, {
name: 'animated.gif',
mimeType: 'image/gif',
buffer: SMALL_ANIMATED_GIF,
}, 'image/gif' )
expect( retainedGif.source_url ).toMatch( /\.gif(\?|$)/i )
} )
} )
Loading
Loading