Skip to content

Instantly share code, notes, and snippets.

@radmiraal
Created June 2, 2015 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radmiraal/7a891aaa826d13b0d868 to your computer and use it in GitHub Desktop.
Save radmiraal/7a891aaa826d13b0d868 to your computer and use it in GitHub Desktop.
<?php
namespace My\Package\Media\Adjustments;
use TYPO3\Flow\Annotations as Flow;
use Imagine\Image\ImageInterface;
use Imagine\Image\Point;
use TYPO3\Media\Domain\Model\Adjustment\CropImageAdjustment;
use TYPO3\Media\Domain\Model\Adjustment\ImageAdjustmentInterface;
use TYPO3\Media\Domain\Model\Adjustment\ResizeImageAdjustment;
use TYPO3\Media\Domain\Model\ImageVariant;
class HexagonImageAdjustment implements ImageAdjustmentInterface {
/**
* @Flow\Inject(lazy=FALSE)
* @var \Imagine\Image\ImagineInterface
*/
protected $imagineService;
/**
* @var integer
*/
protected $maximumHeight = 0;
/**
* Applies this adjustment to the given Imagine Image object
*
* @param ImageInterface $image
* @return ImageInterface
*/
public function applyToImage(ImageInterface $image) {
$resizeImageAdjustment = new ResizeImageAdjustment();
$resizeImageAdjustment->setMaximumHeight($this->maximumHeight);
$resizeImageAdjustment->setMinimumHeight($this->maximumHeight);
$resizeImageAdjustment->setMinimumHeight($this->maximumHeight);
$image = $resizeImageAdjustment->applyToImage($image);
$cropImageAdjustment = new CropImageAdjustment();
$cropImageAdjustment->setHeight($this->maximumHeight);
$cropImageAdjustment->setWidth(intval($this->maximumHeight * 0.866025));
$cropImageAdjustment->setX(intval(($image->getSize()->getWidth() - ($this->maximumHeight * 0.866025)) / 2));
$image = $cropImageAdjustment->applyToImage($image);
$width = $image->getSize()->getWidth() - 1;
$height = $image->getSize()->getHeight() - 1;
$coordinates = [
new Point($width / 2, 1),
new Point($width, $height * .25),
new Point($width, $height * .75),
new Point($width / 2, $height),
new Point(0, $height * .75),
new Point(0, $height * .25),
new Point($width / 2, 1)
];
$mask = $this->imagineService->create(
$image->getSize(),
$image->palette()->color('fff')
);
$mask->draw()->polygon($coordinates, $image->palette()->color('000', 100), TRUE, 0);
$image->applyMask($mask);
return $image;
}
/**
* Sets the image variant this adjustment belongs to
*
* @param ImageVariant $imageVariant
* @return void
*/
public function setImageVariant(ImageVariant $imageVariant) {
}
/**
* Check if this Adjustment can or should be applied to its ImageVariant.
*
* @param ImageInterface $image
* @return boolean
*/
public function canBeApplied(ImageInterface $image) {
return TRUE;
}
/**
* @param integer $maximumHeight
*/
public function setMaximumHeight($maximumHeight) {
$this->maximumHeight = $maximumHeight;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment