[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:
parent
8a2c1658a8
commit
1371e3efb8
|
@ -1447,7 +1447,7 @@ CreateFileImageThumbnailSource: Hook to create image thumbnail source from a Fil
|
||||||
|
|
||||||
StartResizeImageFile: Hook to resize an image and output it to a file. No matching End event yet.
|
StartResizeImageFile: Hook to resize an image and output it to a file. No matching End event yet.
|
||||||
- $imagefile: ImageFile object we're resizing.
|
- $imagefile: ImageFile object we're resizing.
|
||||||
- $outpath: string with output filepath
|
- $outpath: string with output filepath
|
||||||
- $box: array with size ('width', 'height') and boundary box('x', 'y', 'w', 'h').
|
- $box: array with size ('width', 'height') and boundary box('x', 'y', 'w', 'h').
|
||||||
|
|
||||||
FillImageFileMetadata: Get more metadata about the ImageFile if it is perhaps not a real local file
|
FillImageFileMetadata: Get more metadata about the ImageFile if it is perhaps not a real local file
|
||||||
|
|
|
@ -88,12 +88,13 @@ class ImageFile extends MediaFile
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
if (!(($cmp($this, IMAGETYPE_GIF) && function_exists('imagecreatefromgif')) ||
|
if (!(($cmp($this, IMAGETYPE_GIF) && function_exists('imagecreatefromgif')) ||
|
||||||
($cmp($this, IMAGETYPE_JPEG) && function_exists('imagecreatefromjpeg')) ||
|
($cmp($this, IMAGETYPE_JPEG) && function_exists('imagecreatefromjpeg')) ||
|
||||||
($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'));
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user