Skip to content

Commit

Permalink
[5.1] Proper support for avif/webp images (#43295)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrammatiko committed May 14, 2024
1 parent 6d6d00b commit 8b79de7
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export default {
return api.canDelete && (typeof this.item.canDelete !== 'undefined' ? this.item.canDelete : true);
},
canOpenEditView() {
// @TODO pass the array of allowed to edit files from PHP
return ['jpg', 'jpeg', 'png'].includes(this.item.extension.toLowerCase());
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default {
}
// @todo remove the hardcoded extensions here
const extensionWithPreview = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'mp4', 'mp3', 'pdf'];
const extensionWithPreview = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'mp4', 'mp3', 'pdf'];
// Show preview
if (this.item.extension
Expand Down
2 changes: 1 addition & 1 deletion administrator/components/com_media/src/Model/ApiModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ function ($mediaType) use (&$types) {
',',
ComponentHelper::getParams('com_media')->get(
'image_extensions',
'bmp,gif,jpg,jpeg,png,webp'
'bmp,gif,jpg,jpeg,png,webp,avif'
)
)
);
Expand Down
2 changes: 1 addition & 1 deletion administrator/components/com_media/tmpl/file/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
'csrfToken' => Session::getFormToken(),
'uploadPath' => $this->file->path,
'editViewUrl' => Uri::base() . 'index.php?option=com_media&view=file' . ($tmpl ? '&tmpl=' . $tmpl : '') . '&mediatypes=' . $mediaTypes,
'imagesExtensions' => array_map('trim', explode(',', $params->get('image_extensions', 'bmp,gif,jpg,jpeg,png,webp'))),
'imagesExtensions' => array_map('trim', explode(',', $params->get('image_extensions', 'bmp,gif,jpg,jpeg,png,webp,avif'))),
'audioExtensions' => array_map('trim', explode(',', $params->get('audio_extensions', 'mp3,m4a,mp4a,ogg'))),
'videoExtensions' => array_map('trim', explode(',', $params->get('video_extensions', 'mp4,mp4v,mpeg,mov,webm'))),
'documentExtensions' => array_map('trim', explode(',', $params->get('doc_extensions', 'doc,odg,odp,ods,odt,pdf,ppt,txt,xcf,xls,csv'))),
Expand Down
2 changes: 1 addition & 1 deletion administrator/components/com_media/tmpl/media/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
'fileBaseUrl' => Uri::root() . $params->get('file_path', 'images'),
'fileBaseRelativeUrl' => $params->get('file_path', 'images'),
'editViewUrl' => Uri::base() . 'index.php?option=com_media&view=file' . ($tmpl ? '&tmpl=' . $tmpl : '') . $mediaTypes,
'imagesExtensions' => array_map('trim', explode(',', $params->get('image_extensions', 'bmp,gif,jpg,jpeg,png,webp'))),
'imagesExtensions' => array_map('trim', explode(',', $params->get('image_extensions', 'bmp,gif,jpg,jpeg,png,webp,avif'))),
'audioExtensions' => array_map('trim', explode(',', $params->get('audio_extensions', 'mp3,m4a,mp4a,ogg'))),
'videoExtensions' => array_map('trim', explode(',', $params->get('video_extensions', 'mp4,mp4v,mpeg,mov,webm'))),
'documentExtensions' => array_map('trim', explode(',', $params->get('doc_extensions', 'doc,odg,odp,ods,odt,pdf,ppt,txt,xcf,xls,csv'))),
Expand Down
2 changes: 1 addition & 1 deletion libraries/src/Helper/MediaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function canUpload($file, $component = 'com_media', $allowedExecutables =

$filetype = array_pop($filetypes);

$allowable = array_map('trim', explode(',', $params->get('restrict_uploads_extensions', 'bmp,gif,jpg,jpeg,png,webp,ico,mp3,m4a,mp4a,ogg,mp4,mp4v,mpeg,mov,odg,odp,ods,odt,pdf,png,ppt,txt,xcf,xls,csv')));
$allowable = array_map('trim', explode(',', $params->get('restrict_uploads_extensions', 'bmp,gif,jpg,jpeg,png,webp,avif,ico,mp3,m4a,mp4a,ogg,mp4,mp4v,mpeg,mov,odg,odp,ods,odt,pdf,png,ppt,txt,xcf,xls,csv')));
$ignored = array_map('trim', explode(',', $params->get('ignore_extensions', '')));

if ($filetype == '' || $filetype == false || (!\in_array($filetype, $allowable) && !\in_array($filetype, $ignored))) {
Expand Down
16 changes: 16 additions & 0 deletions libraries/src/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function __construct($source = null)
static::$formats[IMAGETYPE_PNG] = $info['PNG Support'];
static::$formats[IMAGETYPE_GIF] = $info['GIF Read Support'];
static::$formats[IMAGETYPE_WEBP] = $info['WebP Support'];
static::$formats[IMAGETYPE_AVIF] = $info['AVIF Support'];
}

// If the source input is a resource, set it as the image handle.
Expand Down Expand Up @@ -583,6 +584,18 @@ public function loadFile($path)

// Attempt to load the image based on the MIME-Type
switch ($properties->mime) {
case 'image/avif':
// Make sure the image type is supported.
if (empty(static::$formats[IMAGETYPE_AVIF])) {
throw new \RuntimeException('Attempting to load an image of unsupported type AVIF.');
}

// Attempt to create the image handle.
$handle = imagecreatefromavif($path);
$type = 'AVIF';

break;

case 'image/gif':
// Make sure the image type is supported.
if (empty(static::$formats[IMAGETYPE_GIF])) {
Expand Down Expand Up @@ -919,6 +932,9 @@ public function watermark(Image $watermark, $transparency = 50, $bottomMargin =
public function toFile($path, $type = IMAGETYPE_JPEG, array $options = [])
{
switch ($type) {
case IMAGETYPE_AVIF:
return imageavif($this->getHandle(), $path, (\array_key_exists('quality', $options)) ? $options['quality'] : 100);

case IMAGETYPE_GIF:
return imagegif($this->getHandle(), $path);

Expand Down
19 changes: 15 additions & 4 deletions plugins/media-action/resize/src/Extension/Resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public function onContentBeforeSave($context, $item, $isNew, $data = [])
return;
}

if (!\in_array($item->extension, ['jpg', 'jpeg', 'png', 'gif'])) {
if (!\in_array(strtolower($item->extension), ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif'])) {
return;
}

if (strtolower($item->extension) === 'avif' && !\function_exists('imageavif')) {
return;
}

Expand All @@ -63,14 +67,21 @@ public function onContentBeforeSave($context, $item, $isNew, $data = [])
Image::SCALE_INSIDE
);

$type = IMAGETYPE_JPEG;

switch ($item->extension) {
switch (strtolower($item->extension)) {
case 'gif':
$type = IMAGETYPE_GIF;
break;
case 'png':
$type = IMAGETYPE_PNG;
break;
case 'avif':
$type = IMAGETYPE_AVIF;
break;
case 'webp':
$type = IMAGETYPE_WEBP;
break;
default:
$type = IMAGETYPE_JPEG;
}

ob_start();
Expand Down

0 comments on commit 8b79de7

Please sign in to comment.