Skip to content

Commit fe69e37

Browse files
committed
added enum ImageType
1 parent 5c541d0 commit fe69e37

2 files changed

Lines changed: 42 additions & 16 deletions

File tree

src/Utils/Image.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,16 @@ class Image
133133

134134
/** image types */
135135
public const
136-
JPEG = IMAGETYPE_JPEG,
137-
PNG = IMAGETYPE_PNG,
138-
GIF = IMAGETYPE_GIF,
139-
WEBP = IMAGETYPE_WEBP,
140-
AVIF = 19, // IMAGETYPE_AVIF,
141-
BMP = IMAGETYPE_BMP;
136+
JPEG = ImageType::JPEG,
137+
PNG = ImageType::PNG,
138+
GIF = ImageType::GIF,
139+
WEBP = ImageType::WEBP,
140+
AVIF = ImageType::AVIF,
141+
BMP = ImageType::BMP;
142142

143143
public const EmptyGIF = "GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;";
144144

145-
private const Formats = [self::JPEG => 'jpeg', self::PNG => 'png', self::GIF => 'gif', self::WEBP => 'webp', self::AVIF => 'avif', self::BMP => 'bmp'];
145+
private const Formats = [ImageType::JPEG => 'jpeg', ImageType::PNG => 'png', ImageType::GIF => 'gif', ImageType::WEBP => 'webp', ImageType::AVIF => 'avif', ImageType::BMP => 'bmp'];
146146

147147
private \GdImage $image;
148148

@@ -283,7 +283,7 @@ public static function typeToExtension(int $type): string
283283
*/
284284
public static function extensionToType(string $extension): int
285285
{
286-
$extensions = array_flip(self::Formats) + ['jpg' => self::JPEG];
286+
$extensions = array_flip(self::Formats) + ['jpg' => ImageType::JPEG];
287287
$extension = strtolower($extension);
288288
if (!isset($extensions[$extension])) {
289289
throw new Nette\InvalidArgumentException("Unsupported file extension '$extension'.");
@@ -608,7 +608,7 @@ public function save(string $file, ?int $quality = null, ?int $type = null): voi
608608
/**
609609
* Outputs image to string. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).
610610
*/
611-
public function toString(int $type = self::JPEG, ?int $quality = null): string
611+
public function toString(int $type = ImageType::JPEG, ?int $quality = null): string
612612
{
613613
return Helpers::capture(function () use ($type, $quality): void {
614614
$this->output($type, $quality);
@@ -629,7 +629,7 @@ public function __toString(): string
629629
* Outputs image to browser. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).
630630
* @throws ImageException
631631
*/
632-
public function send(int $type = self::JPEG, ?int $quality = null): void
632+
public function send(int $type = ImageType::JPEG, ?int $quality = null): void
633633
{
634634
header('Content-Type: ' . self::typeToMimeType($type));
635635
$this->output($type, $quality);
@@ -643,31 +643,31 @@ public function send(int $type = self::JPEG, ?int $quality = null): void
643643
private function output(int $type, ?int $quality, ?string $file = null): void
644644
{
645645
switch ($type) {
646-
case self::JPEG:
646+
case ImageType::JPEG:
647647
$quality = $quality === null ? 85 : max(0, min(100, $quality));
648648
$success = @imagejpeg($this->image, $file, $quality); // @ is escalated to exception
649649
break;
650650

651-
case self::PNG:
651+
case ImageType::PNG:
652652
$quality = $quality === null ? 9 : max(0, min(9, $quality));
653653
$success = @imagepng($this->image, $file, $quality); // @ is escalated to exception
654654
break;
655655

656-
case self::GIF:
656+
case ImageType::GIF:
657657
$success = @imagegif($this->image, $file); // @ is escalated to exception
658658
break;
659659

660-
case self::WEBP:
660+
case ImageType::WEBP:
661661
$quality = $quality === null ? 80 : max(0, min(100, $quality));
662662
$success = @imagewebp($this->image, $file, $quality); // @ is escalated to exception
663663
break;
664664

665-
case self::AVIF:
665+
case ImageType::AVIF:
666666
$quality = $quality === null ? 30 : max(0, min(100, $quality));
667667
$success = @imageavif($this->image, $file, $quality); // @ is escalated to exception
668668
break;
669669

670-
case self::BMP:
670+
case ImageType::BMP:
671671
$success = @imagebmp($this->image, $file); // @ is escalated to exception
672672
break;
673673

src/Utils/ImageType.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Utils;
11+
12+
13+
14+
/**
15+
* Image type.
16+
*/
17+
/*enum*/ final class ImageType
18+
{
19+
public const
20+
JPEG = IMAGETYPE_JPEG,
21+
PNG = IMAGETYPE_PNG,
22+
GIF = IMAGETYPE_GIF,
23+
WEBP = IMAGETYPE_WEBP,
24+
AVIF = 19, // IMAGETYPE_AVIF,
25+
BMP = IMAGETYPE_BMP;
26+
}

0 commit comments

Comments
 (0)