Skip to content

Allow cloning of GdImage from gd 2.2.3 #10241

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 6 commits 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
19 changes: 18 additions & 1 deletion ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,31 @@ void php_gd_assign_libgdimageptr_as_extgdimage(zval *val, gdImagePtr image)
php_gd_exgdimage_from_zobj_p(Z_OBJ_P(val))->image = image;
}

#if GD_MAJOR_VERSION > 2 || (GD_MAJOR_VERSION == 2 && (GD_MINOR_VERSION > 2 || (GD_MINOR_VERSION == 2 && GD_RELEASE_VERSION >= 3)))
static zend_object *php_gd_clone(zend_object *zobj)
{
gdImagePtr oldim = php_gd_exgdimage_from_zobj_p(zobj)->image;
gdImagePtr newim = gdImageClone(oldim);
zval znew;

php_gd_assign_libgdimageptr_as_extgdimage(&znew, newim);

return Z_OBJ(znew);
}
#endif

static void php_gd_object_minit_helper(void)
{
gd_image_ce = register_class_GdImage();
gd_image_ce->create_object = php_gd_image_object_create;

/* setting up the object handlers for the GdImage class */
memcpy(&php_gd_image_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
php_gd_image_object_handlers.clone_obj = NULL;
#if GD_MAJOR_VERSION > 2 || (GD_MAJOR_VERSION == 2 && (GD_MINOR_VERSION > 2 || (GD_MINOR_VERSION == 2 && GD_RELEASE_VERSION >= 3)))
php_gd_image_object_handlers.clone_obj = php_gd_clone;
#else
php_gd_image_object_handlers.clone_obj = NULL;
#endif
php_gd_image_object_handlers.free_obj = php_gd_image_object_free;
php_gd_image_object_handlers.get_constructor = php_gd_image_object_get_constructor;
php_gd_image_object_handlers.compare = zend_objects_not_comparable;
Expand Down
26 changes: 26 additions & 0 deletions ext/gd/tests/gdimage_cloning.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Checks that GdImage instances can be cloned from gd > 2.2.3
--EXTENSIONS--
gd
--SKIPIF--
<?php if (version_compare(GD_VERSION, '2.2.3', '<')) die("Skipped: GdImage is not clonable before gd 2.2.3"); ?>
--FILE--
<?php

$foo = imagecreatetruecolor(32, 32);
$bar = clone $foo;

$red = imagecolorallocate($foo, 255, 0, 0);
imagefill($foo, 0, 0, $red);

$green = imagecolorallocate($bar, 0, 255, 0);
imagefill($bar, 0, 0, $green);

var_dump(imagecolorat($foo, 0, 0) === $red);
var_dump(imagecolorat($bar, 0, 0) === $green);


?>
--EXPECTF--
bool(true)
bool(true)
2 changes: 2 additions & 0 deletions ext/gd/tests/gdimage_prevent_cloning.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Checks that GdImage instances cannot be cloned
--EXTENSIONS--
gd
--SKIPIF--
<?php if (version_compare(GD_VERSION, '2.2.3', '>=')) die("Skipped: GdImage is clonable from gd 2.2.3"); ?>
--FILE--
<?php

Expand Down