Skip to content

Add imagematch() function #14914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3254,6 +3254,63 @@ PHP_FUNCTION(imagecopyresized)
}
/* }}} */

#define GET_TRUECOLOR_PIXEL(px, im, x, y) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use gdImageRed() and friends instead. Shouldn't make much of a performance difference.

php-src/ext/gd/libgd/gd.h

Lines 771 to 778 in 6fb79e2

#define gdImageRed(im, c) ((im)->trueColor ? gdTrueColorGetRed(c) : \
(im)->red[(c)])
#define gdImageGreen(im, c) ((im)->trueColor ? gdTrueColorGetGreen(c) : \
(im)->green[(c)])
#define gdImageBlue(im, c) ((im)->trueColor ? gdTrueColorGetBlue(c) : \
(im)->blue[(c)])
#define gdImageAlpha(im, c) ((im)->trueColor ? gdTrueColorGetAlpha(c) : \
(im)->alpha[(c)])

if (im->trueColor) { \
px = gdImageTrueColorPixel(im, x, y); \
} \
else { \
int c = gdImagePalettePixel(im, x, y); \
px = gdTrueColor( \
gdImageRed(im, c), \
gdImageGreen(im, c), \
gdImageBlue(im, c)); \
}

/* {{{ Check whether two images are pixel by pixel identical */
PHP_FUNCTION(imagematch)
{
zval *IM1, *IM2;
gdImagePtr im1, im2;
int x, y, sx, sy;
bool result = true;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_OBJECT_OF_CLASS(IM1, gd_image_ce)
Z_PARAM_OBJECT_OF_CLASS(IM2, gd_image_ce)
ZEND_PARSE_PARAMETERS_END();

im1 = php_gd_libgdimageptr_from_zval_p(IM1);
im2 = php_gd_libgdimageptr_from_zval_p(IM2);

sx = im1->sx;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to use gdImageSX() and gdImageSY().

php-src/ext/gd/libgd/gd.h

Lines 768 to 769 in 6fb79e2

#define gdImageSX(im) ((im)->sx)
#define gdImageSY(im) ((im)->sy)

if (im1->sx != im2->sx) {
RETURN_FALSE;
}

sy = im1->sy;
if (im1->sy != im2->sy) {
RETURN_FALSE;
}

for (y = 0; y < sy; y++) {
for (x = 0; x < sx; x++) {
int p1, p2;

GET_TRUECOLOR_PIXEL(p1, im1, x, y);
GET_TRUECOLOR_PIXEL(p2, im2, x, y);

if (p1 != p2) {
result = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're only interested in a bool return value, we could return here. It might be useful to have an int return, though, which reports how many pixels are different.

}
}
}

RETURN_BOOL(result);
}
/* }}} */

#undef GET_TRUECOLOR_PIXEL

/* {{{ Get image width */
PHP_FUNCTION(imagesx)
{
Expand Down
2 changes: 2 additions & 0 deletions ext/gd/gd.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ function imagecopymergegray(GdImage $dst_image, GdImage $src_image, int $dst_x,

function imagecopyresized(GdImage $dst_image, GdImage $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $dst_width, int $dst_height, int $src_width, int $src_height): bool {}

function imagematch(GdImage $image1, GdImage $image2): bool {}

function imagesx(GdImage $image): int {}

function imagesy(GdImage $image): int {}
Expand Down
6 changes: 5 additions & 1 deletion ext/gd/gd_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions ext/gd/tests/imagematch_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--TEST--
imagematch() function test
--EXTENSIONS--
gd
--SKIPIF--
<?php
if (!(imagetypes() & IMG_PNG)) die("skip No PNG support");
?>
Comment on lines +5 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no PNG involved, this section can be dropped.

--FILE--
<?php

/* sample palette image, red/green */
$palette_1 = imagecreate(2, 1);
imagesetpixel($palette_1, 0, 0, imagecolorallocate($palette_1, 255, 0, 0));
imagesetpixel($palette_1, 1, 0, imagecolorallocate($palette_1, 0, 255, 0));

/* same image but with inverted colors allocations */
$palette_1b = imagecreate(2, 1);
imagesetpixel($palette_1b, 1, 0, imagecolorallocate($palette_1b, 0, 255, 0));
imagesetpixel($palette_1b, 0, 0, imagecolorallocate($palette_1b, 255, 0, 0));

/* a different palette image, red/blue */
$palette_2 = imagecreate(2, 1);
imagesetpixel($palette_2, 0, 0, imagecolorallocate($palette_2, 255, 0, 0));
imagesetpixel($palette_2, 1, 0, imagecolorallocate($palette_2, 0, 0, 255));

/* a truecolor image, matching the first one */
$truecolor_1 = imagecreatetruecolor(2, 1);
imagesetpixel($truecolor_1, 0, 0, imagecolorallocate($truecolor_1, 255, 0, 0));
imagesetpixel($truecolor_1, 1, 0, imagecolorallocate($truecolor_1, 0, 255, 0));

/* a truecolor image, identical to the previous one */
$truecolor_1a = imagecreatetruecolor(2, 1);
imagesetpixel($truecolor_1a, 0, 0, imagecolorallocate($truecolor_1a, 255, 0, 0));
imagesetpixel($truecolor_1a, 1, 0, imagecolorallocate($truecolor_1a, 0, 255, 0));

/* a truecolor image, differing only by color */
$truecolor_2 = imagecreatetruecolor(2, 1);
imagesetpixel($truecolor_2, 0, 0, imagecolorallocate($truecolor_2, 255, 0, 0));
imagesetpixel($truecolor_2, 1, 0, imagecolorallocate($truecolor_2, 0, 0, 255));

/* a truecolor image, differing only by alpha */
$truecolor_3 = imagecreatetruecolor(2, 1);
imagealphablending($truecolor_3, false);
imagesetpixel($truecolor_3, 0, 0, imagecolorallocatealpha($truecolor_3, 255, 0, 0, 0));
imagesetpixel($truecolor_3, 1, 0, imagecolorallocatealpha($truecolor_3, 0, 255, 0, 1));

print 'imagematch($palette_1, $palette_1b) = ';
var_dump(imagematch($palette_1, $palette_1b));

print 'imagematch($palette_1, $palette_2) = ';
var_dump(imagematch($palette_1, $palette_2));

print 'imagematch($palette_1, $truecolor_1) = ';
var_dump(imagematch($palette_1, $truecolor_1));

print 'imagematch($truecolor_1, $truecolor_1a) = ';
var_dump(imagematch($truecolor_1, $truecolor_1a));

print 'imagematch($truecolor_1, $truecolor_2) = ';
var_dump(imagematch($truecolor_1, $truecolor_2));

print 'imagematch($truecolor_1, $truecolor_3) = ';
var_dump(imagematch($truecolor_1, $truecolor_3));
--EXPECT--
imagematch($palette_1, $palette_1b) = bool(true)
imagematch($palette_1, $palette_2) = bool(false)
imagematch($palette_1, $truecolor_1) = bool(true)
imagematch($truecolor_1, $truecolor_1a) = bool(true)
imagematch($truecolor_1, $truecolor_2) = bool(false)
imagematch($truecolor_1, $truecolor_3) = bool(false)
Loading