Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebP and AVIF support #6279

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions application/config/mimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 2 additions & 0 deletions system/language/english/imglib_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down
74 changes: 51 additions & 23 deletions system/libraries/Image_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1453,38 +1458,46 @@ 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'));
return FALSE;
}

return imagecreatefromgif($path);
case 2:
case IMAGETYPE_JPEG:
if ( ! function_exists('imagecreatefromjpeg'))
{
$this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
return FALSE;
}

return imagecreatefromjpeg($path);
case 3:
case IMAGETYPE_PNG:
if ( ! function_exists('imagecreatefrompng'))
{
$this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
return FALSE;
}

return imagecreatefrompng($path);
case 18:
case IMAGETYPE_WEBP:
if ( ! function_exists('imagecreatefromwebp'))
{
$this->set_error(array('imglib_unsupported_imagecreate', 'imglib_webp_not_supported'));
return FALSE;
}

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;
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion system/libraries/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down