Skip to content

Commit 209d422

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 `imageresolution()` as getter/setter. Given only the image argument, it returns the current resolution as indexed array. Given only a second argument, it sets the horizontal and vertical resolution to this value. Given three arguments, it sets the horizontal and vertical resolution to the given arguments, respectively.
1 parent 932c20f commit 209d422

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

ext/gd/gd.c

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

824+
ZEND_BEGIN_ARG_INFO_EX(arginfo_imageresolution, 0, 0, 1)
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+
824830
/* }}} */
825831

826832
/* {{{ gd_functions[]
@@ -965,6 +971,8 @@ const zend_function_entry gd_functions[] = {
965971
PHP_FE(imagefilter, arginfo_imagefilter)
966972
PHP_FE(imageconvolution, arginfo_imageconvolution)
967973

974+
PHP_FE(imageresolution, arginfo_imageresolution)
975+
968976
PHP_FE_END
969977
};
970978
/* }}} */
@@ -4991,6 +4999,37 @@ PHP_FUNCTION(imagesetinterpolation)
49914999
}
49925000
/* }}} */
49935001

5002+
/* {{{ proto array imageresolution(resource im [, res_x, [res_y]])
5003+
Get or set the resolution of the image in DPI. */
5004+
PHP_FUNCTION(imageresolution)
5005+
{
5006+
zval *IM;
5007+
gdImagePtr im;
5008+
zend_long res_x = GD_RESOLUTION, res_y = GD_RESOLUTION;
5009+
5010+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &IM, &res_x, &res_y) == FAILURE) {
5011+
return;
5012+
}
5013+
5014+
if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
5015+
RETURN_FALSE;
5016+
}
5017+
5018+
switch (ZEND_NUM_ARGS()) {
5019+
case 3:
5020+
gdImageSetResolution(im, res_x, res_y);
5021+
RETURN_TRUE;
5022+
case 2:
5023+
gdImageSetResolution(im, res_x, res_x);
5024+
RETURN_TRUE;
5025+
default:
5026+
array_init(return_value);
5027+
add_next_index_long(return_value, gdImageResolutionX(im));
5028+
add_next_index_long(return_value, gdImageResolutionY(im));
5029+
}
5030+
}
5031+
/* }}} */
5032+
49945033
/*
49955034
* Local variables:
49965035
* tab-width: 4

ext/gd/php_gd.h

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

201+
PHP_FUNCTION(imageresolution);
202+
201203
PHP_GD_API int phpi_get_le_gd(void);
202204

203205
#else
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
$exp = imagecreate(100, 100);
13+
imagecolorallocate($exp, 255, 0, 0);
14+
15+
imageresolution($exp, 71);
16+
imagejpeg($exp, $filename);
17+
$act = imagecreatefromjpeg($filename);
18+
var_dump(imageresolution($act));
19+
20+
imageresolution($exp, 71, 299);
21+
imagejpeg($exp, $filename);
22+
$act = imagecreatefromjpeg($filename);
23+
var_dump(imageresolution($act));
24+
?>
25+
===DONE===
26+
--EXPECT--
27+
array(2) {
28+
[0]=>
29+
int(71)
30+
[1]=>
31+
int(71)
32+
}
33+
array(2) {
34+
[0]=>
35+
int(71)
36+
[1]=>
37+
int(299)
38+
}
39+
===DONE===
40+
--CLEAN--
41+
<?php
42+
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'imageresolution_jpeg.jpeg');
43+
?>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
$exp = imagecreate(100, 100);
12+
imagecolorallocate($exp, 255, 0, 0);
13+
14+
imageresolution($exp, 71);
15+
imagepng($exp, $filename);
16+
$act = imagecreatefrompng($filename);
17+
var_dump(imageresolution($act));
18+
19+
imageresolution($exp, 71, 299);
20+
imagepng($exp, $filename);
21+
$act = imagecreatefrompng($filename);
22+
var_dump(imageresolution($act));
23+
?>
24+
===DONE===
25+
--EXPECT--
26+
array(2) {
27+
[0]=>
28+
int(71)
29+
[1]=>
30+
int(71)
31+
}
32+
array(2) {
33+
[0]=>
34+
int(71)
35+
[1]=>
36+
int(299)
37+
}
38+
===DONE===
39+
--CLEAN--
40+
<?php
41+
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'imageresolution_png.png');
42+
?>

0 commit comments

Comments
 (0)