com php-src: Added basic test for imagewebp() and imagecreatefromwebp(): ext/gd/tests/similarity.inc ext/gd/tests/webp_basic.phpt

From: Date: Sun, 19 Jul 2015 21:05:07 +0000
Subject: com php-src: Added basic test for imagewebp() and imagecreatefromwebp(): ext/gd/tests/similarity.inc ext/gd/tests/webp_basic.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-88049@lists.php.net to get a copy of this message
Commit: 90de2aeaac3de8f0744853ba197457c2241a4bdf Author: Christoph M. Becker <cmb@php.net> Sun, 19 Jul 2015 23:03:02 +0200 Parents: 4b2d025937769d383e6a842a90658c58f10d5794 Branches: PHP-5.6 master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=90de2aeaac3de8f0744853ba197457c2241a4bdf Log: Added basic test for imagewebp() and imagecreatefromwebp() Changed paths: A ext/gd/tests/similarity.inc A ext/gd/tests/webp_basic.phpt Diff: diff --git a/ext/gd/tests/similarity.inc b/ext/gd/tests/similarity.inc new file mode 100644 index 0000000..cb0dba7 --- /dev/null +++ b/ext/gd/tests/similarity.inc @@ -0,0 +1,64 @@ +<?php + +/** + * A very simple algorithm for finding the dissimilarity between images, + * mainly useful for checking lossy compression. + */ + +/** + * Gets the individual components of an RGB value. + * + * @param int $color + * @param int $red + * @param int $green + * @param int $blue + * + * @return void + */ +function get_rgb($color, &$red, &$green, &$blue) +{ + // assumes $color is an RGB value + $red = ($color >> 16) & 0xFF; + $green = ($color >> 8) & 0xFF; + $blue = $color & 0xFF; +} + +/** + * Calculates the euclidean distance of two RGB values. + * + * @param int $color1 + * @param int $color2 + * + * @return int + */ +function calc_pixel_distance($color1, $color2) +{ + get_rgb($color1, $red1, $green1, $blue1); + get_rgb($color2, $red2, $green2, $blue2); + return sqrt( + pow($red1 - $red2, 2) + pow($green1 - $green2, 2) + pow($blue1 - $blue2, 2) + ); +} + +/** + * Calculates dissimilarity of two images. + * + * @param resource $image1 + * @param resource $image2 + * + * @return int The dissimilarity. 0 means the images are identical. The higher + * the value, the more dissimilar are the images. + */ +function calc_image_dissimilarity($image1, $image2) +{ + // assumes image1 and image2 have same width and height + $dissimilarity = 0; + for ($i = 0, $n = imagesx($image1); $i < $n; $i++) { + for ($j = 0, $m = imagesy($image1); $j < $m; $j++) { + $color1 = imagecolorat($image1, $i, $j); + $color2 = imagecolorat($image2, $i, $j); + $dissimilarity += calc_pixel_distance($color1, $color2); + } + } + return $dissimilarity; +} diff --git a/ext/gd/tests/webp_basic.phpt b/ext/gd/tests/webp_basic.phpt new file mode 100644 index 0000000..75933a9 --- /dev/null +++ b/ext/gd/tests/webp_basic.phpt @@ -0,0 +1,35 @@ +--TEST-- +imagewebp() and imagecreatefromwebp() - basic test +--SKIPIF-- +<?php +if (!extension_loaded('gd')) die('skip gd extension not available'); +if (!function_exists('imagewebp') || !function_exists('imagecreatefromwebp')) + die('skip WebP support not available'); +?> +--FILE-- +<?php +require_once __DIR__ . '/similarity.inc'; + +$filename = __DIR__ . '/webp_basic.webp'; + +$im1 = imagecreatetruecolor(75, 75); +$white = imagecolorallocate($im1, 255, 255, 255); +$red = imagecolorallocate($im1, 255, 0, 0); +$green = imagecolorallocate($im1, 0, 255, 0); +$blue = imagecolorallocate($im1, 0, 0, 255); +imagefilledrectangle($im1, 0, 0, 74, 74, $white); +imageline($im1, 3, 3, 71, 71, $red); +imageellipse($im1, 18, 54, 36, 36, $green); +imagerectangle($im1, 41, 3, 71, 33, $blue); +imagewebp($im1, $filename); + +$im2 = imagecreatefromwebp($filename); +imagewebp($im2, $filename); +var_dump(calc_image_dissimilarity($im1, $im2) < 10e5); +?> +--CLEAN-- +<?php +@unlink(__DIR__ . '/webp_basic.webp'); +?> +--EXPECT-- +bool(true)

« previous php.cvs (#88049) next »