11import fs from 'fs' ;
22import { rootCertificates } from 'tls' ;
33import path from 'path' ;
4- import { PHP , setPhpIniEntries } from '@php-wasm/universal' ;
4+ import {
5+ PHP ,
6+ SupportedPHPVersions ,
7+ setPhpIniEntries ,
8+ } from '@php-wasm/universal' ;
59import { loadNodeRuntime } from '../lib' ;
610import { jspi } from 'wasm-feature-detect' ;
711
812const runtimeMode = ( await jspi ( ) ) ? 'jspi' : 'asyncify' ;
913
1014describe ( `Imagick – ${ runtimeMode } ` , ( ) => {
11- const phpVersions = [ '7.2' ] ;
12- // 'PHP' in process.env ? [process.env['PHP']] : SupportedPHPVersions;
15+ const phpVersions =
16+ 'PHP' in process . env ? [ process . env [ 'PHP' ] ] : SupportedPHPVersions ;
1317
1418 describe . each ( phpVersions ) ( `PHP %s – ${ runtimeMode } ` , ( phpVersion ) => {
1519 let php : PHP ;
@@ -323,41 +327,6 @@ describe(`Imagick – ${runtimeMode}`, () => {
323327 expect ( result . text ) . toBe ( '3 frames' ) ;
324328 } ) ;
325329
326- test ( 'convert JPEG to WebP' , async ( ) => {
327- const result = await php . run ( {
328- code : `<?php
329- $imagick = new Imagick('/tmp/test-image.jpg');
330- $imagick->setImageFormat('webp');
331- $imagick->writeImage('/tmp/output.webp');
332-
333- // Verify we can read it back as WebP
334- $verify = new Imagick('/tmp/output.webp');
335- echo strtoupper($verify->getImageFormat());
336- ` ,
337- } ) ;
338- expect ( result . text ) . toBe ( 'WEBP' ) ;
339-
340- // Verify the WebP file exists
341- const webpFile = php . readFileAsBuffer ( '/tmp/output.webp' ) ;
342- expect ( webpFile . byteLength ) . toBeGreaterThan ( 0 ) ;
343- } ) ;
344-
345- test ( 'resize and save as WebP' , async ( ) => {
346- const result = await php . run ( {
347- code : `<?php
348- $imagick = new Imagick('/tmp/test-image.jpg');
349- $imagick->resizeImage(100, 100, Imagick::FILTER_LANCZOS, 1);
350- $imagick->setImageFormat('webp');
351- $imagick->writeImage('/tmp/resized.webp');
352-
353- // Verify
354- $verify = new Imagick('/tmp/resized.webp');
355- echo $verify->getImageWidth() . 'x' . $verify->getImageHeight() . ' ' . strtoupper($verify->getImageFormat());
356- ` ,
357- } ) ;
358- expect ( result . text ) . toBe ( '100x100 WEBP' ) ;
359- } ) ;
360-
361330 test ( 'convert JPEG to PNG' , async ( ) => {
362331 const result = await php . run ( {
363332 code : `<?php
@@ -441,6 +410,49 @@ describe(`Imagick – ${runtimeMode}`, () => {
441410 expect ( outputFile . byteLength ) . toBeGreaterThan ( 0 ) ;
442411 } ) ;
443412
413+ // Playground doesn't support WebP in PHP < 8.0
414+ const majorPHPVersion = phpVersion ?. substring ( 0 , 1 ) ;
415+ test . skipIf ( majorPHPVersion === '7' ) (
416+ 'convert JPEG to WebP' ,
417+ async ( ) => {
418+ const result = await php . run ( {
419+ code : `<?php
420+ $imagick = new Imagick('/tmp/test-image.jpg');
421+ $imagick->setImageFormat('webp');
422+ $imagick->writeImage('/tmp/output.webp');
423+
424+ // Verify we can read it back as WebP
425+ $verify = new Imagick('/tmp/output.webp');
426+ echo strtoupper($verify->getImageFormat());
427+ ` ,
428+ } ) ;
429+ expect ( result . text ) . toBe ( 'WEBP' ) ;
430+
431+ // Verify the WebP file exists
432+ const webpFile = php . readFileAsBuffer ( '/tmp/output.webp' ) ;
433+ expect ( webpFile . byteLength ) . toBeGreaterThan ( 0 ) ;
434+ }
435+ ) ;
436+
437+ test . skipIf ( majorPHPVersion === '7' ) (
438+ 'resize and save as WebP' ,
439+ async ( ) => {
440+ const result = await php . run ( {
441+ code : `<?php
442+ $imagick = new Imagick('/tmp/test-image.jpg');
443+ $imagick->resizeImage(100, 100, Imagick::FILTER_LANCZOS, 1);
444+ $imagick->setImageFormat('webp');
445+ $imagick->writeImage('/tmp/resized.webp');
446+
447+ // Verify
448+ $verify = new Imagick('/tmp/resized.webp');
449+ echo $verify->getImageWidth() . 'x' . $verify->getImageHeight() . ' ' . strtoupper($verify->getImageFormat());
450+ ` ,
451+ } ) ;
452+ expect ( result . text ) . toBe ( '100x100 WEBP' ) ;
453+ }
454+ ) ;
455+
444456 test ( 'get supported image formats' , async ( ) => {
445457 const result = await php . run ( {
446458 code : `<?php
@@ -458,8 +470,10 @@ describe(`Imagick – ${runtimeMode}`, () => {
458470 } ) ;
459471 expect ( result . text ) . toContain ( 'JPEG' ) ;
460472 expect ( result . text ) . toContain ( 'GIF' ) ;
461- expect ( result . text ) . toContain ( 'WEBP' ) ;
462473 expect ( result . text ) . toContain ( 'PNG' ) ;
474+ if ( majorPHPVersion !== '7' ) {
475+ expect ( result . text ) . toContain ( 'WEBP' ) ;
476+ }
463477 } ) ;
464478 } ) ;
465479} ) ;
0 commit comments