Skip to content

Commit 6b0f16f

Browse files
committed
Jet Core
----------------------- * Jet\Data_Image - Refactoring, WEBP and AVIF formats supports added * Jet\Data_Image::typeIsSupported( int $type ) : bool method added * Jet\Data_Image::getSupportedMimeTypes() : array method added * Jet\Form_Field_FileImage->getAllowedMimeTypes() is using Jet\Data_Image::getSupportedMimeTypes() for default value now
1 parent 6611ba1 commit 6b0f16f

File tree

3 files changed

+107
-32
lines changed

3 files changed

+107
-32
lines changed

library/Jet/Data/Image.php

Lines changed: 106 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,60 @@ class Data_Image extends BaseObject
1414
public const TYPE_GIF = IMAGETYPE_GIF;
1515
public const TYPE_JPG = IMAGETYPE_JPEG;
1616
public const TYPE_PNG = IMAGETYPE_PNG;
17+
public const TYPE_WEBP = IMAGETYPE_WEBP;
18+
public const TYPE_AVIF = IMAGETYPE_AVIF;
19+
20+
protected static array $image_create_function = [
21+
self::TYPE_GIF => 'imagecreatefromgif',
22+
self::TYPE_JPG => 'imagecreatefromjpeg',
23+
self::TYPE_PNG => 'imagecreatefrompng',
24+
self::TYPE_WEBP => 'imagecreatefromwebp',
25+
self::TYPE_AVIF => 'imagecreatefromavif',
26+
];
27+
28+
protected static array $mime_types = [
29+
self::TYPE_GIF => [
30+
'image/gif'
31+
],
32+
self::TYPE_JPG => [
33+
'image/pjpeg',
34+
'image/jpeg',
35+
'image/jpg',
36+
],
37+
self::TYPE_PNG => [
38+
'image/png',
39+
],
40+
self::TYPE_WEBP => [
41+
'image/webp',
42+
],
43+
self::TYPE_AVIF => [
44+
'image/avif',
45+
],
46+
47+
];
48+
49+
protected static array $types_that_has_alpha = [
50+
self::TYPE_PNG,
51+
self::TYPE_GIF
52+
];
53+
54+
protected static array $types_that_has_quality = [
55+
self::TYPE_JPG,
56+
self::TYPE_WEBP,
57+
self::TYPE_AVIF
58+
];
59+
60+
protected static array $types_that_has_extra_param = [
61+
];
62+
63+
64+
protected static array $image_output_function = [
65+
self::TYPE_GIF => 'imagegif',
66+
self::TYPE_JPG => 'imagejpeg',
67+
self::TYPE_PNG => 'imagepng',
68+
self::TYPE_WEBP => 'imagewebp',
69+
self::TYPE_AVIF => 'imageavif',
70+
];
1771

1872
/**
1973
* @var string
@@ -92,6 +146,28 @@ public function __construct( string $path )
92146

93147
$this->mime_type = $image_dat['mime'];
94148
}
149+
150+
public static function typeIsSupported( int $type ): bool
151+
{
152+
if(!isset(static::$image_output_function[$type])) {
153+
return false;
154+
}
155+
156+
$function = static::$image_output_function[$type];
157+
return function_exists( $function );
158+
}
159+
160+
public static function getSupportedMimeTypes(): array
161+
{
162+
$supported = [];
163+
foreach(static::$mime_types as $type => $mime_types) {
164+
if(static::typeIsSupported($type)) {
165+
$supported = array_merge($supported, $mime_types);
166+
}
167+
}
168+
169+
return $supported;
170+
}
95171

96172
/**
97173
* @return string
@@ -216,6 +292,7 @@ public function createThumbnail( string $target_path, int $maximal_width, int $m
216292
*/
217293
public function saveAs( string $target_path, ?int $new_width = null, ?int $new_height = null, ?int $target_img_type = null ): static
218294
{
295+
219296
if( !$target_img_type ) {
220297
$target_img_type = $this->img_type;
221298
}
@@ -227,27 +304,36 @@ public function saveAs( string $target_path, ?int $new_width = null, ?int $new_h
227304
if( !$new_height ) {
228305
$new_height = $this->height;
229306
}
230-
231-
$image = match ($this->img_type) {
232-
self::TYPE_JPG => imagecreatefromjpeg( $this->path ),
233-
self::TYPE_GIF => imagecreatefromgif( $this->path ),
234-
self::TYPE_PNG => imagecreatefrompng( $this->path ),
235-
default => throw new Data_Image_Exception('Unsupported image type: '.$this->mime_type ),
236-
};
237-
238-
if( !$image ) {
307+
308+
$image_create_function = static::$image_create_function[$this->img_type]??'';
309+
310+
if(
311+
!$image_create_function ||
312+
!function_exists($image_create_function)
313+
) {
239314
throw new Data_Image_Exception(
240-
'File: \'' . $this->path . '\' Unsupported type! Unable to get image size!',
315+
'File: \'' . $this->path . '\' Unsupported type! Unable to get image size! ('.$image_create_function.')',
316+
Data_Image_Exception::CODE_UNSUPPORTED_IMAGE_TYPE
317+
);
318+
}
319+
320+
321+
$image = Debug_ErrorHandler::doItSilent(function() use ($image_create_function) {
322+
return call_user_func( $image_create_function, $this->path );
323+
});
324+
325+
326+
327+
if(!$image) {
328+
throw new Data_Image_Exception(
329+
'File: \'' . $this->path . '\' Unsupported type! Unable to get image size! ('.$image_create_function.')',
241330
Data_Image_Exception::CODE_UNSUPPORTED_IMAGE_TYPE
242331
);
243332
}
244333

245334
$new_image = imagecreatetruecolor( $new_width, $new_height );
246335

247-
if(
248-
$target_img_type == self::TYPE_PNG ||
249-
$target_img_type == self::TYPE_GIF
250-
) {
336+
if( in_array( $target_img_type, static::$types_that_has_alpha, true ) ) {
251337
imagealphablending( $new_image, false );
252338
imagesavealpha( $new_image, true );
253339

@@ -258,16 +344,12 @@ public function saveAs( string $target_path, ?int $new_width = null, ?int $new_h
258344

259345
imagecopyresampled( $new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $this->width, $this->height );
260346

261-
switch( $target_img_type ) {
262-
case self::TYPE_JPG:
263-
imagejpeg( $new_image, $target_path, $this->image_quality );
264-
break;
265-
case self::TYPE_GIF:
266-
imagegif( $new_image, $target_path );
267-
break;
268-
case self::TYPE_PNG:
269-
imagepng( $new_image, $target_path );
270-
break;
347+
$output_method = static::$image_output_function[$target_img_type];
348+
349+
if( in_array($target_img_type, static::$types_that_has_quality) ) {
350+
call_user_func($output_method, $new_image, $target_path, $this->image_quality );
351+
} else {
352+
call_user_func($output_method, $new_image, $target_path );
271353
}
272354

273355
imagedestroy( $new_image );

library/Jet/Db/Backend/PDO.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function __construct( Db_Backend_Config $config )
3838
/**
3939
* @var Db_Backend_PDO_Config $config
4040
*/
41-
4241
$this->config = $config;
4342

4443
$this->connect();

library/Jet/Form/Field/FileImage.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ class Form_Field_FileImage extends Form_Field implements Form_Field_Part_File_In
5959
public function getAllowedMimeTypes(): array
6060
{
6161
if(!$this->allowed_mime_types) {
62-
return [
63-
'image/pjpeg',
64-
'image/jpeg',
65-
'image/jpg',
66-
'image/gif',
67-
'image/png',
68-
];
62+
$this->allowed_mime_types = Data_Image::getSupportedMimeTypes();
6963
}
7064

7165
return $this->allowed_mime_types;

0 commit comments

Comments
 (0)