Skip to content

ext/gd adding imagecompare. #14877

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
15 changes: 15 additions & 0 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -4337,6 +4337,21 @@ PHP_FUNCTION(imageresolution)
}
/* }}} */

PHP_FUNCTION(imagecompare)
{
zval *IM1, *IM2;
gdImagePtr im1, im2;

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);

RETURN_LONG((zend_long)gdImageCompare(im1, im2));
Copy link
Member

Choose a reason for hiding this comment

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

Should this be normalized to -1/0/1 similarly to what ZEND_THREEWAY_COMPARE does?

Copy link
Member Author

Choose a reason for hiding this comment

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

It returns a mask.

Copy link
Member

Choose a reason for hiding this comment

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

A mask of what?

Copy link
Member Author

Choose a reason for hiding this comment

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

Those values added on the stub :

#define GD_CMP_IMAGE		1
#define GD_CMP_NUM_COLORS	2
#define GD_CMP_COLOR		4
#define GD_CMP_SIZE_X		8
#define GD_CMP_SIZE_Y		16
#define GD_CMP_TRANSPARENT	32
#define GD_CMP_BACKGROUND	64
#define GD_CMP_INTERLACE	128
#define GD_CMP_TRUECOLOR	256

Copy link
Member Author

Choose a reason for hiding this comment

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

So 0 => no difference or one or more of those values for each difference found.

}

/*********************************************************
*
Expand Down
48 changes: 48 additions & 0 deletions ext/gd/gd.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,52 @@
*/
const IMG_AFFINE_SHEAR_VERTICAL = UNKNOWN;

/**
* @var int
* @cvalue GD_CMP_IMAGE
*/
const IMG_CMP_IMAGE = UNKNOWN;
/**
* @var int
* @cvalue GD_CMP_NUM_COLORS
*/
const IMG_CMP_NUM_COLORS = UNKNOWN;
/**
* @var int
* @cvalue GD_CMP_COLOR
*/
const IMG_CMP_COLOR = UNKNOWN;
/**
* @var int
* @cvalue GD_CMP_SIZE_X
*/
const IMG_CMP_SIZE_X = UNKNOWN;
/**
* @var int
* @cvalue GD_CMP_SIZE_Y
*/
const IMG_CMP_SIZE_Y = UNKNOWN;
/**
* @var int
* @cvalue GD_CMP_TRANSPARENT
*/
const IMG_CMP_TRANSPARENT = UNKNOWN;
/**
* @var int
* @cvalue GD_CMP_BACKGROUND
*/
const IMG_CMP_BACKGROUND = UNKNOWN;
/**
* @var int
* @cvalue GD_CMP_INTERLACE
*/
const IMG_CMP_INTERLACE = UNKNOWN;
/**
* @var int
* @cvalue GD_CMP_TRUECOLOR
*/
const IMG_CMP_TRUECOLOR = UNKNOWN;

/**
* @var int
* @cvalue GD_BUNDLED
Expand Down Expand Up @@ -794,3 +840,5 @@ function imagesetinterpolation(GdImage $image, int $method = IMG_BILINEAR_FIXED)
* @refcount 1
*/
function imageresolution(GdImage $image, ?int $resolution_x = null, ?int $resolution_y = null): array|bool {}

function imagecompare(GdImage $image1, GdImage $image2): int {}
18 changes: 17 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.

30 changes: 30 additions & 0 deletions ext/gd/tests/imagecompare.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
imagecompare() function test
--EXTENSIONS--
gd
--SKIPIF--
<?php
if (!(imagetypes() & IMG_PNG)) die("skip No PNG support");
?>
--FILE--
<?php
$SAVE_DIR = __DIR__;
$SOURCE_IMG = $SAVE_DIR . "/test.png";
$im1 = imagecreatefrompng($SOURCE_IMG);
$im2 = imagecreatefrompng($SOURCE_IMG);

var_dump(imagecompare($im1, $im2) === 0);

$cl = imagecolorallocate($im2, 255, 0, 0);
imagecolortransparent($im2, $cl);
var_dump((imagecompare($im1, $im2) & IMG_CMP_TRANSPARENT) === IMG_CMP_TRANSPARENT);

$im3 = imagecreate(10, 10);
imagecopyresized($im3, $im2, 0, 0, 0, 0, 10, 10, 10, 10);
var_dump((imagecompare($im1, $im3) & IMG_CMP_SIZE_X) !== 0);
var_dump((imagecompare($im1, $im3) & IMG_CMP_SIZE_Y) !== 0);
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
Loading