From 209d4220d53bce6f4ac4d69a2781e98f47e17710 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 19 Sep 2016 22:48:22 +0200 Subject: [PATCH] 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. --- ext/gd/gd.c | 39 +++++++++++++++++++++++ ext/gd/php_gd.h | 2 ++ ext/gd/tests/imageresolution_jpeg.phpt | 43 ++++++++++++++++++++++++++ ext/gd/tests/imageresolution_png.phpt | 42 +++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 ext/gd/tests/imageresolution_jpeg.phpt create mode 100644 ext/gd/tests/imageresolution_png.phpt diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 6cfcce8c4c..5ed7744a3e 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -821,6 +821,12 @@ ZEND_BEGIN_ARG_INFO(arginfo_imagesetinterpolation, 0) ZEND_ARG_INFO(0, method) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_imageresolution, 0, 0, 1) + ZEND_ARG_INFO(0, im) + ZEND_ARG_INFO(0, res_x) + ZEND_ARG_INFO(0, res_y) +ZEND_END_ARG_INFO() + /* }}} */ /* {{{ gd_functions[] @@ -965,6 +971,8 @@ const zend_function_entry gd_functions[] = { PHP_FE(imagefilter, arginfo_imagefilter) PHP_FE(imageconvolution, arginfo_imageconvolution) + PHP_FE(imageresolution, arginfo_imageresolution) + PHP_FE_END }; /* }}} */ @@ -4991,6 +4999,37 @@ PHP_FUNCTION(imagesetinterpolation) } /* }}} */ +/* {{{ proto array imageresolution(resource im [, res_x, [res_y]]) + Get or set the resolution of the image in DPI. */ +PHP_FUNCTION(imageresolution) +{ + zval *IM; + gdImagePtr im; + zend_long res_x = GD_RESOLUTION, res_y = GD_RESOLUTION; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &IM, &res_x, &res_y) == FAILURE) { + return; + } + + if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) { + RETURN_FALSE; + } + + switch (ZEND_NUM_ARGS()) { + case 3: + gdImageSetResolution(im, res_x, res_y); + RETURN_TRUE; + case 2: + gdImageSetResolution(im, res_x, res_x); + RETURN_TRUE; + default: + array_init(return_value); + add_next_index_long(return_value, gdImageResolutionX(im)); + add_next_index_long(return_value, gdImageResolutionY(im)); + } +} +/* }}} */ + /* * Local variables: * tab-width: 4 diff --git a/ext/gd/php_gd.h b/ext/gd/php_gd.h index 19e7063d9c..3363283e9d 100644 --- a/ext/gd/php_gd.h +++ b/ext/gd/php_gd.h @@ -198,6 +198,8 @@ PHP_FUNCTION(imagexbm); PHP_FUNCTION(imagefilter); PHP_FUNCTION(imageconvolution); +PHP_FUNCTION(imageresolution); + PHP_GD_API int phpi_get_le_gd(void); #else diff --git a/ext/gd/tests/imageresolution_jpeg.phpt b/ext/gd/tests/imageresolution_jpeg.phpt new file mode 100644 index 0000000000..a659e2682a --- /dev/null +++ b/ext/gd/tests/imageresolution_jpeg.phpt @@ -0,0 +1,43 @@ +--TEST-- +Set and get image resolution of JPEG images +--SKIPIF-- + +--FILE-- + +===DONE=== +--EXPECT-- +array(2) { + [0]=> + int(71) + [1]=> + int(71) +} +array(2) { + [0]=> + int(71) + [1]=> + int(299) +} +===DONE=== +--CLEAN-- + diff --git a/ext/gd/tests/imageresolution_png.phpt b/ext/gd/tests/imageresolution_png.phpt new file mode 100644 index 0000000000..5bb835b4e8 --- /dev/null +++ b/ext/gd/tests/imageresolution_png.phpt @@ -0,0 +1,42 @@ +--TEST-- +Set and get image resolution of PNG images +--SKIPIF-- + +--FILE-- + +===DONE=== +--EXPECT-- +array(2) { + [0]=> + int(71) + [1]=> + int(71) +} +array(2) { + [0]=> + int(71) + [1]=> + int(299) +} +===DONE=== +--CLEAN-- + -- 2.40.0