Skip to content

Commit 5fd808e

Browse files
committed
Add PHP bindings for setting and getting the image resolution
We expose the image resolution related GD functionality to userland by introducing `imagesetresolution()`, `imageresolutionx()` and `imageresolutiony()` as simple wrappers.
1 parent 932c20f commit 5fd808e

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

ext/gd/gd.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,20 @@ ZEND_BEGIN_ARG_INFO(arginfo_imagesetinterpolation, 0)
821821
ZEND_ARG_INFO(0, method)
822822
ZEND_END_ARG_INFO()
823823

824+
ZEND_BEGIN_ARG_INFO(arginfo_imagesetresolution, 0)
825+
ZEND_ARG_INFO(0, im)
826+
ZEND_ARG_INFO(0, res_x)
827+
ZEND_ARG_INFO(0, res_y)
828+
ZEND_END_ARG_INFO()
829+
830+
ZEND_BEGIN_ARG_INFO(arginfo_imageresolutionx, 0)
831+
ZEND_ARG_INFO(0, im)
832+
ZEND_END_ARG_INFO()
833+
834+
ZEND_BEGIN_ARG_INFO(arginfo_imageresolutiony, 0)
835+
ZEND_ARG_INFO(0, im)
836+
ZEND_END_ARG_INFO()
837+
824838
/* }}} */
825839

826840
/* {{{ gd_functions[]
@@ -965,6 +979,10 @@ const zend_function_entry gd_functions[] = {
965979
PHP_FE(imagefilter, arginfo_imagefilter)
966980
PHP_FE(imageconvolution, arginfo_imageconvolution)
967981

982+
PHP_FE(imagesetresolution, arginfo_imagesetresolution)
983+
PHP_FE(imageresolutionx, arginfo_imageresolutionx)
984+
PHP_FE(imageresolutiony, arginfo_imageresolutiony)
985+
968986
PHP_FE_END
969987
};
970988
/* }}} */
@@ -4991,6 +5009,64 @@ PHP_FUNCTION(imagesetinterpolation)
49915009
}
49925010
/* }}} */
49935011

5012+
/* {{{ proto void imagesetresolution(resource im , int res_x, int res_y)
5013+
Set the resolution of the image in DPI. */
5014+
PHP_FUNCTION(imagesetresolution)
5015+
{
5016+
zval *IM;
5017+
gdImagePtr im;
5018+
zend_long res_x, res_y;
5019+
5020+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rll", &IM, &res_x, &res_y) == FAILURE) {
5021+
return;
5022+
}
5023+
5024+
if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
5025+
RETURN_FALSE;
5026+
}
5027+
5028+
gdImageSetResolution(im, res_x, res_y);
5029+
}
5030+
/* }}} */
5031+
5032+
/* {{{ proto int imageresolutionx(resource im)
5033+
Get the horizontal resolution of the image in DPI. */
5034+
PHP_FUNCTION(imageresolutionx)
5035+
{
5036+
zval *IM;
5037+
gdImagePtr im;
5038+
5039+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
5040+
return;
5041+
}
5042+
5043+
if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
5044+
RETURN_FALSE;
5045+
}
5046+
5047+
RETURN_LONG(gdImageResolutionX(im));
5048+
}
5049+
/* }}} */
5050+
5051+
/* {{{ proto int imageresolutiony(resource im)
5052+
Get the vertical resolution of the image in DPI. */
5053+
PHP_FUNCTION(imageresolutiony)
5054+
{
5055+
zval *IM;
5056+
gdImagePtr im;
5057+
5058+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
5059+
return;
5060+
}
5061+
5062+
if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
5063+
RETURN_FALSE;
5064+
}
5065+
5066+
RETURN_LONG(gdImageResolutionY(im));
5067+
}
5068+
/* }}} */
5069+
49945070
/*
49955071
* Local variables:
49965072
* tab-width: 4

ext/gd/php_gd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ PHP_FUNCTION(imagexbm);
198198
PHP_FUNCTION(imagefilter);
199199
PHP_FUNCTION(imageconvolution);
200200

201+
PHP_FUNCTION(imagesetresolution);
202+
PHP_FUNCTION(imageresolutionx);
203+
PHP_FUNCTION(imageresolutiony);
204+
201205
PHP_GD_API int phpi_get_le_gd(void);
202206

203207
#else
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Set and get image resolution of JPEG images
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
if (!(imagetypes() & IMG_JPEG)) die('skip JPEG support not available');
7+
?>
8+
--FILE--
9+
<?php
10+
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'imageresolution_jpeg.jpeg';
11+
12+
$im = imagecreate(100, 100);
13+
imagesetresolution($im, 72, 300);
14+
$red = imagecolorallocate($im, 255, 0, 0);
15+
imagefilledrectangle($im, 0,0, 99,99, $red);
16+
imagejpeg($im, $filename);
17+
18+
$im = imagecreatefromjpeg($filename);
19+
var_dump(
20+
imageresolutionx($im),
21+
imageresolutiony($im)
22+
);
23+
?>
24+
===DONE===
25+
--EXPECT--
26+
int(72)
27+
int(300)
28+
===DONE===
29+
--CLEAN--
30+
<?php
31+
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'imageresolution_jpeg.jpeg');
32+
?>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Set and get image resolution of PNG images
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'imageresolution_png.png';
10+
11+
$im = imagecreate(100, 100);
12+
imagesetresolution($im, 72, 300);
13+
$red = imagecolorallocate($im, 255, 0, 0);
14+
imagefilledrectangle($im, 0,0, 99,99, $red);
15+
imagepng($im, $filename);
16+
17+
$im = imagecreatefrompng($filename);
18+
var_dump(
19+
imageresolutionx($im),
20+
imageresolutiony($im)
21+
);
22+
?>
23+
===DONE===
24+
--EXPECT--
25+
int(72)
26+
int(300)
27+
===DONE===
28+
--CLEAN--
29+
<?php
30+
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'imageresolution_png.png');
31+
?>

0 commit comments

Comments
 (0)