diff --git a/application/config/mimes.php b/application/config/mimes.php index b2e989fea9e..5b45f16bf55 100644 --- a/application/config/mimes.php +++ b/application/config/mimes.php @@ -90,6 +90,8 @@ 'tif' => 'image/tiff', 'heic' => 'image/heic', 'heif' => 'image/heif', + 'avif' => 'image/avif', + 'webp' => 'image/webp', 'css' => array('text/css', 'text/plain'), 'html' => array('text/html', 'text/plain'), 'htm' => array('text/html', 'text/plain'), diff --git a/system/language/english/imglib_lang.php b/system/language/english/imglib_lang.php index b0a80a5d063..1fc34df15f9 100644 --- a/system/language/english/imglib_lang.php +++ b/system/language/english/imglib_lang.php @@ -46,6 +46,8 @@ $lang['imglib_jpg_not_supported'] = 'JPG images are not supported.'; $lang['imglib_png_not_supported'] = 'PNG images are not supported.'; $lang['imglib_webp_not_supported'] = 'WEBP images are not supported.'; +$lang['imglib_avif_not_supported'] = 'AVIF images are not supported.'; +$lang['imglib_webp_not_supported'] = 'WEBP images are not supported.'; $lang['imglib_jpg_or_png_required'] = 'The image resize protocol specified in your preferences only works with JPEG or PNG image types.'; $lang['imglib_copy_error'] = 'An error was encountered while attempting to replace the file. Please make sure your file directory is writable.'; $lang['imglib_rotate_unsupported'] = 'Image rotation does not appear to be supported by your server.'; diff --git a/system/libraries/Image_lib.php b/system/libraries/Image_lib.php index 4e5fc7be6fc..8f4869b700e 100644 --- a/system/libraries/Image_lib.php +++ b/system/libraries/Image_lib.php @@ -566,7 +566,7 @@ public function initialize($props = array()) else { // Is there a file name? - if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $this->new_image)) + if ( ! preg_match('#\.(jpe?g|gif|png|webp|avif)$#i', $this->new_image)) { $this->dest_image = $this->source_image; $this->dest_folder = $this->new_image; @@ -814,7 +814,8 @@ public function image_process_gd($action = 'resize') $dst_img = $create($this->width, $this->height); - if ($this->image_type === 3) // png we can actually preserve transparency + // We can preserve transparency for PNG, WEBP, AVIF images + if (in_array($this->image_type, [IMAGETYPE_PNG, IMAGETYPE_WEBP, IMAGETYPE_AVIF], true)) { imagealphablending($dst_img, FALSE); imagesavealpha($dst_img, TRUE); @@ -935,22 +936,26 @@ public function image_process_netpbm($action = 'resize') // Build the resizing command switch ($this->image_type) { - case 1 : + case IMAGETYPE_GIF : $cmd_in = 'giftopnm'; $cmd_out = 'ppmtogif'; break; - case 2 : + case IMAGETYPE_JPEG : $cmd_in = 'jpegtopnm'; $cmd_out = 'ppmtojpeg'; break; - case 3 : + case IMAGETYPE_PNG : $cmd_in = 'pngtopnm'; $cmd_out = 'ppmtopng'; break; - case 18 : + /*case IMAGETYPE_WEBP : $cmd_in = 'webptopnm'; $cmd_out = 'ppmtowebp'; break; + case IMAGETYPE_AVIF : + $cmd_in = 'webptopnm'; + $cmd_out = 'ppmtowebp'; + break;*/ } if ($action === 'crop') @@ -1234,8 +1239,8 @@ public function overlay_watermark() imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity); } - // We can preserve transparency for PNG images - if ($this->image_type === 3) + // We can preserve transparency for PNG, WEBP, AVIF images + if (in_array($this->image_type, [IMAGETYPE_PNG, IMAGETYPE_WEBP, IMAGETYPE_AVIF], true)) { imagealphablending($src_img, FALSE); imagesavealpha($src_img, TRUE); @@ -1405,8 +1410,8 @@ public function text_watermark() imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); } - // We can preserve transparency for PNG images - if ($this->image_type === 3) + // We can preserve transparency for PNG, WEBP, AVIF images + if (in_array($this->image_type, [IMAGETYPE_PNG, IMAGETYPE_WEBP, IMAGETYPE_AVIF], true)) { imagealphablending($src_img, FALSE); imagesavealpha($src_img, TRUE); @@ -1453,7 +1458,7 @@ public function image_create_gd($path = '', $image_type = '') switch ($image_type) { - case 1: + case IMAGETYPE_GIF: if ( ! function_exists('imagecreatefromgif')) { $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); @@ -1461,7 +1466,7 @@ public function image_create_gd($path = '', $image_type = '') } return imagecreatefromgif($path); - case 2: + case IMAGETYPE_JPEG: if ( ! function_exists('imagecreatefromjpeg')) { $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); @@ -1469,7 +1474,7 @@ public function image_create_gd($path = '', $image_type = '') } return imagecreatefromjpeg($path); - case 3: + case IMAGETYPE_PNG: if ( ! function_exists('imagecreatefrompng')) { $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); @@ -1477,7 +1482,7 @@ public function image_create_gd($path = '', $image_type = '') } return imagecreatefrompng($path); - case 18: + case IMAGETYPE_WEBP: if ( ! function_exists('imagecreatefromwebp')) { $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_webp_not_supported')); @@ -1485,6 +1490,14 @@ public function image_create_gd($path = '', $image_type = '') } return imagecreatefromwebp($path); + case IMAGETYPE_AVIF: + if ( ! function_exists('imagecreatefromavif')) + { + $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_avif_not_supported')); + return FALSE; + } + + return imagecreatefromavif($path); default: $this->set_error(array('imglib_unsupported_imagecreate')); return FALSE; @@ -1506,7 +1519,7 @@ public function image_save_gd($resource) { switch ($this->image_type) { - case 1: + case IMAGETYPE_GIF: if ( ! function_exists('imagegif')) { $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); @@ -1519,7 +1532,7 @@ public function image_save_gd($resource) return FALSE; } break; - case 2: + case IMAGETYPE_JPEG : if ( ! function_exists('imagejpeg')) { $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); @@ -1532,7 +1545,7 @@ public function image_save_gd($resource) return FALSE; } break; - case 3: + case IMAGETYPE_PNG: if ( ! function_exists('imagepng')) { $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); @@ -1545,7 +1558,7 @@ public function image_save_gd($resource) return FALSE; } break; - case 18: + case IMAGETYPE_WEBP: if ( ! function_exists('imagewebp')) { $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_webp_not_supported')); @@ -1558,6 +1571,19 @@ public function image_save_gd($resource) return FALSE; } break; + case IMAGETYPE_AVIF: + if ( ! function_exists('imageavif')) + { + $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_avif_not_supported')); + return FALSE; + } + + if ( ! @imageavif($resource, $this->full_dst_path)) + { + $this->set_error('imglib_save_failed'); + return FALSE; + } + break; default: $this->set_error(array('imglib_unsupported_imagecreate')); return FALSE; @@ -1593,13 +1619,15 @@ public function image_display_gd($resource) switch ($this->image_type) { - case 1 : imagegif($resource); + case IMAGETYPE_GIF : imagegif($resource); + break; + case IMAGETYPE_JPEG : imagejpeg($resource, NULL, $this->quality); break; - case 2 : imagejpeg($resource, NULL, $this->quality); + case IMAGETYPE_PNG : imagepng($resource); break; - case 3 : imagepng($resource); + case IMAGETYPE_WEBP : imagewebp($resource); break; - case 18 : imagewebp($resource); + case IMAGETYPE_AVIF : imageavif($resource); break; default: echo 'Unable to display the image'; break; @@ -1695,7 +1723,7 @@ public function get_image_properties($path = '', $return = FALSE) return FALSE; } - $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); + $types = array(IMAGETYPE_GIF => 'gif', IMAGETYPE_JPEG => 'jpeg', IMAGETYPE_PNG => 'png', IMAGETYPE_WEBP => 'webp', IMAGETYPE_AVIF => 'avif'); $mime = isset($types[$vals[2]]) ? 'image/'.$types[$vals[2]] : 'image/jpg'; if ($return === TRUE) diff --git a/system/libraries/Upload.php b/system/libraries/Upload.php index 168211a9144..383efcb4bdb 100644 --- a/system/libraries/Upload.php +++ b/system/libraries/Upload.php @@ -815,7 +815,7 @@ public function set_image_properties($path = '') { if (FALSE !== ($D = @getimagesize($path))) { - $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); + $types = array(IMAGETYPE_GIF => 'gif', IMAGETYPE_JPEG => 'jpeg', IMAGETYPE_PNG => 'png', IMAGETYPE_WEBP => 'webp', IMAGETYPE_AVIF => 'avif'); $this->image_width = $D[0]; $this->image_height = $D[1];