From 79f6c1a8c9a0119139bdfd2541b13fbfdaba5313 Mon Sep 17 00:00:00 2001 From: Augusto Cesar Ferreira Date: Tue, 14 Dec 2021 16:45:05 +0100 Subject: [PATCH 1/6] Adds support for image resolution for GD --- docs/usage/introduction.rst | 6 +++--- src/Gd/Image.php | 13 +++++++++++++ src/Image/ImageInterface.php | 7 +++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/docs/usage/introduction.rst b/docs/usage/introduction.rst index 5bf33ef45..39c411794 100644 --- a/docs/usage/introduction.rst +++ b/docs/usage/introduction.rst @@ -29,7 +29,7 @@ Install the dependencies using composer.phar and use Imagine : .. code-block:: none php composer.phar install - + .. code-block:: php color('#000', 0); $image = $imagine->create($size, $color); - + To use a solid background color, for example orange, provide an alpha of 100. .. code-block:: php @@ -173,7 +173,7 @@ Three options groups are currently supported : quality, resolution and flatten. Default values are 75 for Jpeg quality, 7 for Png compression level, 75 for webp quality and 72 dpi for x/y-resolution. .. NOTE:: - GD does not support resolution options group + GD does support resolution options group only with PHP 7 >= 7.2, PHP 8 The following example demonstrates the basic quality settings. diff --git a/src/Gd/Image.php b/src/Gd/Image.php index e418576e4..b69efb401 100644 --- a/src/Gd/Image.php +++ b/src/Gd/Image.php @@ -747,6 +747,19 @@ private function finalizeOptions(Format $format, array $options) break; } + if (isset($options['resolution-units']) && isset($options['resolution-x']) && function_exists('imageresolution')) { + + $resolution_x = $options['resolution-x']; + $resolution_y = isset($options['resolution-y']) ? $options['resolution-y'] : $resolution_x; + + if ($options['resolution-units'] === ImageInterface::RESOLUTION_PIXELSPERCENTIMETER) { + $resolution_x = $resolution_x * ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER; + $resolution_y = $resolution_y * ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER; + } + + imageresolution($this->resource, $resolution_x, $resolution_y); + } + return $result; } diff --git a/src/Image/ImageInterface.php b/src/Image/ImageInterface.php index 6b5abd529..abcda0ebb 100644 --- a/src/Image/ImageInterface.php +++ b/src/Image/ImageInterface.php @@ -32,6 +32,13 @@ interface ImageInterface extends ManipulatorInterface */ const RESOLUTION_PIXELSPERCENTIMETER = 'ppc'; + /** + * Multiplier for converting resolution from ppc to ppi + * + * @var float + */ + const RESOLUTION_PPC_TO_PPI_MULTIPLIER = 2.54; + /** * Image interlacing: none. * From 35ee2af13352cd90c54c597cecdacb2e990dc848 Mon Sep 17 00:00:00 2001 From: Augusto Cesar Ferreira Date: Tue, 14 Dec 2021 12:53:24 -0300 Subject: [PATCH 2/6] Fix cs --- src/Gd/Image.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Gd/Image.php b/src/Gd/Image.php index b69efb401..948d1e664 100644 --- a/src/Gd/Image.php +++ b/src/Gd/Image.php @@ -188,7 +188,7 @@ final public function paste(ImageInterface $image, PointInterface $start, $alpha throw new RuntimeException('Image paste operation failed'); } } elseif ($alpha > 0) { - if (imagecopymerge(/*dst_im*/$this->resource, /*src_im*/$image->resource, /*dst_x*/$start->getX(), /*dst_y*/$start->getY(), /*src_x*/0, /*src_y*/0, /*src_w*/$size->getWidth(), /*src_h*/$size->getHeight(), /*pct*/$alpha) === false) { + if (imagecopymerge(/*dst_im*/$this->resource, /*src_im*/ $image->resource, /*dst_x*/ $start->getX(), /*dst_y*/ $start->getY(), /*src_x*/ 0, /*src_y*/ 0, /*src_w*/ $size->getWidth(), /*src_h*/ $size->getHeight(), /*pct*/ $alpha) === false) { throw new RuntimeException('Image paste operation failed'); } } @@ -748,7 +748,6 @@ private function finalizeOptions(Format $format, array $options) } if (isset($options['resolution-units']) && isset($options['resolution-x']) && function_exists('imageresolution')) { - $resolution_x = $options['resolution-x']; $resolution_y = isset($options['resolution-y']) ? $options['resolution-y'] : $resolution_x; From 0ee5f39d9e160500f5092c7f97f21cad4c10b168 Mon Sep 17 00:00:00 2001 From: Augusto Cesar Ferreira Date: Tue, 14 Dec 2021 13:37:55 -0300 Subject: [PATCH 3/6] CS Fix --- src/Image/ImageInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Image/ImageInterface.php b/src/Image/ImageInterface.php index abcda0ebb..6dfd299ed 100644 --- a/src/Image/ImageInterface.php +++ b/src/Image/ImageInterface.php @@ -33,7 +33,7 @@ interface ImageInterface extends ManipulatorInterface const RESOLUTION_PIXELSPERCENTIMETER = 'ppc'; /** - * Multiplier for converting resolution from ppc to ppi + * Multiplier for converting resolution from ppc to ppi. * * @var float */ From e7f7b5c5c2ecdff42e12b7c3ce1176259942abef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augusto=20C=C3=A9sar=20Ferreira?= Date: Wed, 15 Dec 2021 08:00:27 -0300 Subject: [PATCH 4/6] Update src/Gd/Image.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apply correct code style. Co-authored-by: Martin Auswöger --- src/Gd/Image.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Gd/Image.php b/src/Gd/Image.php index 948d1e664..b3412cc44 100644 --- a/src/Gd/Image.php +++ b/src/Gd/Image.php @@ -748,15 +748,15 @@ private function finalizeOptions(Format $format, array $options) } if (isset($options['resolution-units']) && isset($options['resolution-x']) && function_exists('imageresolution')) { - $resolution_x = $options['resolution-x']; - $resolution_y = isset($options['resolution-y']) ? $options['resolution-y'] : $resolution_x; + $resolutionX = $options['resolution-x']; + $resolutionY = isset($options['resolution-y']) ? $options['resolution-y'] : $resolutionX; if ($options['resolution-units'] === ImageInterface::RESOLUTION_PIXELSPERCENTIMETER) { - $resolution_x = $resolution_x * ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER; - $resolution_y = $resolution_y * ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER; + $resolutionX *= ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER; + $resolutionY *= ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER; } - imageresolution($this->resource, $resolution_x, $resolution_y); + imageresolution($this->resource, $resolutionX, $resolutionY); } return $result; From aec243bc144a693e0c0690076b3e9e63a04f6fbf Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Tue, 3 Dec 2024 14:43:46 +0100 Subject: [PATCH 5/6] Parse resolution-units --- src/Gd/Image.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Gd/Image.php b/src/Gd/Image.php index b3412cc44..16f0e2732 100644 --- a/src/Gd/Image.php +++ b/src/Gd/Image.php @@ -750,13 +750,16 @@ private function finalizeOptions(Format $format, array $options) if (isset($options['resolution-units']) && isset($options['resolution-x']) && function_exists('imageresolution')) { $resolutionX = $options['resolution-x']; $resolutionY = isset($options['resolution-y']) ? $options['resolution-y'] : $resolutionX; - - if ($options['resolution-units'] === ImageInterface::RESOLUTION_PIXELSPERCENTIMETER) { - $resolutionX *= ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER; - $resolutionY *= ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER; + switch ($options['resolution-units']) { + case ImageInterface::RESOLUTION_PIXELSPERCENTIMETER: + imageresolution($this->resource, $resolutionX * ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER, $resolutionY * ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER); + break; + case ImageInterface::RESOLUTION_PIXELSPERINCH: + imageresolution($this->resource, $resolutionX, $resolutionY); + break; + default: + throw new RuntimeException('Unsupported image unit format'); } - - imageresolution($this->resource, $resolutionX, $resolutionY); } return $result; From ef5c8a6a72a264dd1fdbce4e65d307980c87e56d Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Tue, 3 Dec 2024 15:04:28 +0100 Subject: [PATCH 6/6] Declare that GD supports resolution --- src/Gd/DriverInfo.php | 10 ++++++++-- tests/tests/Gd/ImageTest.php | 6 ++++++ tests/tests/Image/AbstractImageTest.php | 8 ++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Gd/DriverInfo.php b/src/Gd/DriverInfo.php index 358818710..07ab86afc 100644 --- a/src/Gd/DriverInfo.php +++ b/src/Gd/DriverInfo.php @@ -94,9 +94,15 @@ protected function checkFeature($feature) case static::FEATURE_MULTIPLELAYERS: throw new NotSupportedException('GD does not support layer sets'); case static::FEATURE_CUSTOMRESOLUTION: - throw new NotSupportedException('GD does not support setting custom resolutions'); + if (!function_exists('imageresolution')) { + throw new NotSupportedException('GD driver for PHP older than 7.2 does not support setting custom resolutions'); + } + break; case static::FEATURE_EXPORTWITHCUSTOMRESOLUTION: - throw new NotSupportedException('GD driver does not support exporting images with custom resolutions'); + if (!function_exists('imageresolution')) { + throw new NotSupportedException('GD driver for PHP older than 7.2 does not support exporting images with custom resolutions'); + } + break; case static::FEATURE_DRAWFILLEDCHORDSCORRECTLY: throw new NotSupportedException('The GD Drawer can NOT draw correctly filled chords'); case static::FEATURE_DRAWUNFILLEDCIRCLESWITHTICHKESSCORRECTLY: diff --git a/tests/tests/Gd/ImageTest.php b/tests/tests/Gd/ImageTest.php index 823a022d1..623834b66 100644 --- a/tests/tests/Gd/ImageTest.php +++ b/tests/tests/Gd/ImageTest.php @@ -48,6 +48,12 @@ protected function getImagine() */ protected function getImageResolution(ImageInterface $image) { + $resolutions = imageresolution($image->getGdResource()); + + return array( + 'x' => $resolutions[0], + 'y' => $resolutions[1], + ); } /** diff --git a/tests/tests/Image/AbstractImageTest.php b/tests/tests/Image/AbstractImageTest.php index a4613437e..d6a8de7be 100644 --- a/tests/tests/Image/AbstractImageTest.php +++ b/tests/tests/Image/AbstractImageTest.php @@ -952,10 +952,14 @@ public function testResolutionOnSave($source) public function provideVariousSources() { - return array( - array(IMAGINE_TEST_FIXTURESFOLDER . '/example.svg'), + $result = array( array(IMAGINE_TEST_FIXTURESFOLDER . '/100-percent-black.png'), ); + if ($this->getImagine() instanceof \Imagine\Imagick\Imagine) { + $result[] = array(IMAGINE_TEST_FIXTURESFOLDER . '/example.svg'); + } + + return $result; } public function testFillAlphaPrecision()