[MEDIA] Add WebP support

WebP format is now the default image format for anything that
is not an animated GIF. Image Intervention doesn't support
animated WebPs so we don't convert animated GIFs.

ImageFile:
MediaFile:
default:
- Add WebP support
This commit is contained in:
tenma 2020-08-13 18:37:31 +01:00
parent 8a2c1658a8
commit 1371e3efb8
4 changed files with 18 additions and 21 deletions

View File

@ -93,7 +93,8 @@ class ImageFile extends MediaFile
($cmp($this, IMAGETYPE_BMP) && function_exists('imagecreatefrombmp')) || ($cmp($this, IMAGETYPE_BMP) && function_exists('imagecreatefrombmp')) ||
($cmp($this, IMAGETYPE_WBMP) && function_exists('imagecreatefromwbmp')) || ($cmp($this, IMAGETYPE_WBMP) && function_exists('imagecreatefromwbmp')) ||
($cmp($this, IMAGETYPE_XBM) && function_exists('imagecreatefromxbm')) || ($cmp($this, IMAGETYPE_XBM) && function_exists('imagecreatefromxbm')) ||
($cmp($this, IMAGETYPE_PNG) && function_exists('imagecreatefrompng')) ($cmp($this, IMAGETYPE_PNG) && function_exists('imagecreatefrompng')) ||
($cmp($this, IMAGETYPE_WEBP) && function_exists('imagecreatefromwebp'))
) )
) { ) {
common_debug("Mimetype '{$this->mimetype}' was not recognized as a supported format"); common_debug("Mimetype '{$this->mimetype}' was not recognized as a supported format");
@ -169,6 +170,7 @@ class ImageFile extends MediaFile
if ($media !== 'image') { if ($media !== 'image') {
throw new UnsupportedMediaException(_m('Unsupported media format.'), $file->getPath()); throw new UnsupportedMediaException(_m('Unsupported media format.'), $file->getPath());
} }
if (!empty($file->filename)) { if (!empty($file->filename)) {
$imgPath = $file->getPath(); $imgPath = $file->getPath();
} }
@ -282,22 +284,19 @@ class ImageFile extends MediaFile
} }
/** /**
* Several obscure file types should be normalized to PNG on resize. * Several obscure file types should be normalized to WebP on resize.
* *
* Keeps only PNG, JPEG and GIF * Keeps only GIF (if animated) and WebP formats
* *
* @return int * @return int
*/ */
public function preferredType() public function preferredType()
{ {
// Keep only JPEG and GIF in their original format if ($this->type == IMAGETYPE_GIF && $this->animated) {
if ($this->type === IMAGETYPE_JPEG || $this->type === IMAGETYPE_GIF) {
return $this->type; return $this->type;
} }
// We don't want to save some formats as they are rare, inefficient and antiquated
// thus we can't guarantee clients will support return IMAGETYPE_WEBP;
// So just save it as PNG
return IMAGETYPE_PNG;
} }
/** /**
@ -436,15 +435,12 @@ class ImageFile extends MediaFile
// Ensure we save in the correct format and allow customization based on type // Ensure we save in the correct format and allow customization based on type
$type = $this->preferredType(); $type = $this->preferredType();
switch ($type) { switch ($type) {
case IMAGETYPE_WEBP:
$img->save($outpath, 100, 'webp');
break;
case IMAGETYPE_GIF: case IMAGETYPE_GIF:
$img->save($outpath, 100, 'gif'); $img->save($outpath, 100, 'gif');
break; break;
case IMAGETYPE_PNG:
$img->save($outpath, 100, 'png');
break;
case IMAGETYPE_JPEG:
$img->save($outpath, common_config('image', 'jpegquality'), 'jpg');
break;
default: default:
// TRANS: Exception thrown when trying resize an unknown file type. // TRANS: Exception thrown when trying resize an unknown file type.
throw new Exception(_m('Unknown file type')); throw new Exception(_m('Unknown file type'));

View File

@ -408,8 +408,8 @@ class MediaFile
if ($media == 'image') { if ($media == 'image') {
// Use -1 for the id to avoid adding this temporary file to the DB // Use -1 for the id to avoid adding this temporary file to the DB
$img = new ImageFile(-1, $_FILES[$param]['tmp_name']); $img = new ImageFile(-1, $_FILES[$param]['tmp_name']);
// Validate the image by re-encoding it. Additionally normalizes old formats to PNG, // Validate the image by re-encoding it. Additionally normalizes old formats to WebP,
// keeping JPEG and GIF untouched // keeping GIF untouched if animated
$outpath = $img->resizeTo($img->filepath); $outpath = $img->resizeTo($img->filepath);
$ext = image_type_to_extension($img->preferredType(), false); $ext = image_type_to_extension($img->preferredType(), false);
} }

View File

@ -256,6 +256,7 @@ $default =
image_type_to_mime_type(IMAGETYPE_GIF) => image_type_to_extension(IMAGETYPE_GIF), image_type_to_mime_type(IMAGETYPE_GIF) => image_type_to_extension(IMAGETYPE_GIF),
'image/svg+xml' => 'svg', // No built-in constant 'image/svg+xml' => 'svg', // No built-in constant
image_type_to_mime_type(IMAGETYPE_ICO) => image_type_to_extension(IMAGETYPE_ICO), image_type_to_mime_type(IMAGETYPE_ICO) => image_type_to_extension(IMAGETYPE_ICO),
image_type_to_mime_type(IMAGETYPE_WEBP) => image_type_to_extension(IMAGETYPE_WEBP),
'audio/ogg' => 'ogg', 'audio/ogg' => 'ogg',
'audio/mpeg' => 'mpg', 'audio/mpeg' => 'mpg',
'audio/x-speex' => 'spx', 'audio/x-speex' => 'spx',