Skip to content
Open
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
98 changes: 82 additions & 16 deletions classes/PHPixie/Image/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
$this->set_quality($quality);
$image->writeImage($file);
}

if ($format == 'jpeg')
$image->destroy();
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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();
}
}

}