61 $this->image = new \Imagick();
82 $allowAnimated = (isset($this->options[
"allowAnimatedImages"]) && $this->options[
"allowAnimatedImages"] ===
true);
85 $suffix = ($allowAnimated ?
'' :
'[0]');
87 $this->image->readImage($this->file . $suffix);
92 $this->image->thumbnailImage($this->jpegSize->getWidth(), $this->jpegSize->getHeight());
95 if ($allowAnimated && $this->image->getNumberImages() > 1)
97 $this->animated =
true;
98 $this->image = $this->image->coalesceImages();
103 catch (\ImagickException $e)
133 $source = $this->
getInfo()->toRectangle();
140 $this->image->setOption(
'jpeg:size', $this->jpegSize->getWidth() .
'x' . $this->jpegSize->getHeight());
141 $this->image->setSize($this->jpegSize->getWidth(), $this->jpegSize->getHeight());
296 if($this->image ===
null)
303 if(!file_exists($font))
313 $draw = new \ImagickDraw();
314 $draw->setFont($font);
315 $draw->setFillColor(
new \ImagickPixel($watermark->
getColor()->toRgba()));
317 if(($textWidth = $watermark->
getWidth()) > 0)
319 $draw->setFontSize(20);
321 $metrics = $this->image->queryFontMetrics($draw, $utfText);
324 if($metrics[
"textWidth"] > 0)
326 $scale = $textWidth / $metrics[
"textWidth"];
329 $fontSize = 20 * $scale;
330 $draw->setFontSize($fontSize);
332 $position =
new Rectangle($textWidth, $metrics[
"textHeight"] * $scale);
337 $fontSize = $watermark->
getFontSize($width) * (96/72);
338 $draw->setFontSize($fontSize);
340 $metrics = $this->image->queryFontMetrics($draw, $utfText);
342 $position =
new Rectangle($metrics[
"textWidth"], $metrics[
"textHeight"]);
347 $fontSize *= (72/90);
354 $descender = $fontSize * 0.2;
355 $y = $position->getY() + $position->getHeight() - $descender;
359 $y = $position->getY() + $fontSize;
362 return $this->image->annotateImage($draw, $position->getX(), $y, 0, $utfText);
370 if($this->image ===
null)
380 $watermarkWidth =
$image->getWidth();
381 $watermarkHeight =
$image->getHeight();
383 $position =
new Rectangle($watermarkWidth, $watermarkHeight);
390 $watermarkAlpha = $watermark->
getAlpha();
392 if(intval(round($watermarkAlpha, 2)) < 1)
395 $image->image->evaluateImage(\Imagick::EVALUATE_MULTIPLY, $watermarkAlpha, \Imagick::CHANNEL_ALPHA);
400 $posY = $position->getY();
403 $posX = $position->getX();
406 $this->image->compositeImage(
$image->image, \Imagick::COMPOSITE_OVER, $posX, $posY);
408 $posX += $watermarkWidth;
409 if($repeat ==
false || $posX > $width)
415 $posY += $watermarkHeight;
416 if($repeat ==
false || $posY > $height)
430 public function save(
$file, $quality = 95, $format =
null)
432 if ($this->image ===
null)
438 if ($format !==
null)
440 $format = static::convertFormat($format);
443 $prefix =
"{$format}:";
447 $this->image->setImageCompressionQuality($quality);
449 if ($format ===
"gif" || ($format = $this->image->getImageFormat()) ===
"GIF" || $format ===
"GIF87")
452 $this->image->quantizeImage(255, \Imagick::COLORSPACE_SRGB, 0,
false,
false);
457 return $this->image->deconstructImages()->writeImages($prefix.$file,
true);
461 return $this->image->writeImage($prefix.$file);
498 File\Image::FORMAT_BMP =>
"bmp",
499 File\Image::FORMAT_GIF =>
"gif",
500 File\Image::FORMAT_JPEG =>
"jpg",
501 File\Image::FORMAT_PNG =>
"png",
502 File\Image::FORMAT_WEBP =>
"webp",
505 if(isset($formats[$format]))
507 return $formats[$format];