Skip to content

ext/gd: adding imagecopyrotated. #11519

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 2 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

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ PHP 8.3 UPGRADE NOTES
- DOM:
. Added DOMNode::contains() and DOMNameSpaceNode::contains().

- GD:
. Added imagerotated which copy a rotated area from a defined position and angle.

- JSON:
. Added json_validate(), which returns whether the json is valid for
the given $depth and $options.
Expand Down
1 change: 1 addition & 0 deletions ext/gd/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ AC_DEFUN([PHP_GD_CHECK_VERSION],[
PHP_CHECK_LIBRARY(gd, gdImageStringFT, [AC_DEFINE(HAVE_GD_FREETYPE, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
PHP_CHECK_LIBRARY(gd, gdVersionString, [AC_DEFINE(HAVE_GD_LIBVERSION, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
PHP_CHECK_LIBRARY(gd, gdImageGetInterpolationMethod, [AC_DEFINE(HAVE_GD_GET_INTERPOLATION, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
PHP_CHECK_LIBRARY(gd, gdImageCopyRotated, [AC_DEFINE(HAVE_GD_COPY_ROTATED, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
])

dnl
Expand Down
53 changes: 53 additions & 0 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,59 @@ PHP_FUNCTION(imagecopyresampled)
}
/* }}} */

#if defined(HAVE_GD_COPY_ROTATED)
PHP_FUNCTION(imagecopyrotated)
{
zval *SIM, *DIM;
zend_long SX, SY, SW, SH, A;
gdImagePtr im_dst, im_src;
int srcH, srcW, srcY, srcX, angle;
double dstX, dstY;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOddlllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &dstX, &dstY, &SX, &SY, &SW, &SH, &A) == FAILURE) {
RETURN_THROWS();
}

im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
im_src = php_gd_libgdimageptr_from_zval_p(SIM);

srcX = SX;
srcY = SY;
srcW = SW;
srcH = SH;
angle = A;

if (srcX < 0) {
zend_argument_value_error(5, "must be positive");
RETURN_THROWS();
}

if (srcY < 0) {
zend_argument_value_error(6, "must be positive");
RETURN_THROWS();
}

if (srcW <= 0) {
zend_argument_value_error(7, "must be greater than 0");
RETURN_THROWS();
}

if (srcH <= 0) {
zend_argument_value_error(8, "must be greater than 0");
RETURN_THROWS();
}

if (angle <= 0) {
zend_argument_value_error(9, "must be greater than 0");
RETURN_THROWS();
}


gdImageCopyRotated(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, angle);
Comment on lines +1105 to +1106
Copy link
Member

Choose a reason for hiding this comment

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

Only thing would be to check that the zend_long being passed fits into the value storable by a C int and if not throw a ValueError like we do in other functions

Copy link
Member Author

Choose a reason for hiding this comment

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

How does it look to you now ?

}
#endif


#ifdef PHP_WIN32
/* {{{ Grab a window or its client area using a windows handle (HWND property in COM instance) */
PHP_FUNCTION(imagegrabwindow)
Expand Down
4 changes: 4 additions & 0 deletions ext/gd/gd.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ function imagecolorexactalpha(GdImage $image, int $red, int $green, int $blue, i

function imagecopyresampled(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 {}

#ifdef HAVE_GD_COPY_ROTATED
function imagecopyrotated(GdImage $dst_image, GdImage $src_image, float $dst_x, float $dst_y, int $src_x, int $src_y, int $src_width, int $src_height, int $angle): void {}
#endif

#ifdef PHP_WIN32

/** @refcount 1 */
Expand Down
22 changes: 21 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.

67 changes: 67 additions & 0 deletions ext/gd/tests/copyrotated.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
--TEST--
imagecopyrotated
--EXTENSIONS--
gd
--SKIPIF--
<?php
if (!function_exists("imagecopyrotated")) die("skip requires imagecopyrotated");
?>
--FILE--
<?php

$src_tc = imagecreatetruecolor(5,5);
imagefill($src_tc, 0,0, 0x000000);
imagesetpixel($src_tc, 1, 2, 0x0000ff);
imagesetpixel($src_tc, 2, 2, 0xffffff);
imagesetpixel($src_tc, 3, 2, 0xff0000);


$dst_tc = imagecreatetruecolor(5,5);
imagecopyrotated($dst_tc, $src_tc, 3.0,3.0, 0,0, imagesx($src_tc), imagesy($src_tc), 180);
$p1 = imagecolorat($dst_tc, 1, 2) == imagecolorat($src_tc, 3, 2);
$p2 = imagecolorat($dst_tc, 2, 2) == imagecolorat($src_tc, 2, 2);
$p3 = imagecolorat($dst_tc, 3, 2) == imagecolorat($src_tc, 1, 2);

if ($p1 && $p2 && $p3) {
echo "TC/TC: ok\n";
}

try {
imagecopyrotated($dst_tc, $src_tc, 3.0,3.0, -10,0, imagesx($src_tc), imagesy($src_tc), 180);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
imagecopyrotated($dst_tc, $src_tc, 3.0,3.0, 0,PHP_INT_MAX, imagesx($src_tc), imagesy($src_tc), 180);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
imagecopyrotated($dst_tc, $src_tc, 3.0,3.0, 0,0, -5, imagesy($src_tc), 180);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
imagecopyrotated($dst_tc, $src_tc, 3.0,3.0, 0,0, imagesx($src_tc), PHP_INT_MAX, 180);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
imagecopyrotated($dst_tc, $src_tc, 3.0,3.0, 0,0, imagesx($src_tc), imagesy($src_tc), PHP_INT_MAX);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

imagedestroy($src_tc); imagedestroy($dst_tc);
?>
--EXPECT--
TC/TC: ok
imagecopyrotated(): Argument #5 ($src_x) must be positive
imagecopyrotated(): Argument #6 ($src_y) must be positive
imagecopyrotated(): Argument #7 ($src_width) must be greater than 0
imagecopyrotated(): Argument #8 ($src_height) must be greater than 0
imagecopyrotated(): Argument #9 ($angle) must be greater than 0