Skip to content

Commit 5349e47

Browse files
mlocatigutocfausi
authored
Enables the GD support for resolution options group for PHP >= 7.2 (#866)
* Adds support for image resolution for GD * Fix cs * CS Fix * Update src/Gd/Image.php Apply correct code style. Co-authored-by: Martin Auswöger <[email protected]> * Parse resolution-units * Declare that GD supports resolution --------- Co-authored-by: Augusto Cesar Ferreira <[email protected]> Co-authored-by: Augusto César Ferreira <[email protected]> Co-authored-by: Martin Auswöger <[email protected]>
1 parent 01ccac9 commit 5349e47

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

docs/usage/introduction.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Install the dependencies using composer.phar and use Imagine :
2929
.. code-block:: none
3030
3131
php composer.phar install
32-
32+
3333
.. code-block:: php
3434
3535
<?php
@@ -138,7 +138,7 @@ You can optionally specify the fill color for the new image, which defaults to o
138138
$size = new Imagine\Image\Box(400, 300);
139139
$color = $palette->color('#000', 0);
140140
$image = $imagine->create($size, $color);
141-
141+
142142
To use a solid background color, for example orange, provide an alpha of 100.
143143

144144
.. code-block:: php
@@ -173,7 +173,7 @@ Three options groups are currently supported : quality, resolution and flatten.
173173
Default values are 75 for Jpeg quality, 7 for Png compression level, 75 for webp quality and 72 dpi for x/y-resolution.
174174

175175
.. NOTE::
176-
GD does not support resolution options group
176+
GD does support resolution options group only with PHP 7 >= 7.2, PHP 8
177177

178178
The following example demonstrates the basic quality settings.
179179

src/Gd/DriverInfo.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,15 @@ protected function checkFeature($feature)
9494
case static::FEATURE_MULTIPLELAYERS:
9595
throw new NotSupportedException('GD does not support layer sets');
9696
case static::FEATURE_CUSTOMRESOLUTION:
97-
throw new NotSupportedException('GD does not support setting custom resolutions');
97+
if (!function_exists('imageresolution')) {
98+
throw new NotSupportedException('GD driver for PHP older than 7.2 does not support setting custom resolutions');
99+
}
100+
break;
98101
case static::FEATURE_EXPORTWITHCUSTOMRESOLUTION:
99-
throw new NotSupportedException('GD driver does not support exporting images with custom resolutions');
102+
if (!function_exists('imageresolution')) {
103+
throw new NotSupportedException('GD driver for PHP older than 7.2 does not support exporting images with custom resolutions');
104+
}
105+
break;
100106
case static::FEATURE_DRAWFILLEDCHORDSCORRECTLY:
101107
throw new NotSupportedException('The GD Drawer can NOT draw correctly filled chords');
102108
case static::FEATURE_DRAWUNFILLEDCIRCLESWITHTICHKESSCORRECTLY:

src/Gd/Image.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ final public function paste(ImageInterface $image, PointInterface $start, $alpha
188188
throw new RuntimeException('Image paste operation failed');
189189
}
190190
} elseif ($alpha > 0) {
191-
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) {
191+
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) {
192192
throw new RuntimeException('Image paste operation failed');
193193
}
194194
}
@@ -747,6 +747,21 @@ private function finalizeOptions(Format $format, array $options)
747747
break;
748748
}
749749

750+
if (isset($options['resolution-units']) && isset($options['resolution-x']) && function_exists('imageresolution')) {
751+
$resolutionX = $options['resolution-x'];
752+
$resolutionY = isset($options['resolution-y']) ? $options['resolution-y'] : $resolutionX;
753+
switch ($options['resolution-units']) {
754+
case ImageInterface::RESOLUTION_PIXELSPERCENTIMETER:
755+
imageresolution($this->resource, $resolutionX * ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER, $resolutionY * ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER);
756+
break;
757+
case ImageInterface::RESOLUTION_PIXELSPERINCH:
758+
imageresolution($this->resource, $resolutionX, $resolutionY);
759+
break;
760+
default:
761+
throw new RuntimeException('Unsupported image unit format');
762+
}
763+
}
764+
750765
return $result;
751766
}
752767

src/Image/ImageInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ interface ImageInterface extends ManipulatorInterface
3232
*/
3333
const RESOLUTION_PIXELSPERCENTIMETER = 'ppc';
3434

35+
/**
36+
* Multiplier for converting resolution from ppc to ppi.
37+
*
38+
* @var float
39+
*/
40+
const RESOLUTION_PPC_TO_PPI_MULTIPLIER = 2.54;
41+
3542
/**
3643
* Image interlacing: none.
3744
*

tests/tests/Gd/ImageTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ protected function getImagine()
4848
*/
4949
protected function getImageResolution(ImageInterface $image)
5050
{
51+
$resolutions = imageresolution($image->getGdResource());
52+
53+
return array(
54+
'x' => $resolutions[0],
55+
'y' => $resolutions[1],
56+
);
5157
}
5258

5359
/**

tests/tests/Image/AbstractImageTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,10 +952,14 @@ public function testResolutionOnSave($source)
952952

953953
public function provideVariousSources()
954954
{
955-
return array(
956-
array(IMAGINE_TEST_FIXTURESFOLDER . '/example.svg'),
955+
$result = array(
957956
array(IMAGINE_TEST_FIXTURESFOLDER . '/100-percent-black.png'),
958957
);
958+
if ($this->getImagine() instanceof \Imagine\Imagick\Imagine) {
959+
$result[] = array(IMAGINE_TEST_FIXTURESFOLDER . '/example.svg');
960+
}
961+
962+
return $result;
959963
}
960964

961965
public function testFillAlphaPrecision()

0 commit comments

Comments
 (0)