Skip to content

Commit 03bd433

Browse files
committed
Add imagegetinterpolation()
While `imagesetinterpolation()` is available as of PHP 5.5.0, there is no according getter function, so users would have to track the current interpolation method manually. To remedy this, we introduce `imagegetinterpolation()` as thin wrapper for `gdImageGetInterpolationMethod()` (which has been introduced with libgd 2.1.1), and use `im->interpolation_id` as fallback for older libgd. Since our bundled libgd does not yet have this function, we add it. We also simplify the recently introduced bug79068.phpt, where it is sufficient to check that the interpolation method has not been changed.
1 parent 127d6f3 commit 03bd433

12 files changed

+123
-14
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ PHP NEWS
2626
. Made the $num_points parameter of php_imagepolygon optional. (cmb)
2727
. Removed deprecated image2wbmp(). (cmb)
2828
. Removed deprecated png2wbmp() and jpeg2wbmp(). (cmb)
29+
. Added imagegetinterpolation(). (cmb)
2930

3031
- Intl:
3132
. Removed deprecated INTL_IDNA_VARIANT_2003. (cmb)

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ PHP 8.0 UPGRADE NOTES
394394
imagefilledpolygon() is now optional, i.e. these functions may be called
395395
with either 3 or 4 arguments. If the arguments is omitted, it is calculated
396396
as count($points)/2.
397+
. The function imagegetinterpolation() to get the current interpolation method
398+
has been added.
397399

398400
========================================
399401
10. New Global Constants

ext/gd/config.m4

+9-8
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,15 @@ AC_DEFUN([PHP_GD_JISX0208],[
120120
])
121121

122122
AC_DEFUN([PHP_GD_CHECK_VERSION],[
123-
PHP_CHECK_LIBRARY(gd, gdImageCreateFromPng, [AC_DEFINE(HAVE_GD_PNG, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
124-
PHP_CHECK_LIBRARY(gd, gdImageCreateFromWebp, [AC_DEFINE(HAVE_GD_WEBP, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
125-
PHP_CHECK_LIBRARY(gd, gdImageCreateFromJpeg, [AC_DEFINE(HAVE_GD_JPG, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
126-
PHP_CHECK_LIBRARY(gd, gdImageCreateFromXpm, [AC_DEFINE(HAVE_GD_XPM, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
127-
PHP_CHECK_LIBRARY(gd, gdImageCreateFromBmp, [AC_DEFINE(HAVE_GD_BMP, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
128-
PHP_CHECK_LIBRARY(gd, gdImageCreateFromTga, [AC_DEFINE(HAVE_GD_TGA, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
129-
PHP_CHECK_LIBRARY(gd, gdImageStringFT, [AC_DEFINE(HAVE_GD_FREETYPE, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
130-
PHP_CHECK_LIBRARY(gd, gdVersionString, [AC_DEFINE(HAVE_GD_LIBVERSION, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
123+
PHP_CHECK_LIBRARY(gd, gdImageCreateFromPng, [AC_DEFINE(HAVE_GD_PNG, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
124+
PHP_CHECK_LIBRARY(gd, gdImageCreateFromWebp, [AC_DEFINE(HAVE_GD_WEBP, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
125+
PHP_CHECK_LIBRARY(gd, gdImageCreateFromJpeg, [AC_DEFINE(HAVE_GD_JPG, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
126+
PHP_CHECK_LIBRARY(gd, gdImageCreateFromXpm, [AC_DEFINE(HAVE_GD_XPM, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
127+
PHP_CHECK_LIBRARY(gd, gdImageCreateFromBmp, [AC_DEFINE(HAVE_GD_BMP, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
128+
PHP_CHECK_LIBRARY(gd, gdImageCreateFromTga, [AC_DEFINE(HAVE_GD_TGA, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
129+
PHP_CHECK_LIBRARY(gd, gdImageStringFT, [AC_DEFINE(HAVE_GD_FREETYPE, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
130+
PHP_CHECK_LIBRARY(gd, gdVersionString, [AC_DEFINE(HAVE_GD_LIBVERSION, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
131+
PHP_CHECK_LIBRARY(gd, gdImageGetInterpolationMethod, [AC_DEFINE(HAVE_GD_GET_INTERPOLATION, 1, [ ])], [], [ $GD_SHARED_LIBADD ])
131132
])
132133

133134
dnl

ext/gd/config.w32

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ if (PHP_GD != "no") {
7272
/D HAVE_LIBPNG \
7373
/D HAVE_XPM \
7474
/D HAVE_COLORCLOSESTHWB \
75+
/D HAVE_GD_GET_INTERPOLATION \
7576
/D USE_GD_IOCTX \
7677
/D MSWIN32 \
7778
");

ext/gd/gd.c

+21
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ static const zend_function_entry gd_functions[] = {
294294
PHP_FE(imageaffine, arginfo_imageaffine)
295295
PHP_FE(imageaffinematrixconcat, arginfo_imageaffinematrixconcat)
296296
PHP_FE(imageaffinematrixget, arginfo_imageaffinematrixget)
297+
PHP_FE(imagegetinterpolation, arginfo_imagegetinterpolation)
297298
PHP_FE(imagesetinterpolation, arginfo_imagesetinterpolation)
298299
PHP_FE(imagesettile, arginfo_imagesettile)
299300
PHP_FE(imagesetbrush, arginfo_imagesetbrush)
@@ -4100,6 +4101,26 @@ PHP_FUNCTION(imageaffinematrixconcat)
41004101
}
41014102
} /* }}} */
41024103

4104+
/* {{{ proto resource imagegetinterpolation(resource im)
4105+
Get the default interpolation method. */
4106+
PHP_FUNCTION(imagegetinterpolation)
4107+
{
4108+
zval *IM;
4109+
gdImagePtr im;
4110+
4111+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
4112+
RETURN_THROWS();
4113+
}
4114+
im = php_gd_libgdimageptr_from_zval_p(IM);
4115+
4116+
#ifdef HAVE_GD_GET_INTERPOLATION
4117+
RETURN_LONG(gdImageGetInterpolationMethod(im));
4118+
#else
4119+
RETURN_LONG(im->interpolation_id);
4120+
#endif
4121+
}
4122+
/* }}} */
4123+
41034124
/* {{{ proto resource imagesetinterpolation(resource im [, int method]])
41044125
Set the default interpolation method, passing -1 or 0 sets it to the libgd default (bilinear). */
41054126
PHP_FUNCTION(imagesetinterpolation)

ext/gd/gd.stub.php

+2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ function imageaffinematrixget(int $type, $options = UNKNOWN): array|false {}
232232

233233
function imageaffinematrixconcat(array $m1, array $m2): array|false {}
234234

235+
function imagegetinterpolation(GdImage $im): int {}
236+
235237
function imagesetinterpolation(GdImage $im, int $method = IMG_BILENEAR_FIXED): bool {}
236238

237239
function imageresolution(GdImage $im, int $res_x = UNKNOWN, int $res_y = UNKNOWN): array|bool {}

ext/gd/gd_arginfo.h

+2
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_imageaffinematrixconcat, 0, 2, M
556556
ZEND_ARG_TYPE_INFO(0, m2, IS_ARRAY, 0)
557557
ZEND_END_ARG_INFO()
558558

559+
#define arginfo_imagegetinterpolation arginfo_imagecolorstotal
560+
559561
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imagesetinterpolation, 0, 1, _IS_BOOL, 0)
560562
ZEND_ARG_OBJ_INFO(0, im, GdImage, 0)
561563
ZEND_ARG_TYPE_INFO(0, method, IS_LONG, 0)

ext/gd/libgd/gd_interpolation.c

+23
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,29 @@ int gdImageSetInterpolationMethod(gdImagePtr im, gdInterpolationMethod id)
25342534
return 1;
25352535
}
25362536

2537+
/**
2538+
* Function: gdImageGetInterpolationMethod
2539+
*
2540+
* Get the current interpolation method
2541+
*
2542+
* This is here so that the value can be read via a language or VM with an FFI
2543+
* but no (portable) way to extract the value from the struct.
2544+
*
2545+
* Parameters:
2546+
* im - The image.
2547+
*
2548+
* Returns:
2549+
* The current interpolation method.
2550+
*
2551+
* See also:
2552+
* - <gdInterpolationMethod>
2553+
* - <gdImageSetInterpolationMethod>
2554+
*/
2555+
gdInterpolationMethod gdImageGetInterpolationMethod(gdImagePtr im)
2556+
{
2557+
return im->interpolation_id;
2558+
}
2559+
25372560
#ifdef _MSC_VER
25382561
# pragma optimize("", on)
25392562
#endif

ext/gd/php_gd.h

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ PHP_FUNCTION(imagescale);
134134
PHP_FUNCTION(imageaffine);
135135
PHP_FUNCTION(imageaffinematrixget);
136136
PHP_FUNCTION(imageaffinematrixconcat);
137+
PHP_FUNCTION(imagegetinterpolation);
137138
PHP_FUNCTION(imagesetinterpolation);
138139

139140
PHP_FUNCTION(imagesetthickness);

ext/gd/tests/bug79068.phpt

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ if (!extension_loaded('gd')) die('skip gd extension not available');
66
?>
77
--FILE--
88
<?php
9-
require_once __DIR__ . '/func.inc';
10-
119
$src = imagecreatetruecolor(100, 100);
1210
imagefilledrectangle($src, 0, 0, 99, 99, 0xffffff);
1311
imageline($src, 10, 10, 90, 90, 0x000000);
1412
imagesetinterpolation($src, IMG_BSPLINE);
1513
imageaffine($src, [1, 1, 1, 1, 1, 1]);
16-
$dst = imagerotate($src, 80, 0xffffff);
17-
18-
test_image_equals_file(__DIR__ . '/bug79068.png', $dst);
14+
var_dump(imagegetinterpolation($src) === IMG_BSPLINE);
1915
?>
2016
--EXPECT--
21-
The images are equal.
17+
bool(true)

ext/gd/tests/bug79068.png

-798 Bytes
Binary file not shown.
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--TEST--
2+
imagegetinterpolation() and imagesetinterpolation() basic test
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$methods = array(
10+
IMG_BELL,
11+
IMG_BESSEL,
12+
IMG_BILINEAR_FIXED,
13+
IMG_BICUBIC,
14+
IMG_BICUBIC_FIXED,
15+
IMG_BLACKMAN,
16+
IMG_BOX,
17+
IMG_BSPLINE,
18+
IMG_CATMULLROM,
19+
IMG_GAUSSIAN,
20+
IMG_GENERALIZED_CUBIC,
21+
IMG_HERMITE,
22+
IMG_HAMMING,
23+
IMG_HANNING,
24+
IMG_MITCHELL,
25+
IMG_NEAREST_NEIGHBOUR,
26+
IMG_POWER,
27+
IMG_QUADRATIC,
28+
IMG_SINC,
29+
IMG_TRIANGLE,
30+
IMG_WEIGHTED4,
31+
);
32+
$im = imagecreate(8, 8);
33+
foreach ($methods as $method) {
34+
imagesetinterpolation($im, $method);
35+
var_dump(imagegetinterpolation($im) === $method);
36+
}
37+
?>
38+
--EXPECT--
39+
bool(true)
40+
bool(true)
41+
bool(true)
42+
bool(true)
43+
bool(true)
44+
bool(true)
45+
bool(true)
46+
bool(true)
47+
bool(true)
48+
bool(true)
49+
bool(true)
50+
bool(true)
51+
bool(true)
52+
bool(true)
53+
bool(true)
54+
bool(true)
55+
bool(true)
56+
bool(true)
57+
bool(true)
58+
bool(true)
59+
bool(true)

0 commit comments

Comments
 (0)