-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
base: master
Are you sure you want to change the base?
Add imagematch() function #14914
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to use Lines 768 to 769 in 6fb79e2
|
||||||
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
{ | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
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