Skip to content

Commit 440d715

Browse files
committed
Merge branch 'PHP-5.6'
* PHP-5.6: Added basic test for imagewebp() and imagecreatefromwebp()
2 parents 3002ba4 + 90de2ae commit 440d715

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

ext/gd/tests/similarity.inc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/**
4+
* A very simple algorithm for finding the dissimilarity between images,
5+
* mainly useful for checking lossy compression.
6+
*/
7+
8+
/**
9+
* Gets the individual components of an RGB value.
10+
*
11+
* @param int $color
12+
* @param int $red
13+
* @param int $green
14+
* @param int $blue
15+
*
16+
* @return void
17+
*/
18+
function get_rgb($color, &$red, &$green, &$blue)
19+
{
20+
// assumes $color is an RGB value
21+
$red = ($color >> 16) & 0xFF;
22+
$green = ($color >> 8) & 0xFF;
23+
$blue = $color & 0xFF;
24+
}
25+
26+
/**
27+
* Calculates the euclidean distance of two RGB values.
28+
*
29+
* @param int $color1
30+
* @param int $color2
31+
*
32+
* @return int
33+
*/
34+
function calc_pixel_distance($color1, $color2)
35+
{
36+
get_rgb($color1, $red1, $green1, $blue1);
37+
get_rgb($color2, $red2, $green2, $blue2);
38+
return sqrt(
39+
pow($red1 - $red2, 2) + pow($green1 - $green2, 2) + pow($blue1 - $blue2, 2)
40+
);
41+
}
42+
43+
/**
44+
* Calculates dissimilarity of two images.
45+
*
46+
* @param resource $image1
47+
* @param resource $image2
48+
*
49+
* @return int The dissimilarity. 0 means the images are identical. The higher
50+
* the value, the more dissimilar are the images.
51+
*/
52+
function calc_image_dissimilarity($image1, $image2)
53+
{
54+
// assumes image1 and image2 have same width and height
55+
$dissimilarity = 0;
56+
for ($i = 0, $n = imagesx($image1); $i < $n; $i++) {
57+
for ($j = 0, $m = imagesy($image1); $j < $m; $j++) {
58+
$color1 = imagecolorat($image1, $i, $j);
59+
$color2 = imagecolorat($image2, $i, $j);
60+
$dissimilarity += calc_pixel_distance($color1, $color2);
61+
}
62+
}
63+
return $dissimilarity;
64+
}

ext/gd/tests/webp_basic.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
imagewebp() and imagecreatefromwebp() - basic test
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
if (!function_exists('imagewebp') || !function_exists('imagecreatefromwebp'))
7+
die('skip WebP support not available');
8+
?>
9+
--FILE--
10+
<?php
11+
require_once __DIR__ . '/similarity.inc';
12+
13+
$filename = __DIR__ . '/webp_basic.webp';
14+
15+
$im1 = imagecreatetruecolor(75, 75);
16+
$white = imagecolorallocate($im1, 255, 255, 255);
17+
$red = imagecolorallocate($im1, 255, 0, 0);
18+
$green = imagecolorallocate($im1, 0, 255, 0);
19+
$blue = imagecolorallocate($im1, 0, 0, 255);
20+
imagefilledrectangle($im1, 0, 0, 74, 74, $white);
21+
imageline($im1, 3, 3, 71, 71, $red);
22+
imageellipse($im1, 18, 54, 36, 36, $green);
23+
imagerectangle($im1, 41, 3, 71, 33, $blue);
24+
imagewebp($im1, $filename);
25+
26+
$im2 = imagecreatefromwebp($filename);
27+
imagewebp($im2, $filename);
28+
var_dump(calc_image_dissimilarity($im1, $im2) < 10e5);
29+
?>
30+
--CLEAN--
31+
<?php
32+
@unlink(__DIR__ . '/webp_basic.webp');
33+
?>
34+
--EXPECT--
35+
bool(true)

0 commit comments

Comments
 (0)