From 1157e146b99d72c2dc340756606a3be87f683935 Mon Sep 17 00:00:00 2001 From: Giovanni Giacobbi Date: Wed, 10 Jul 2024 14:03:47 +0200 Subject: [PATCH] Add imagematch() function This function is a replacement for the missing imagecompare() which does not properly check for transparency of truecolor images. --- ext/gd/gd.c | 57 ++++++++++++++++++++++++ ext/gd/gd.stub.php | 2 + ext/gd/gd_arginfo.h | 6 ++- ext/gd/tests/imagematch_basic.phpt | 71 ++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 ext/gd/tests/imagematch_basic.phpt diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 5af2d4a5152bb..1e8d0938c6d15 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -3254,6 +3254,63 @@ PHP_FUNCTION(imagecopyresized) } /* }}} */ +#define GET_TRUECOLOR_PIXEL(px, im, x, y) \ + 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; + 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; + } + } + } + + RETURN_BOOL(result); +} +/* }}} */ + +#undef GET_TRUECOLOR_PIXEL + /* {{{ Get image width */ PHP_FUNCTION(imagesx) { diff --git a/ext/gd/gd.stub.php b/ext/gd/gd.stub.php index 347e43e728b87..46d76fe474462 100644 --- a/ext/gd/gd.stub.php +++ b/ext/gd/gd.stub.php @@ -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 {} diff --git a/ext/gd/gd_arginfo.h b/ext/gd/gd_arginfo.h index f68b34d5e101b..a30cfbd725f98 100644 --- a/ext/gd/gd_arginfo.h +++ b/ext/gd/gd_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 0f8a22bff1d123313f37da400500e573baace837 */ + * Stub hash: f46f454200e4dc01949b48e243c5e64459d5bdf8 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gd_info, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -459,6 +459,8 @@ ZEND_END_ARG_INFO() #define arginfo_imagecopyresized arginfo_imagecopyresampled +#define arginfo_imagematch arginfo_imagecolormatch + #define arginfo_imagesx arginfo_imagecolorstotal #define arginfo_imagesy arginfo_imagecolorstotal @@ -690,6 +692,7 @@ ZEND_FUNCTION(imagecopy); ZEND_FUNCTION(imagecopymerge); ZEND_FUNCTION(imagecopymergegray); ZEND_FUNCTION(imagecopyresized); +ZEND_FUNCTION(imagematch); ZEND_FUNCTION(imagesx); ZEND_FUNCTION(imagesy); ZEND_FUNCTION(imagesetclip); @@ -830,6 +833,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(imagecopymerge, arginfo_imagecopymerge) ZEND_FE(imagecopymergegray, arginfo_imagecopymergegray) ZEND_FE(imagecopyresized, arginfo_imagecopyresized) + ZEND_FE(imagematch, arginfo_imagematch) ZEND_FE(imagesx, arginfo_imagesx) ZEND_FE(imagesy, arginfo_imagesy) ZEND_FE(imagesetclip, arginfo_imagesetclip) diff --git a/ext/gd/tests/imagematch_basic.phpt b/ext/gd/tests/imagematch_basic.phpt new file mode 100644 index 0000000000000..27e3bdf33f4e4 --- /dev/null +++ b/ext/gd/tests/imagematch_basic.phpt @@ -0,0 +1,71 @@ +--TEST-- +imagematch() function test +--EXTENSIONS-- +gd +--SKIPIF-- + +--FILE-- +