1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 17:41:58 +02:00

Retain image resolution in GD driver modifiers

This commit is contained in:
Oliver Vogel
2023-12-16 13:58:55 +01:00
parent e4004c6402
commit 516c6aa08f
8 changed files with 52 additions and 14 deletions

View File

@@ -5,7 +5,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Drivers\DriverSpecializedModifier;
use Intervention\Image\Drivers\Gd\SpecializedModifier;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -20,7 +20,7 @@ use Intervention\Image\Modifiers\FillModifier;
* @property mixed $background
* @property string $position
*/
class ContainModifier extends DriverSpecializedModifier
class ContainModifier extends SpecializedModifier
{
public function apply(ImageInterface $image): ImageInterface
{
@@ -49,6 +49,9 @@ class ContainModifier extends DriverSpecializedModifier
new FillModifier($background)
)->core()->native();
// retain resolution
$this->copyResolution($frame->native(), $modified);
// make image area transparent to keep transparency
// even if background-color is set
$transparent = imagecolorallocatealpha(

View File

@@ -2,7 +2,7 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\DriverSpecializedModifier;
use Intervention\Image\Drivers\Gd\SpecializedModifier;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
@@ -11,7 +11,7 @@ use Intervention\Image\Interfaces\SizeInterface;
* @method SizeInterface getResizeSize(ImageInterface $image)
* @method SizeInterface getCropSize(ImageInterface $image)
*/
class CoverModifier extends DriverSpecializedModifier
class CoverModifier extends SpecializedModifier
{
public function apply(ImageInterface $image): ImageInterface
{
@@ -36,6 +36,9 @@ class CoverModifier extends DriverSpecializedModifier
// get original image
$original = $frame->native();
// retain resolution
$this->copyResolution($original, $modified);
// preserve transparency
$transIndex = imagecolortransparent($original);

View File

@@ -2,7 +2,7 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\DriverSpecializedModifier;
use Intervention\Image\Drivers\Gd\SpecializedModifier;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
@@ -12,7 +12,7 @@ use Intervention\Image\Interfaces\SizeInterface;
* @property int $offset_x
* @property int $offset_y
*/
class CropModifier extends DriverSpecializedModifier
class CropModifier extends SpecializedModifier
{
public function apply(ImageInterface $image): ImageInterface
{
@@ -36,6 +36,9 @@ class CropModifier extends DriverSpecializedModifier
// get original image
$original = $frame->native();
// retain resolution
$this->copyResolution($original, $modified);
// preserve transparency
$transIndex = imagecolortransparent($original);

View File

@@ -2,7 +2,7 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\DriverSpecializedModifier;
use Intervention\Image\Drivers\Gd\SpecializedModifier;
use Intervention\Image\Exceptions\InputException;
use Intervention\Image\Interfaces\ImageInterface;
@@ -10,7 +10,7 @@ use Intervention\Image\Interfaces\ImageInterface;
* @property int $limit
* @property mixed $background
*/
class QuantizeColorsModifier extends DriverSpecializedModifier
class QuantizeColorsModifier extends SpecializedModifier
{
public function apply(ImageInterface $image): ImageInterface
{
@@ -35,6 +35,9 @@ class QuantizeColorsModifier extends DriverSpecializedModifier
// create new image for color quantization
$reduced = imagecreatetruecolor($width, $height);
// retain resolution
$this->copyResolution($frame->native(), $reduced);
// fill with background
imagefill($reduced, 0, 0, $background);

View File

@@ -5,7 +5,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Drivers\DriverSpecializedModifier;
use Intervention\Image\Drivers\Gd\SpecializedModifier;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -16,7 +16,7 @@ use Intervention\Image\Modifiers\FillModifier;
* @method SizeInterface cropSize(ImageInterface $image)
* @property mixed $background
*/
class ResizeCanvasModifier extends DriverSpecializedModifier
class ResizeCanvasModifier extends SpecializedModifier
{
public function apply(ImageInterface $image): ImageInterface
{
@@ -43,6 +43,9 @@ class ResizeCanvasModifier extends DriverSpecializedModifier
new FillModifier($background)
)->core()->native();
// retain resolution
$this->copyResolution($frame->native(), $modified);
// make image area transparent to keep transparency
// even if background-color is set
$transparent = imagecolorallocatealpha(

View File

@@ -2,7 +2,7 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\DriverSpecializedModifier;
use Intervention\Image\Drivers\Gd\SpecializedModifier;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
@@ -11,7 +11,7 @@ use Intervention\Image\Interfaces\SizeInterface;
* @property null|int $width
* @property null|int $height
*/
class ResizeModifier extends DriverSpecializedModifier
class ResizeModifier extends SpecializedModifier
{
public function apply(ImageInterface $image): ImageInterface
{
@@ -34,6 +34,9 @@ class ResizeModifier extends DriverSpecializedModifier
// get current GDImage
$current = $frame->native();
// retain resolution
$this->copyResolution($current, $modified);
// preserve transparency
$transIndex = imagecolortransparent($current);

View File

@@ -5,7 +5,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Drivers\DriverSpecializedModifier;
use Intervention\Image\Drivers\Gd\SpecializedModifier;
use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FrameInterface;
@@ -16,7 +16,7 @@ use Intervention\Image\Modifiers\FillModifier;
* @method mixed rotationAngle()
* @property mixed $background
*/
class RotateModifier extends DriverSpecializedModifier
class RotateModifier extends SpecializedModifier
{
public function apply(ImageInterface $image): ImageInterface
{
@@ -81,6 +81,9 @@ class RotateModifier extends DriverSpecializedModifier
->core()
->native();
// retain resolution
$this->copyResolution($frame->native(), $modified);
// draw the cutout on new gd image to have a transparent
// background where the rotated image will be placed
imagealphablending($modified, false);

View File

@@ -0,0 +1,17 @@
<?php
namespace Intervention\Image\Drivers\Gd;
use GdImage;
use Intervention\Image\Drivers\DriverSpecializedModifier;
abstract class SpecializedModifier extends DriverSpecializedModifier
{
protected function copyResolution(GdImage $source, GdImage $target): void
{
$resolution = imageresolution($source);
if (is_array($resolution) && array_key_exists(0, $resolution) && array_key_exists(1, $resolution)) {
imageresolution($target, $resolution[0], $resolution[1]);
}
}
}