xref: /PHP-8.2/ext/gd/tests/webp_basic.phpt (revision eb6c9eb9)
1--TEST--
2imagewebp() and imagecreatefromwebp() - basic test
3--EXTENSIONS--
4gd
5--SKIPIF--
6<?php
7if (!GD_BUNDLED && version_compare(GD_VERSION, '2.2.0', '<')) {
8    die("skip test requires GD 2.2.0 or higher");
9}
10if (!function_exists('imagewebp') || !function_exists('imagecreatefromwebp'))
11    die('skip WebP support not available');
12?>
13--FILE--
14<?php
15require_once __DIR__ . '/similarity.inc';
16
17$filename = __DIR__ . '/webp_basic.webp';
18
19$im1 = imagecreatetruecolor(75, 75);
20$white = imagecolorallocate($im1, 255, 255, 255);
21$red = imagecolorallocate($im1, 255, 0, 0);
22$green = imagecolorallocate($im1, 0, 255, 0);
23$blue = imagecolorallocate($im1, 0, 0, 255);
24imagefilledrectangle($im1, 0, 0, 74, 74, $white);
25imageline($im1, 3, 3, 71, 71, $red);
26imageellipse($im1, 18, 54, 36, 36, $green);
27imagerectangle($im1, 41, 3, 71, 33, $blue);
28imagewebp($im1, $filename);
29
30$im2 = imagecreatefromwebp($filename);
31imagewebp($im2, $filename);
32echo 'Is lossy conversion close enough? ';
33var_dump(calc_image_dissimilarity($im1, $im2) < 10e5);
34
35imagewebp($im1, $filename, IMG_WEBP_LOSSLESS);
36$im_lossless = imagecreatefromwebp($filename);
37echo 'Does lossless conversion work? ';
38var_dump(calc_image_dissimilarity($im1, $im_lossless) == 0);
39
40?>
41--CLEAN--
42<?php
43@unlink(__DIR__ . '/webp_basic.webp');
44?>
45--EXPECT--
46Is lossy conversion close enough? bool(true)
47Does lossless conversion work? bool(true)
48