ob_start();
+ // fix PNG alpha channel handling
+ // see http://php.net/manual/en/function.imagecopymerge.php#92787
imagealphablending($image, false);
imagesavealpha($image, true);
+
if ($this->type == IMAGETYPE_GIF) {
imagegif($image);
}
// does nothing
}
+ /**
+ * @inheritDoc
+ */
+ public function saveImageAs($image, string $filename, string $type, int $quality = 100): void {
+ if (!$this->isImage($image)) {
+ throw new \InvalidArgumentException("Given image is not a valid image resource.");
+ }
+
+ ob_start();
+
+ // fix PNG alpha channel handling
+ // see http://php.net/manual/en/function.imagecopymerge.php#92787
+ imagealphablending($image, false);
+ imagesavealpha($image, true);
+
+ switch ($type) {
+ case "gif":
+ imagegif($image);
+ break;
+
+ case "jpg":
+ case "jpeg":
+ imagejpeg($image, null, $quality);
+ break;
+
+ case "png":
+ imagepng($image, null, $quality);
+ break;
+
+ case "webp":
+ imagewebp($image, null, $quality);
+ break;
+
+ default:
+ throw new \InvalidArgumentException("Unreachable");
+ }
+
+ $stream = ob_get_contents();
+ ob_end_clean();
+
+ file_put_contents($filename, $stream);
+ }
+
/**
* @inheritDoc
*/
*/
public function overlayImageRelative($file, $position, $margin, $opacity);
+ /**
+ * Saves an image using a different file type.
+ *
+ * @since 5.4
+ */
+ public function saveImageAs($image, string $filename, string $type, int $quality = 100): void;
+
/**
* Determines if an image adapter is supported.
*
return FileUtil::checkMemoryLimit($width * $height * $channels * 2.1);
}
+ /**
+ * @inheritDoc
+ */
+ public function saveImageAs($image, string $filename, string $type, int $quality = 100): void {
+ switch ($type) {
+ case "gif":
+ case "jpg":
+ case "jpeg":
+ case "png":
+ case "webp":
+ break;
+
+ default:
+ throw new \InvalidArgumentException("Unsupported image format '{$type}'.");
+ }
+
+ if ($quality < 0 || $quality > 100) {
+ throw new \InvalidArgumentException("The quality must be an integer between 0 and 100.");
+ }
+
+ $this->adapter->saveImageAs($image, $filename, $type, $quality);
+ }
+
+
/**
* @inheritDoc
*/
return $match['version'];
}
+ /**
+ * @inheritDoc
+ */
+ public function saveImageAs($image, string $filename, string $type, int $quality = 100): void {
+ if (!($image instanceof \Imagick)) {
+ throw new \InvalidArgumentException("Given image is not a valid Imagick-object.");
+ }
+
+ // Greatly reduces the time required to create the image and drastically
+ // reduces the filesize to more reasonable levels without a visible
+ // quality loss.
+ //
+ // See https://github.com/Imagick/imagick/issues/360
+ if ($image->getImageFormat() == "GIF") {
+ $image = $image->deconstructImages();
+ $image->quantizeImages(256, \Imagick::COLORSPACE_SRGB, 0, false, false);
+ }
+
+ switch ($type) {
+ case "jpg":
+ case "jpeg":
+ $fileFormat = "jpg";
+ break;
+
+ case "png":
+ $fileFormat = "png";
+ break;
+
+ case "webp":
+ $fileFormat = "webp";
+ break;
+
+ default:
+ throw new \LogicException("Unreachable");
+ }
+
+ $image->writeImages("{$fileFormat}:{$filename}", true);
+ }
+
+
/**
* @param string $version
* @return bool