Skip to content

Commit b5a3d51

Browse files
committed
added enum ImageType
1 parent a4858af commit b5a3d51

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

src/Utils/Image.php

Lines changed: 28 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

@@ -249,6 +249,7 @@ public static function fromBlank(int $width, int $height, ?array $color = null):
249249

250250
/**
251251
* Returns the type of image from file.
252+
* @return ImageType::*|null
252253
*/
253254
public static function detectTypeFromFile(string $file, &$width = null, &$height = null): ?int
254255
{
@@ -259,6 +260,7 @@ public static function detectTypeFromFile(string $file, &$width = null, &$height
259260

260261
/**
261262
* Returns the type of image from string.
263+
* @return ImageType::*|null
262264
*/
263265
public static function detectTypeFromString(string $s, &$width = null, &$height = null): ?int
264266
{
@@ -268,7 +270,8 @@ public static function detectTypeFromString(string $s, &$width = null, &$height
268270

269271

270272
/**
271-
* Returns the file extension for the given `Image::XXX` constant.
273+
* Returns the file extension for the given image type.
274+
* @param ImageType::* $type
272275
* @return value-of<self::Formats>
273276
*/
274277
public static function typeToExtension(int $type): string
@@ -282,11 +285,12 @@ public static function typeToExtension(int $type): string
282285

283286

284287
/**
285-
* Returns the `Image::XXX` constant for given file extension.
288+
* Returns the image type for given file extension.
289+
* @return ImageType::*
286290
*/
287291
public static function extensionToType(string $extension): int
288292
{
289-
$extensions = array_flip(self::Formats) + ['jpg' => self::JPEG];
293+
$extensions = array_flip(self::Formats) + ['jpg' => ImageType::JPEG];
290294
$extension = strtolower($extension);
291295
if (!isset($extensions[$extension])) {
292296
throw new Nette\InvalidArgumentException("Unsupported file extension '$extension'.");
@@ -297,7 +301,8 @@ public static function extensionToType(string $extension): int
297301

298302

299303
/**
300-
* Returns the mime type for the given `Image::XXX` constant.
304+
* Returns the mime type for the given image type.
305+
* @param ImageType::* $type
301306
*/
302307
public static function typeToMimeType(int $type): string
303308
{
@@ -601,6 +606,7 @@ public function place(self $image, int|string $left = 0, int|string $top = 0, in
601606

602607
/**
603608
* Saves image to the file. 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).
609+
* @param ImageType::*|null $type
604610
* @throws ImageException
605611
*/
606612
public function save(string $file, ?int $quality = null, ?int $type = null): void
@@ -612,8 +618,9 @@ public function save(string $file, ?int $quality = null, ?int $type = null): voi
612618

613619
/**
614620
* 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).
621+
* @param ImageType::* $type
615622
*/
616-
public function toString(int $type = self::JPEG, ?int $quality = null): string
623+
public function toString(int $type = ImageType::JPEG, ?int $quality = null): string
617624
{
618625
return Helpers::capture(function () use ($type, $quality): void {
619626
$this->output($type, $quality);
@@ -632,9 +639,10 @@ public function __toString(): string
632639

633640
/**
634641
* 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).
642+
* @param ImageType::* $type
635643
* @throws ImageException
636644
*/
637-
public function send(int $type = self::JPEG, ?int $quality = null): void
645+
public function send(int $type = ImageType::JPEG, ?int $quality = null): void
638646
{
639647
header('Content-Type: ' . self::typeToMimeType($type));
640648
$this->output($type, $quality);
@@ -643,36 +651,37 @@ public function send(int $type = self::JPEG, ?int $quality = null): void
643651

644652
/**
645653
* Outputs image to browser or file.
654+
* @param ImageType::* $type
646655
* @throws ImageException
647656
*/
648657
private function output(int $type, ?int $quality, ?string $file = null): void
649658
{
650659
switch ($type) {
651-
case self::JPEG:
660+
case ImageType::JPEG:
652661
$quality = $quality === null ? 85 : max(0, min(100, $quality));
653662
$success = @imagejpeg($this->image, $file, $quality); // @ is escalated to exception
654663
break;
655664

656-
case self::PNG:
665+
case ImageType::PNG:
657666
$quality = $quality === null ? 9 : max(0, min(9, $quality));
658667
$success = @imagepng($this->image, $file, $quality); // @ is escalated to exception
659668
break;
660669

661-
case self::GIF:
670+
case ImageType::GIF:
662671
$success = @imagegif($this->image, $file); // @ is escalated to exception
663672
break;
664673

665-
case self::WEBP:
674+
case ImageType::WEBP:
666675
$quality = $quality === null ? 80 : max(0, min(100, $quality));
667676
$success = @imagewebp($this->image, $file, $quality); // @ is escalated to exception
668677
break;
669678

670-
case self::AVIF:
679+
case ImageType::AVIF:
671680
$quality = $quality === null ? 30 : max(0, min(100, $quality));
672681
$success = @imageavif($this->image, $file, $quality); // @ is escalated to exception
673682
break;
674683

675-
case self::BMP:
684+
case ImageType::BMP:
676685
$success = @imagebmp($this->image, $file); // @ is escalated to exception
677686
break;
678687

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)