Skip to content

Commit 0fddd5d

Browse files
committed
Image::save() and send() throws ImageException on failure (BC break)
1 parent fd24cce commit 0fddd5d

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
@@ -484,9 +484,10 @@ public function place(Image $image, $left = 0, $top = 0, int $opacity = 100)
484484

485485
/**
486486
* Saves image to the file. Quality is 0..100 for JPEG and WEBP, 0..9 for PNG.
487-
* @return bool TRUE on success or FALSE on failure.
487+
* @return void
488+
* @throws ImageException
488489
*/
489-
public function save(string $file = NULL, int $quality = NULL, int $type = NULL): bool
490+
public function save(string $file = NULL, int $quality = NULL, int $type = NULL)
490491
{
491492
if ($type === NULL) {
492493
$extensions = array_flip(self::$formats) + ['jpg' => self::JPEG];
@@ -500,22 +501,29 @@ public function save(string $file = NULL, int $quality = NULL, int $type = NULL)
500501
switch ($type) {
501502
case self::JPEG:
502503
$quality = $quality === NULL ? 85 : max(0, min(100, $quality));
503-
return imagejpeg($this->image, $file, $quality);
504+
$success = imagejpeg($this->image, $file, $quality);
505+
break;
504506

505507
case self::PNG:
506508
$quality = $quality === NULL ? 9 : max(0, min(9, $quality));
507-
return imagepng($this->image, $file, $quality);
509+
$success = imagepng($this->image, $file, $quality);
510+
break;
508511

509512
case self::GIF:
510-
return imagegif($this->image, $file);
513+
$success = imagegif($this->image, $file);
514+
break;
511515

512516
case self::WEBP:
513517
$quality = $quality === NULL ? 80 : max(0, min(100, $quality));
514-
return imagewebp($this->image, $file, $quality);
518+
$success = imagewebp($this->image, $file, $quality);
519+
break;
515520

516521
default:
517522
throw new Nette\InvalidArgumentException("Unsupported image type '$type'.");
518523
}
524+
if (!$success) {
525+
throw new ImageException(error_get_last()['message']);
526+
}
519527
}
520528

521529

@@ -548,15 +556,16 @@ public function __toString(): string
548556

549557
/**
550558
* Outputs image to browser. Quality is 0..100 for JPEG and WEBP, 0..9 for PNG.
551-
* @return bool TRUE on success or FALSE on failure.
559+
* @return void
560+
* @throws ImageException
552561
*/
553-
public function send(int $type = self::JPEG, int $quality = NULL): bool
562+
public function send(int $type = self::JPEG, int $quality = NULL)
554563
{
555564
if (!isset(self::$formats[$type])) {
556565
throw new Nette\InvalidArgumentException("Unsupported image type '$type'.");
557566
}
558567
header('Content-Type: image/' . self::$formats[$type]);
559-
return $this->save(NULL, $quality, $type);
568+
$this->save(NULL, $quality, $type);
560569
}
561570

562571

0 commit comments

Comments
 (0)