diff --git a/src/Utils/Image.php b/src/Utils/Image.php index f4dfbb11f..629d4745c 100644 --- a/src/Utils/Image.php +++ b/src/Utils/Image.php @@ -492,32 +492,7 @@ public function save(string $file = null, int $quality = null, int $type = null) $type = $extensions[$ext]; } - switch ($type) { - case self::JPEG: - $quality = $quality === null ? 85 : max(0, min(100, $quality)); - $success = imagejpeg($this->image, $file, $quality); - break; - - case self::PNG: - $quality = $quality === null ? 9 : max(0, min(9, $quality)); - $success = imagepng($this->image, $file, $quality); - break; - - case self::GIF: - $success = imagegif($this->image, $file); - break; - - case self::WEBP: - $quality = $quality === null ? 80 : max(0, min(100, $quality)); - $success = imagewebp($this->image, $file, $quality); - break; - - default: - throw new Nette\InvalidArgumentException("Unsupported image type '$type'."); - } - if (!$success) { - throw new ImageException(error_get_last()['message']); - } + $this->output($type, $quality, $file); } @@ -527,7 +502,7 @@ public function save(string $file = null, int $quality = null, int $type = null) public function toString(int $type = self::JPEG, int $quality = null): string { ob_start(function () {}); - $this->save(null, $quality, $type); + $this->output($type, $quality); return ob_get_clean(); } @@ -558,7 +533,42 @@ public function send(int $type = self::JPEG, int $quality = null): void throw new Nette\InvalidArgumentException("Unsupported image type '$type'."); } header('Content-Type: ' . image_type_to_mime_type($type)); - $this->save(null, $quality, $type); + $this->output($type, $quality); + } + + + /** + * Outputs image to browser or file. + * @throws ImageException + */ + private function output(int $type, ?int $quality, string $file = null): void + { + switch ($type) { + case self::JPEG: + $quality = $quality === null ? 85 : max(0, min(100, $quality)); + $success = imagejpeg($this->image, $file, $quality); + break; + + case self::PNG: + $quality = $quality === null ? 9 : max(0, min(9, $quality)); + $success = imagepng($this->image, $file, $quality); + break; + + case self::GIF: + $success = imagegif($this->image, $file); + break; + + case self::WEBP: + $quality = $quality === null ? 80 : max(0, min(100, $quality)); + $success = imagewebp($this->image, $file, $quality); + break; + + default: + throw new Nette\InvalidArgumentException("Unsupported image type '$type'."); + } + if (!$success) { + throw new ImageException(error_get_last()['message']); + } }