61 $this->image = new \Imagick();
76 if (
$info->getFormat() ==
File\Image::FORMAT_JPEG)
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)
133 $source = $this->
getInfo()->toRectangle();
135 $needResize = $source->resize($this->jpegSize,
File\Image::RESIZE_PROPORTIONAL);
140 $this->image->setOption(
'jpeg:size', $this->jpegSize->getWidth() .
'x' . $this->jpegSize->getHeight());
141 $this->image->setSize($this->jpegSize->getWidth(), $this->jpegSize->getHeight());
312 if ($this->image ===
null)
319 if (!file_exists($font))
329 $draw = new \ImagickDraw();
330 $draw->setFont($font);
331 $draw->setFillColor(
new \ImagickPixel($watermark->
getColor()->toRgba()));
335 $draw->setFontSize(20);
337 $metrics = $this->image->queryFontMetrics($draw, $utfText);
340 if ($metrics[
"textWidth"] > 0)
356 $metrics = $this->image->queryFontMetrics($draw, $utfText);
358 $position =
new Rectangle($metrics[
"textWidth"], $metrics[
"textHeight"]);
371 $y = $position->getY() + $position->getHeight() - $descender;
378 return $this->image->annotateImage($draw, $position->getX(), $y, 0, $utfText);
386 if ($this->image ===
null)
396 $watermarkWidth =
$image->getWidth();
397 $watermarkHeight =
$image->getHeight();
399 $position =
new Rectangle($watermarkWidth, $watermarkHeight);
406 $watermarkAlpha = $watermark->
getAlpha();
408 if (intval(round($watermarkAlpha, 2)) < 1)
411 $image->image->evaluateImage(\Imagick::EVALUATE_MULTIPLY, $watermarkAlpha, \Imagick::CHANNEL_ALPHA);
416 $posY = $position->getY();
419 $posX = $position->getX();
422 $this->image->compositeImage(
$image->image, \Imagick::COMPOSITE_OVER, $posX, $posY);
424 $posX += $watermarkWidth;
425 if (!$repeat || $posX >
$width)
431 $posY += $watermarkHeight;
432 if (!$repeat || $posY > $height)
446 public function save(
$file, $quality = 95, $format =
null)
448 if ($this->image ===
null)
454 if ($format ===
null)
456 $format = $this->
getInfo()?->getFormat();
458 if ($format !==
null)
460 $format = static::convertFormat($format);
461 if ($format !==
null)
463 $prefix =
"{$format}:";
467 $this->image->setImageCompressionQuality($quality);
469 if ($format ===
"gif" || ($format = $this->image->getImageFormat()) ===
"GIF" || $format ===
"GIF87")
472 $this->image->quantizeImage(255, \Imagick::COLORSPACE_SRGB, 0,
false,
false);
477 return $this->image->deconstructImages()->writeImages($prefix .
$file,
true);
481 return $this->image->writeImage($prefix .
$file);
518 File\Image::FORMAT_BMP =>
"bmp",
519 File\Image::FORMAT_GIF =>
"gif",
520 File\Image::FORMAT_JPEG =>
"jpg",
521 File\Image::FORMAT_PNG =>
"png",
522 File\Image::FORMAT_WEBP =>
"webp",
525 if (isset($formats[$format]))
527 return $formats[$format];