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

Rotate/flip layers #3

Open
wants to merge 3 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
12 changes: 12 additions & 0 deletions src/Engine/PhpGd/Extension/Core/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -103,6 +111,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);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Engine/PhpGd/Extension/Gif/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -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'),
Expand All @@ -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);
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/Engine/PhpGd/Helper/ResourceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,35 @@ 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;
}

/**
* 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
Expand Down
24 changes: 24 additions & 0 deletions src/Layer/BackgroundLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,28 @@ 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
* @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;
}
}
24 changes: 24 additions & 0 deletions src/Layer/ImageLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -69,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;
}
}