Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables the GD support for resolution options group for PHP 7 >= 7.2, PHP 8.0. #813

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 13 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 @@ -727,6 +727,18 @@ private function saveOrOutput(Format $format, array $options, $filename = null)
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;

if ($options['resolution-units'] === ImageInterface::RESOLUTION_PIXELSPERCENTIMETER) {
$resolutionX *= ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER;
$resolutionY *= ImageInterface::RESOLUTION_PPC_TO_PPI_MULTIPLIER;
}

imageresolution($this->resource, $resolutionX, $resolutionY);
}

ErrorHandling::throwingRuntimeException(E_WARNING | E_NOTICE, function () use ($saveFunction, $args) {
if (call_user_func_array($saveFunction, $args) === false) {
throw new RuntimeException('Save operation failed');
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