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 5, 2018
1 parent 8bc32a1 commit e2a373b
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions src/Utils/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 e2a373b

Please sign in to comment.