From a316b52b4c41a8ff79a643b10502496e0d28f1d2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 5 Apr 2018 16:01:40 +0200 Subject: [PATCH] Image::save() $file cannot be null, added output() --- src/Utils/Image.php | 68 ++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/src/Utils/Image.php b/src/Utils/Image.php index f4dfbb11f..1dd3cc276 100644 --- a/src/Utils/Image.php +++ b/src/Utils/Image.php @@ -481,7 +481,7 @@ public function place(self $image, $left = 0, $top = 0, int $opacity = 100) * Saves image to the file. Quality is 0..100 for JPEG and WEBP, 0..9 for PNG. * @throws ImageException */ - public function save(string $file = null, int $quality = null, int $type = null): void + public function save(string $file, int $quality = null, int $type = null): void { if ($type === null) { $extensions = array_flip(self::$formats) + ['jpg' => self::JPEG]; @@ -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']); + } }