diff --git a/Adapter/GD.php b/Adapter/GD.php index b63c734..65ef014 100644 --- a/Adapter/GD.php +++ b/Adapter/GD.php @@ -380,7 +380,7 @@ public function flip($flipVertical, $flipHorizontal) { } else if (!$flipVertical && $flipHorizontal) { $flipMode = \IMG_FLIP_HORIZONTAL; } - + imageflip($this->resource, $flipMode); } else { $width = $this->width(); @@ -410,7 +410,7 @@ public function flip($flipVertical, $flipHorizontal) { $this->resource = $imgdest; } } - + return $this; } diff --git a/Adapter/Imagick.php b/Adapter/Imagick.php index 9da3969..7b8ac86 100644 --- a/Adapter/Imagick.php +++ b/Adapter/Imagick.php @@ -4,386 +4,458 @@ use Gregwar\Image\Image; -class Imagick extends Common{ - public function __construct(){ - throw new \Exception('Imagick is not supported right now'); +class Imagick extends Common +{ + /** + * @var \Imagick + */ + protected $resource; + + /** + * @inheritdoc + */ + public function __construct() + { + parent::__construct(); + + if (!(extension_loaded('imagick') && class_exists('Imagick'))) { + throw new \RuntimeException('You need to install Imagick PHP Extension to use this library'); + } } - /** - * Gets the name of the adapter - * - * @return string - */ - public function getName(){ + public function __destruct() + { + if ($this->resource !== null) { + $this->resource->clear(); + $this->resource->destroy(); + } + } + + /** + * @inheritdoc + */ + public function getName() + { return 'ImageMagick'; } - /** - * Image width - * - * @return int - */ - public function width(){ - // TODO: Implement width() method. - } + /** + * @inheritdoc + */ + public function width() + { + if (null === $this->resource) { + $this->init(); + } - /** - * Image height - * - * @return int - */ - public function height(){ - // TODO: Implement height() method. - } + return $this->resource->getImageWidth(); + } - /** - * Save the image as a gif - * - * @return $this - */ - public function saveGif($file){ - // TODO: Implement saveGif() method. - } + /** + * @inheritdoc + */ + public function height() + { + if (null === $this->resource) { + $this->init(); + } - /** - * Save the image as a png - * - * @return $this - */ - public function savePng($file){ - // TODO: Implement savePng() method. - } + return $this->resource->getImageHeight(); + } - /** - * Save the image as a jpeg - * - * @return $this - */ - public function saveJpeg($file, $quality){ - // TODO: Implement saveJpeg() method. - } + /** + * @inheritdoc + */ + public function saveGif($file) + { + $this->resource->setImageFormat('gif'); + $this->resource->writeImage($file); - /** - * Crops the image - * - * @param int $x the top-left x position of the crop box - * @param int $y the top-left y position of the crop box - * @param int $width the width of the crop box - * @param int $height the height of the crop box - * - * @return $this - */ - public function crop($x, $y, $width, $height){ - // TODO: Implement crop() method. - } + return $this; + } - /** - * Fills the image background to $bg if the image is transparent - * - * @param int $background background color - * - * @return $this - */ - public function fillBackground($background = 0xffffff){ - // TODO: Implement fillBackground() method. - } + /** + * @inheritdoc + */ + public function savePng($file) + { + $this->resource->setImageFormat('png'); + $this->resource->writeImage($file); - /** - * Negates the image - * - * @return $this - */ - public function negate(){ - // TODO: Implement negate() method. - } + return $this; + } - /** - * Changes the brightness of the image - * - * @param int $brightness the brightness - * - * @return $this - */ - public function brightness($brightness){ - // TODO: Implement brightness() method. - } + /** + * @inheritdoc + */ + public function saveJpeg($file, $quality) + { + $this->resource->setImageFormat('jpeg'); + $this->resource->setCompressionQuality($quality); + $this->resource->writeImage($file); - /** - * Contrasts the image - * - * @param int $contrast the contrast [-100, 100] - * - * @return $this - */ - public function contrast($contrast){ - // TODO: Implement contrast() method. - } + return $this; + } - /** - * Apply a grayscale level effect on the image - * - * @return $this - */ - public function grayscale(){ - // TODO: Implement grayscale() method. - } + /** + * @inheritdoc + */ + public function crop($x, $y, $width, $height) + { + $this->resource->cropImage($width, $height, $x, $y); - /** - * Emboss the image - * - * @return $this - */ - public function emboss(){ - // TODO: Implement emboss() method. - } + return $this; + } - /** - * Smooth the image - * - * @param int $p value between [-10,10] - * - * @return $this - */ - public function smooth($p){ - // TODO: Implement smooth() method. - } + /** + * @inheritdoc + */ + public function fillBackground($background = 0xffffff) + { + // TODO: Check if int works or if hexadecimal notation is needed. + $this->resource->setImageBackgroundColor($background); - /** - * Sharps the image - * - * @return $this - */ - public function sharp(){ - // TODO: Implement sharp() method. - } + return $this; + } - /** - * Edges the image - * - * @return $this - */ - public function edge(){ - // TODO: Implement edge() method. - } + /** + * @inheritdoc + */ + public function negate() + { + // TODO: Check if $gray should be false or true according to GD method + $this->resource->negateImage(true); - /** - * Colorize the image - * - * @param int $red value in range [-255, 255] - * @param int $green value in range [-255, 255] - * @param int $blue value in range [-255, 255] - * - * @return $this - */ - public function colorize($red, $green, $blue){ - // TODO: Implement colorize() method. - } + return $this; + } - /** - * apply sepia to the image - * - * @return $this - */ - public function sepia(){ - // TODO: Implement sepia() method. - } + /** + * @inheritdoc + */ + public function brightness($brightness) + { + // TODO: Test according to this note: http://php.net/manual/en/imagick.modulateimage.php#87330 + $this->resource->modulateImage($brightness, 100, 100); - /** - * Merge with another image - * - * @param Image $other - * @param int $x - * @param int $y - * @param int $width - * @param int $height - * - * @return $this - */ - public function merge(Image $other, $x = 0, $y = 0, $width = null, $height = null){ - // TODO: Implement merge() method. - } + return $this; + } - /** - * Rotate the image - * - * @param float $angle - * @param int $background - * - * @return $this - */ - public function rotate($angle, $background = 0xffffff){ - // TODO: Implement rotate() method. - } + /** + * @inheritdoc + */ + public function contrast($contrast) + { + // TODO: Implement contrast() method. + // Note: Imagick want a boolean. How to convert it? + } + + /** + * @inheritdoc + */ + public function grayscale() + { + // TODO: Don't know if it is the good solution. Can we add colors after? Test it. + $this->resource->setImageColorspace(\Imagick::COLORSPACE_GRAY); - /** - * Fills the image - * - * @param int $color - * @param int $x - * @param int $y - * - * @return $this - */ - public function fill($color = 0xffffff, $x = 0, $y = 0){ - // TODO: Implement fill() method. - } + return $this; + } - /** - * write text to the image - * - * @param string $font - * @param string $text - * @param int $x - * @param int $y - * @param int $size - * @param int $angle - * @param int $color - * @param string $align - */ - public function write($font, $text, $x = 0, $y = 0, $size = 12, $angle = 0, $color = 0x000000, $align = 'left'){ - // TODO: Implement write() method. - } + /** + * @inheritdoc + */ + public function emboss() + { + // TODO: Manage parameters? http://php.net/manual/en/imagick.embossImage.php + $this->resource->embossImage(0, 1); - /** - * Draws a rectangle - * - * @param int $x1 - * @param int $y1 - * @param int $x2 - * @param int $y2 - * @param int $color - * @param bool $filled - * - * @return $this - */ - public function rectangle($x1, $y1, $x2, $y2, $color, $filled = false){ - // TODO: Implement rectangle() method. - } + return $this; + } - /** - * Draws a rounded rectangle - * - * @param int $x1 - * @param int $y1 - * @param int $x2 - * @param int $y2 - * @param int $radius - * @param int $color - * @param bool $filled - * - * @return $this - */ - public function roundedRectangle($x1, $y1, $x2, $y2, $radius, $color, $filled = false){ - // TODO: Implement roundedRectangle() method. - } + /** + * @inheritdoc + */ + public function smooth($p) + { + $this->resource->reduceNoiseImage($p); - /** - * Draws a line - * - * @param int $x1 - * @param int $y1 - * @param int $x2 - * @param int $y2 - * @param int $color - * - * @return $this - */ - public function line($x1, $y1, $x2, $y2, $color = 0x000000){ - // TODO: Implement line() method. - } + return $this; + } - /** - * Draws an ellipse - * - * @param int $cx - * @param int $cy - * @param int $width - * @param int $height - * @param int $color - * @param bool $filled - * - * @return $this - */ - public function ellipse($cx, $cy, $width, $height, $color = 0x000000, $filled = false){ - // TODO: Implement ellipse() method. - } + /** + * @inheritdoc + */ + public function sharp() + { + // TODO: Manage parameters? http://php.net/manual/en/imagick.sharpenimage.php + $this->resource->sharpenImage(0, 1); - /** - * Draws a circle - * - * @param int $cx - * @param int $cy - * @param int $r - * @param int $color - * @param bool $filled - * - * @return $this - */ - public function circle($cx, $cy, $r, $color = 0x000000, $filled = false){ - // TODO: Implement circle() method. - } + return $this; + } - /** - * Draws a polygon - * - * @param array $points - * @param int $color - * @param bool $filled - * - * @return $this - */ - public function polygon(array $points, $color, $filled = false){ - // TODO: Implement polygon() method. - } + /** + * @inheritdoc + */ + public function edge() + { + // TODO: Manage parameters? http://php.net/manual/en/imagick.edgeimage.php + $this->resource->edgeImage(0); - /** - * @inheritdoc + return $this; + } + + /** + * @inheritdoc */ - public function flip($flipVertical, $flipHorizontal) { - // TODO: Implement flip method - } + public function colorize($red, $green, $blue) + { + $color = sprintf('rgb(%d, %d, %d)', $red, $green, $blue); + // TODO: Manage opacity? http://php.net/manual/en/imagick.colorizeimage.php + $this->resource->colorizeImage($color, 1.0); - /** - * Opens the image - */ - protected function openGif($file){ - // TODO: Implement openGif() method. - } + return $this; + } - protected function openJpeg($file){ - // TODO: Implement openJpeg() method. - } + /** + * @inheritdoc + */ + public function sepia() + { + // TODO: Manage threshold parameter? http://php.net/manual/en/imagick.sepiatoneimage.php + $this->resource->sepiaToneImage(80); - protected function openPng($file){ - // TODO: Implement openPng() method. - } + return $this; + } - /** - * Creates an image - */ - protected function createImage($width, $height){ - // TODO: Implement createImage() method. - } + /** + * @inheritdoc + */ + public function merge(Image $other, $x = 0, $y = 0, $width = null, $height = null) + { + // TODO: implement it on Common class. + $other = clone $other; + $other->init(); + $other->applyOperations(); + + if (null == $width) { + $width = $other->width(); + } + if (null == $height) { + $height = $other->height(); + } + // ENDTOTO + + // TODO: Implement merge() method. + } + + /** + * Rotate the image + * + * @param float $angle + * @param int $background + * + * @return $this + */ + public function rotate($angle, $background = 0xffffff) + { + // TODO: Implement rotate() method. + } + + /** + * Fills the image + * + * @param int $color + * @param int $x + * @param int $y + * + * @return $this + */ + public function fill($color = 0xffffff, $x = 0, $y = 0) + { + // TODO: Implement fill() method. + } + + /** + * write text to the image + * + * @param string $font + * @param string $text + * @param int $x + * @param int $y + * @param int $size + * @param int $angle + * @param int $color + * @param string $align + */ + public function write($font, $text, $x = 0, $y = 0, $size = 12, $angle = 0, $color = 0x000000, $align = 'left') + { + // TODO: Implement write() method. + } + + /** + * Draws a rectangle + * + * @param int $x1 + * @param int $y1 + * @param int $x2 + * @param int $y2 + * @param int $color + * @param bool $filled + * + * @return $this + */ + public function rectangle($x1, $y1, $x2, $y2, $color, $filled = false) + { + // TODO: Implement rectangle() method. + } + + /** + * Draws a rounded rectangle + * + * @param int $x1 + * @param int $y1 + * @param int $x2 + * @param int $y2 + * @param int $radius + * @param int $color + * @param bool $filled + * + * @return $this + */ + public function roundedRectangle($x1, $y1, $x2, $y2, $radius, $color, $filled = false) + { + // TODO: Implement roundedRectangle() method. + } + + /** + * Draws a line + * + * @param int $x1 + * @param int $y1 + * @param int $x2 + * @param int $y2 + * @param int $color + * + * @return $this + */ + public function line($x1, $y1, $x2, $y2, $color = 0x000000) + { + // TODO: Implement line() method. + } + + /** + * Draws an ellipse + * + * @param int $cx + * @param int $cy + * @param int $width + * @param int $height + * @param int $color + * @param bool $filled + * + * @return $this + */ + public function ellipse($cx, $cy, $width, $height, $color = 0x000000, $filled = false) + { + // TODO: Implement ellipse() method. + } + + /** + * Draws a circle + * + * @param int $cx + * @param int $cy + * @param int $r + * @param int $color + * @param bool $filled + * + * @return $this + */ + public function circle($cx, $cy, $r, $color = 0x000000, $filled = false) + { + // TODO: Implement circle() method. + } + + /** + * Draws a polygon + * + * @param array $points + * @param int $color + * @param bool $filled + * + * @return $this + */ + public function polygon(array $points, $color, $filled = false) + { + // TODO: Implement polygon() method. + } + + /** + * Flips the image + * + * @param int $flipVertical + * @param int $flipHorizontal + * + * @return $this + */ + public function flip($flipVertical, $flipHorizontal) + { + // TODO: Implement flip() method. + } - /** - * Creating an image using $data - */ - protected function createImageFromData($data){ - // TODO: Implement createImageFromData() method. - } + /** + * @inheritdoc + */ + protected function openGif($file) + { + $this->resource = new \Imagick($file); + } - /** - * Resizes the image to an image having size of $target_width, $target_height, using - * $new_width and $new_height and padding with $bg color - */ - protected function doResize($bg, $target_width, $target_height, $new_width, $new_height){ - // TODO: Implement doResize() method. - } + /** + * @inheritdoc + */ + protected function openJpeg($file) + { + $this->resource = new \Imagick($file); + } - /** - * Gets the color of the $x, $y pixel - */ - protected function getColor($x, $y){ - // TODO: Implement getColor() method. - } + /** + * @inheritdoc + */ + protected function openPng($file) + { + $this->resource = new \Imagick($file); + } + + /** + * Creates an image + */ + protected function createImage($width, $height) + { + // TODO: Implement createImage() method. + } + + /** + * Creating an image using $data + */ + protected function createImageFromData($data) + { + // TODO: Implement createImageFromData() method. + } + + /** + * Resizes the image to an image having size of $target_width, $target_height, using + * $new_width and $new_height and padding with $bg color + */ + protected function doResize($bg, $target_width, $target_height, $new_width, $new_height) + { + // TODO: Implement doResize() method. + } + + /** + * Gets the color of the $x, $y pixel + */ + protected function getColor($x, $y) + { + // TODO: Implement getColor() method. + } } diff --git a/Image.php b/Image.php index 4a51fc0..9fb9f4f 100644 --- a/Image.php +++ b/Image.php @@ -87,7 +87,7 @@ class Image 'png' => 'png', 'gif' => 'gif', ); - + /** * Fallback image */ @@ -252,8 +252,8 @@ public function getCacheFallback() public function getAdapter() { if (null === $this->adapter) { - // Defaults to GD - $this->setAdapter('gd'); + // Defaults to Imagick + $this->setAdapter('imagick'); } return $this->adapter; @@ -268,16 +268,18 @@ public function setAdapter($adapter) $adapter = strtolower($adapter); switch ($adapter) { - case 'gd': - $this->adapter = new Adapter\GD(); - break; - case 'imagemagick': - case 'imagick': - $this->adapter = new Adapter\Imagick(); - break; - default: - throw new \Exception('Unknown adapter: '.$adapter); - break; + case 'gd': + $this->adapter = new Adapter\GD(); + break; + case 'imagemagick': + $this->adapter = new Adapter\Imagick(); + break; + case 'imagick': + $this->adapter = new Adapter\Imagick(); + break; + default: + throw new \Exception('Unknown adapter: '.$adapter); + break; } } else { throw new \Exception('Unable to load the given adapter (not string or Adapter)'); @@ -454,8 +456,8 @@ public function cacheFile($type = 'jpg', $quality = 80, $actual = false) // If the files does not exists, save it $image = $this; - // Target file should be younger than all the current image - // dependencies + // Target file should be younger than all the current image + // dependencies $conditions = array( 'younger-than' => $this->getDependencies() );