Skip to content

Commit 7ed983a

Browse files
committed
Image::save() and send() throws ImageException on failure (BC break)
1 parent b21a1a6 commit 7ed983a

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/Utils/Image.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,10 @@ public function place(Image $image, $left = 0, $top = 0, int $opacity = 100): se
507507
* @param string filename
508508
* @param int quality (0..100 for JPEG and WEBP, 0..9 for PNG)
509509
* @param int optional image type
510-
* @return bool TRUE on success or FALSE on failure.
510+
* @return void
511+
* @throws ImageException
511512
*/
512-
public function save(string $file = NULL, int $quality = NULL, int $type = NULL): bool
513+
public function save(string $file = NULL, int $quality = NULL, int $type = NULL)
513514
{
514515
if ($type === NULL) {
515516
$extensions = array_flip(self::$formats) + ['jpg' => self::JPEG];
@@ -523,22 +524,29 @@ public function save(string $file = NULL, int $quality = NULL, int $type = NULL)
523524
switch ($type) {
524525
case self::JPEG:
525526
$quality = $quality === NULL ? 85 : max(0, min(100, $quality));
526-
return imagejpeg($this->image, $file, $quality);
527+
$success = imagejpeg($this->image, $file, $quality);
528+
break;
527529

528530
case self::PNG:
529531
$quality = $quality === NULL ? 9 : max(0, min(9, $quality));
530-
return imagepng($this->image, $file, $quality);
532+
$success = imagepng($this->image, $file, $quality);
533+
break;
531534

532535
case self::GIF:
533-
return imagegif($this->image, $file);
536+
$success = imagegif($this->image, $file);
537+
break;
534538

535539
case self::WEBP:
536540
$quality = $quality === NULL ? 80 : max(0, min(100, $quality));
537-
return imagewebp($this->image, $file, $quality);
541+
$success = imagewebp($this->image, $file, $quality);
542+
break;
538543

539544
default:
540545
throw new Nette\InvalidArgumentException("Unsupported image type '$type'.");
541546
}
547+
if (!$success) {
548+
throw new ImageException(error_get_last()['message']);
549+
}
542550
}
543551

544552

@@ -577,15 +585,16 @@ public function __toString(): string
577585
* Outputs image to browser.
578586
* @param int image type
579587
* @param int quality (0..100 for JPEG and WEBP, 0..9 for PNG)
580-
* @return bool TRUE on success or FALSE on failure.
588+
* @return void
589+
* @throws ImageException
581590
*/
582-
public function send(int $type = self::JPEG, int $quality = NULL): bool
591+
public function send(int $type = self::JPEG, int $quality = NULL)
583592
{
584593
if (!isset(self::$formats[$type])) {
585594
throw new Nette\InvalidArgumentException("Unsupported image type '$type'.");
586595
}
587596
header('Content-Type: image/' . self::$formats[$type]);
588-
return $this->save(NULL, $quality, $type);
597+
$this->save(NULL, $quality, $type);
589598
}
590599

591600

0 commit comments

Comments
 (0)