image = new Imagick(); $this->image->readimageblob($imageData); } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::getHeight() */ public function getHeight() { return $this->image->getimageheight(); } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::getWidth() */ public function getWidth() { return $this->image->getimagewidth(); } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::getAsJpeg() */ public function convertToJpeg() { $this->image->setImageCompression(Imagick::COMPRESSION_JPEG); $this->image->setImageFormat('jpeg'); } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::crop() */ public function crop($x, $y, $width, $height) { $this->image->cropimage($width, $height, $x, $y); } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::resize() */ public function thumbnail($width, $height) { if (($this->getWidth() <= $width) && ($this->getHeight() <= $height)) { // skip if smaller than target size return; } $this->image->thumbnailimage($width, $height, true); } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::getImageData() */ public function getImageData() { return $this->image->getimageblob(); } } /** * Manipulates images using gd library. * * @author Roland Gruber */ class ImageManipulatorGd implements ImageManipulator { /** * Image * * @var resource image */ private $image; /** * GD image type * * @var int image type */ private $type; /** * Constructor. * * @param string $imageData original image as binary string */ public function __construct($imageData) { $this->image = imagecreatefromstring($imageData); $info = getimagesizefromstring($imageData); $this->type = $info[2]; } /** * Destructor */ public function __destruct() { if ($this->image != null) { imagedestroy($this->image); } } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::getHeight() */ public function getHeight() { return imagesy($this->image); } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::getWidth() */ public function getWidth() { return imagesx($this->image); } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::getAsJpeg() */ public function convertToJpeg() { $this->type = IMAGETYPE_JPEG; } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::crop() */ public function crop($x, $y, $width, $height) { $this->image = imagecrop($this->image, array( 'x' => $x, 'y' => $y, 'width' => $width, 'height' => $height )); } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::resize() */ public function thumbnail($width, $height) { if (($this->getWidth() <= $width) && ($this->getHeight() <= $height)) { // skip if smaller than target size return; } $thumbWidth = $this->getWidth(); $thumbHeight = $this->getHeight(); if ($thumbWidth > $width) { $factor = $width / $thumbWidth; $thumbWidth = $thumbWidth * $factor; $thumbHeight = $thumbHeight * $factor; } if ($thumbHeight > $height) { $factor = $height / $thumbHeight; $thumbWidth = $thumbWidth * $factor; $thumbHeight = $thumbHeight * $factor; } $thumbnail = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresampled( $thumbnail, $this->image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $this->getWidth(), $this->getHeight()); $this->image = $thumbnail; } /** * {@inheritDoc} * @see \LAM\ImageUtils\ImageManipulator::getImageData() */ public function getImageData() { ob_start(); if ($this->type == IMAGETYPE_JPEG) { imagejpeg($this->image); } else if ($this->type == IMAGETYPE_PNG) { imagepng($this->image); } $output = ob_get_contents(); ob_clean(); return $output; } } ?>