Skip to content

Commit

Permalink
Image::save() $file cannot be null, added output()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 6, 2018
1 parent 8bc32a1 commit a316b52
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions src/Utils/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
}


Expand All @@ -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();
}

Expand Down Expand Up @@ -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']);
}
}


Expand Down

0 comments on commit a316b52

Please sign in to comment.