Skip to content

Commit

Permalink
Enables the GD support for resolution options group for PHP >= 7.2 (#866
Browse files Browse the repository at this point in the history
)

* 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 <martin@auswoeger.com>

* Parse resolution-units

* Declare that GD supports resolution

---------

Co-authored-by: Augusto Cesar Ferreira <gutocf@gmail.com>
Co-authored-by: Augusto César Ferreira <guto@notato.com.br>
Co-authored-by: Martin Auswöger <martin@auswoeger.com>
  • Loading branch information
4 people authored Dec 3, 2024
1 parent 01ccac9 commit 5349e47
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docs/usage/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Install the dependencies using composer.phar and use Imagine :
.. code-block:: none
php composer.phar install
.. code-block:: php
<?php
Expand Down Expand Up @@ -138,7 +138,7 @@ You can optionally specify the fill color for the new image, which defaults to o
$size = new Imagine\Image\Box(400, 300);
$color = $palette->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
Expand Down Expand Up @@ -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.

Expand Down
10 changes: 8 additions & 2 deletions src/Gd/DriverInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 16 additions & 1 deletion src/Gd/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand Down Expand Up @@ -747,6 +747,21 @@ private function finalizeOptions(Format $format, array $options)
break;
}

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;
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');
}
}

return $result;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Image/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/tests/Gd/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ protected function getImagine()
*/
protected function getImageResolution(ImageInterface $image)
{
$resolutions = imageresolution($image->getGdResource());

return array(
'x' => $resolutions[0],
'y' => $resolutions[1],
);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions tests/tests/Image/AbstractImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5349e47

Please sign in to comment.