From 549e410d95666f6c21f9c11597386cf4abf25974 Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Fri, 6 Feb 2015 00:59:07 +0100 Subject: [PATCH 1/3] Add possibility to flip layer --- src/Engine/PhpGd/Extension/Core/ImageFactory.php | 4 ++++ src/Engine/PhpGd/Helper/ResourceHelper.php | 11 +++++++++++ src/Layer/BackgroundLayer.php | 10 ++++++++++ src/Layer/ImageLayer.php | 10 ++++++++++ 4 files changed, 35 insertions(+) diff --git a/src/Engine/PhpGd/Extension/Core/ImageFactory.php b/src/Engine/PhpGd/Extension/Core/ImageFactory.php index 4274612..0ad0be6 100644 --- a/src/Engine/PhpGd/Extension/Core/ImageFactory.php +++ b/src/Engine/PhpGd/Extension/Core/ImageFactory.php @@ -103,6 +103,10 @@ protected function initImageAwareLayerResource(ImageAwareLayerInterface $layer) true ); } + + if ($layer->has('image.flip')) { + $resource = $this->rh->getFlippedGdResource($resource, $layer->get('image.flip')); + } $layer->set('final.resource', $resource); } diff --git a/src/Engine/PhpGd/Helper/ResourceHelper.php b/src/Engine/PhpGd/Helper/ResourceHelper.php index f8034b5..1812b60 100644 --- a/src/Engine/PhpGd/Helper/ResourceHelper.php +++ b/src/Engine/PhpGd/Helper/ResourceHelper.php @@ -255,6 +255,17 @@ public function getResizedGdResource( return $dstResource; } + + /** + * Flip the image + * @param resource $srcResource the image resource + * @param int $mode flip mode + * @return resource the flipped resource + */ + public function getFlippedGdResource($srcResource, $mode) { + imageflip($srcResource, $mode); + return $srcResource; + } /** * @param resource $dstResource diff --git a/src/Layer/BackgroundLayer.php b/src/Layer/BackgroundLayer.php index 260d980..4447c52 100644 --- a/src/Layer/BackgroundLayer.php +++ b/src/Layer/BackgroundLayer.php @@ -55,4 +55,14 @@ public function resize($width, $height, $option = ImageAwareLayerInterface::RESI return $this; } + + /** + * @inheritDoc + * @param int $mode one of the IMG_FLIP_ constants + */ + public function flip($mode) + { + $this->add(['image.flip' => $mode]); + return $this; + } } diff --git a/src/Layer/ImageLayer.php b/src/Layer/ImageLayer.php index 038e499..c1010cb 100644 --- a/src/Layer/ImageLayer.php +++ b/src/Layer/ImageLayer.php @@ -55,6 +55,16 @@ public function resize($width, $height, $option = ImageAwareLayerInterface::RESI return $this; } + + /** + * @inheritDoc + * @param int $mode one of the IMG_FLIP_ constants + */ + public function flip($mode) + { + $this->add(['image.flip' => $mode]); + return $this; + } /** * @inheritDoc From 254cee34a848e3b3012850c919629f086c0a6ddd Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Fri, 6 Feb 2015 00:59:17 +0100 Subject: [PATCH 2/3] API to rotate layers --- .../PhpGd/Extension/Core/ImageFactory.php | 8 ++++++++ src/Engine/PhpGd/Helper/ResourceHelper.php | 18 ++++++++++++++++++ src/Layer/BackgroundLayer.php | 14 ++++++++++++++ src/Layer/ImageLayer.php | 14 ++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/src/Engine/PhpGd/Extension/Core/ImageFactory.php b/src/Engine/PhpGd/Extension/Core/ImageFactory.php index 0ad0be6..cb4dd1d 100644 --- a/src/Engine/PhpGd/Extension/Core/ImageFactory.php +++ b/src/Engine/PhpGd/Extension/Core/ImageFactory.php @@ -93,6 +93,14 @@ protected function initImageAwareLayerResource(ImageAwareLayerInterface $layer) $contents = $layer->get('image.contents'); $resource = $this->rh->getGdResourceFromContents($format, $contents, true); } + + if ($layer->has('image.rotate.angle')) { + $resource = $this->rh->getRotatedGdResource( + $resource, + $layer->get('image.rotate.angle'), + $layer->get('image.rotate.bgcolor') + ); + } if ($layer->has('image.resize.width')) { $resource = $this->rh->getResizedGdResource( diff --git a/src/Engine/PhpGd/Helper/ResourceHelper.php b/src/Engine/PhpGd/Helper/ResourceHelper.php index 1812b60..e1f573a 100644 --- a/src/Engine/PhpGd/Helper/ResourceHelper.php +++ b/src/Engine/PhpGd/Helper/ResourceHelper.php @@ -267,6 +267,24 @@ public function getFlippedGdResource($srcResource, $mode) { return $srcResource; } + /** + * Rotate the image with the given degress + * @param resource $srcResource the image resource + * @param float $angle angle of rotation in degress, counter-clockwise + * @param null,array $bgColor color of the uncovered area + * @return resource the rotated + */ + public function getRotatedGdResource($srcResource, $angle, $bgColor = null) { + if ($bgColor) { + $bgColor = imagecolorallocate($resource, $bgColor[0], $bgColor[1], $bgColor[2]); + } else { + $bgColor = 0; + } + $dstResource = imagerotate($srcResource, $angle, $bgColor); + imagedestroy($srcResource); + return $dstResource; + } + /** * @param resource $dstResource * @param resource $srcResource diff --git a/src/Layer/BackgroundLayer.php b/src/Layer/BackgroundLayer.php index 4447c52..b617aa5 100644 --- a/src/Layer/BackgroundLayer.php +++ b/src/Layer/BackgroundLayer.php @@ -65,4 +65,18 @@ public function flip($mode) $this->add(['image.flip' => $mode]); return $this; } + + /** + * @inheritDoc + * @param float $angle angle of rotation + * @param null|array color of uncovered pixels + */ + public function rotate($angle, $bgColor = null) + { + $this->add(['image.rotate.angle' => $angle]); + if ($bgColor) { + $this->add(['image.rotate.bgcolor' => $bgColor]); + } + return $this; + } } diff --git a/src/Layer/ImageLayer.php b/src/Layer/ImageLayer.php index c1010cb..902ecfc 100644 --- a/src/Layer/ImageLayer.php +++ b/src/Layer/ImageLayer.php @@ -79,4 +79,18 @@ public function move($x, $y, $gravity = RegularLayerInterface::MOVE_TOP_LEFT) return $this; } + + /** + * @inheritDoc + * @param int $angle angle of rotation + * @param null|array color of uncovered pixels + */ + public function rotate($angle, $bgColor = null) + { + $this->add(['image.rotate.angle' => $angle]); + if ($bgColor) { + $this->add(['image.rotate.bgcolor' => $bgColor]); + } + return $this; + } } From 9853d3cb9ce8c97bf436495a162f5e6eaf5d64c5 Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Sat, 7 Feb 2015 00:37:03 +0100 Subject: [PATCH 3/3] Support for rotating and flipping gifs --- .../PhpGd/Extension/Gif/ImageFactory.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Engine/PhpGd/Extension/Gif/ImageFactory.php b/src/Engine/PhpGd/Extension/Gif/ImageFactory.php index fe9eb80..65bed7e 100644 --- a/src/Engine/PhpGd/Extension/Gif/ImageFactory.php +++ b/src/Engine/PhpGd/Extension/Gif/ImageFactory.php @@ -209,6 +209,14 @@ protected function initImageAwareLayerResource(ImageAwareLayerInterface $layer) $resource = $this->rh->getGdResourceFromContents($format, $contents, true); } + if ($layer->has('image.rotate.angle')) { + $resource = $this->rh->getRotatedGdResource( + $resource, + $layer->get('image.rotate.angle'), + $layer->get('image.rotate.bgcolor') + ); + } + if ($layer->has('image.resize.width')) { $resource = $this->rh->getResizedGdResource( $resource, @@ -219,6 +227,10 @@ protected function initImageAwareLayerResource(ImageAwareLayerInterface $layer) ); } + if ($layer->has('image.flip')) { + $resource = $this->rh->getFlippedGdResource($resource, $layer->get('image.flip')); + } + $layer->set('final.resource', $resource); } @@ -308,6 +320,15 @@ protected function coalesceFrameResource(BackgroundLayerInterface $layer) */ protected function renderFrameResource(BackgroundLayerInterface $layer) { + if ($layer->has('image.rotate.angle')) { + $resource = $this->rh->getRotatedGdResource( + $layer->get('gif.frame_resource'), + $layer->get('image.rotate.angle'), + $layer->get('image.rotate.bgcolor') + ); + $layer->set('gif.frame_resource', $resource); + } + if ($layer->has('image.resize.width')) { $resource = $this->rh->getResizedGdResource( $layer->get('gif.frame_resource'), @@ -316,6 +337,12 @@ protected function renderFrameResource(BackgroundLayerInterface $layer) $layer->get('image.resize.option'), $layer->get('gif.quality') ); + + $layer->set('gif.frame_resource', $resource); + } + + if ($layer->has('image.flip')) { + $resource = $this->rh->getFlippedGdResource($layer->get('gif.frame_resource'), $layer->get('image.flip')); $layer->set('gif.frame_resource', $resource); } }