From be436d8771943dbfe4cd40170bb0db778dff771e Mon Sep 17 00:00:00 2001 From: aVadim483 Date: Sun, 1 Mar 2015 00:33:25 +0300 Subject: [PATCH 1/2] Supports animated gif --- classes/PHPixie/Image/Imagick.php | 98 ++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 16 deletions(-) diff --git a/classes/PHPixie/Image/Imagick.php b/classes/PHPixie/Image/Imagick.php index 3289502..a504a6c 100755 --- a/classes/PHPixie/Image/Imagick.php +++ b/classes/PHPixie/Image/Imagick.php @@ -60,7 +60,7 @@ public function load($bytes) { * * @param int $width Image width * @param int $height Image height - * @param bool $format Whether to get image format + * @param bool $get_format Whether to get image format */ protected function update_size($width, $height, $get_format = false) { $this->width = $width; @@ -113,7 +113,7 @@ public function render($format = 'png', $die = true, $quality = 90) { throw new \Exception("Type must be either png, jpeg or gif"); } $this->set_quality($quality); - echo $image; + echo $image->getImagesBlob(); if($die){ die; @@ -139,8 +139,12 @@ public function save($file, $format = null, $quality = 90) { throw new \Exception("Type must be either png, jpeg or gif"); } - $this->set_quality($quality); - $image->writeImage($file); + if ($format == 'gif' && $this->multiframe()) { + $image->writeImages($file, true); + } else { + $image->set_quality($quality); + $image->writeImage($file); + } if ($format == 'jpeg') $image->destroy(); @@ -162,7 +166,15 @@ public function crop($width, $height, $x = 0, $y = 0) { if ($height > ($maxheight = $this->height-$y)) $height = $maxheight; - $this->image->cropImage($width, $height, $x, $y); + if ($this->multiframe()) { + $this->image = $this->image->coalesceImages(); + foreach ($this->image as $frame) { + $frame->cropImage($width, $height, $x, $y); + $frame->setImagePage($width, $height, 0, 0); + } + } else { + $this->image->cropImage($width, $height, $x, $y); + } $this->update_size($width, $height); return $this; @@ -172,22 +184,57 @@ public function scale($scale){ $width = ceil($this->width*$scale); $height = ceil($this->height*$scale); - $this->image->scaleImage($width, $height, true); + if ($this->multiframe()) { + $this->image = $this->image->coalesceImages(); + foreach ($this->image as $frame) { + $frame->scaleImage($width, $height, true); + $frame->setImagePage($width, $height, 0, 0); + } + } else { + $this->image->scaleImage($width, $height, true); + } $this->update_size($width, $height); return $this; } public function rotate($angle, $bg_color = 0xffffff, $bg_opacity = 0) { - $this->image->rotateImage($this->get_color($bg_color, $bg_opacity), -$angle); + if ($this->multiframe()) { + foreach ($this->image as $frame) { + $frame->rotateImage( + $this->get_color($bg_color, $bg_opacity), -$angle + ); + $frame->setImagePage( + $this->image->width, $this->image->height, 0, 0 + ); + } + } else { + $this->image->rotateImage( + $this->get_color($bg_color, $bg_opacity), -$angle + ); + } $this->update_size($this->image->getImageWidth(), $this->image->getImageHeight()); return $this; } public function flip($flip_x = false, $flip_y = false) { - if ($flip_x) - $this->image->flopImage(); - if ($flip_y) - $this->image->flipImage(); + if ($flip_x) { + if ($this->multiframe()) { + foreach ($this->image as $frame) { + $frame->flopImage(); + } + } else { + $this->image->flopImage(); + } + } + if ($flip_y) { + if ($this->multiframe()) { + foreach ($this->image as $frame) { + $frame->flipImage(); + } + } else { + $this->image->flipImage(); + } + } return $this; } @@ -207,7 +254,14 @@ protected function draw_text($text, $size, $font_file, $x, $y, $color, $opacity, $draw->setFont($font_file); $draw->setFontSize($size); $draw->setFillColor($this->get_color($color, $opacity)); - $this->image-> annotateImage($draw, $x, $y, -$angle, $text); + if ($this->multiframe()) { + $this->image = $this->image->coalesceImages(); + foreach ($this->image as $frame) { + $frame->annotateImage($draw, $x, $y, -$angle, $text); + } + } else { + $this->image->annotateImage($draw, $x, $y, -$angle, $text); + } return $this; } @@ -227,11 +281,23 @@ public function text_metrics($text, $size, $font_file) { /** * Set Compression Quality * - * @params integer $quality Compression quality + * @param integer $quality Compression quality * * @return void */ - protected function set_quality($quality) { - $this->image->setImageCompressionQuality($quality); - } + protected function set_quality($quality) { + $this->image->setImageCompressionQuality($quality); + } + + /** + * Returns true if image has many frames + * + * @return bool + */ + protected function multiframe() { + if ($this->image) { + return (bool)$this->image->getImageIterations(); + } + } + } From 06ac6441e77eca7a822c4b4ef9729e47a5bf81d7 Mon Sep 17 00:00:00 2001 From: aVadim483 Date: Sun, 1 Mar 2015 03:26:08 +0300 Subject: [PATCH 2/2] Fixed 'save' method --- classes/PHPixie/Image/Imagick.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/PHPixie/Image/Imagick.php b/classes/PHPixie/Image/Imagick.php index a504a6c..bdf49de 100755 --- a/classes/PHPixie/Image/Imagick.php +++ b/classes/PHPixie/Image/Imagick.php @@ -142,7 +142,7 @@ public function save($file, $format = null, $quality = 90) { if ($format == 'gif' && $this->multiframe()) { $image->writeImages($file, true); } else { - $image->set_quality($quality); + $this->set_quality($quality); $image->writeImage($file); }