Skip to content

Commit 0c20c9d

Browse files
committed
added enum ImageType
1 parent c10300d commit 0c20c9d

2 files changed

Lines changed: 44 additions & 19 deletions

File tree

src/Utils/Image.php

Lines changed: 19 additions & 19 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

@@ -266,7 +266,7 @@ public static function detectTypeFromString(string $s, &$width = null, &$height
266266

267267

268268
/**
269-
* Returns the file extension for the given `Image::XXX` constant.
269+
* Returns the file extension for the given `ImageType::XXX` constant.
270270
*/
271271
public static function typeToExtension(int $type): string
272272
{
@@ -279,11 +279,11 @@ public static function typeToExtension(int $type): string
279279

280280

281281
/**
282-
* Returns the `Image::XXX` constant for given file extension.
282+
* Returns the `ImageType::XXX` constant for given file extension.
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'.");
@@ -294,7 +294,7 @@ public static function extensionToType(string $extension): int
294294

295295

296296
/**
297-
* Returns the mime type for the given `Image::XXX` constant.
297+
* Returns the mime type for the given `ImageType::XXX` constant.
298298
*/
299299
public static function typeToMimeType(int $type): string
300300
{
@@ -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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
* Type of image file.
15+
*/
16+
/*enum*/ final class ImageType
17+
{
18+
public const
19+
JPEG = IMAGETYPE_JPEG,
20+
PNG = IMAGETYPE_PNG,
21+
GIF = IMAGETYPE_GIF,
22+
WEBP = IMAGETYPE_WEBP,
23+
AVIF = 19, // IMAGETYPE_AVIF,
24+
BMP = IMAGETYPE_BMP;
25+
}

0 commit comments

Comments
 (0)