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 support to Image Manipulation Class #3084

Merged
merged 2 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions system/Images/Handlers/GDHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,17 @@ public function save(string $target = null, int $quality = 90): bool
throw ImageException::forSaveFailed();
}
break;
case IMAGETYPE_WEBP:
if (! function_exists('imagewebp'))
{
throw ImageException::forInvalidImageCreate(lang('images.webpNotSupported'));
}

if (! @imagewebp($this->resource, $target))
{
throw ImageException::forSaveFailed();
}
break;
default:
throw ImageException::forInvalidImageCreate();
}
Expand Down Expand Up @@ -429,6 +440,13 @@ protected function createImage(string $path = '', string $imageType = '')
}

return imagecreatefrompng($path);
case IMAGETYPE_WEBP:
if (! function_exists('imagecreatefromwebp'))
{
throw ImageException::forInvalidImageCreate(lang('images.webpNotSupported'));
}

return imagecreatefromwebp($path);
default:
throw ImageException::forInvalidImageCreate('Ima');
}
Expand Down Expand Up @@ -456,6 +474,9 @@ protected function ensureResource()
case IMAGETYPE_PNG:
$this->resource = imagecreatefrompng($path);
break;
case IMAGETYPE_WEBP:
$this->resource = imagecreatefromwebp($path);
break;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions system/Images/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public function getProperties(bool $return = false)
1 => 'gif',
2 => 'jpeg',
3 => 'png',
3 => 'webp'
nicojmb marked this conversation as resolved.
Show resolved Hide resolved
];

$mime = 'image/' . ($types[$vals[2]] ?? 'jpg');
Expand Down
1 change: 1 addition & 0 deletions system/Language/en/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'gifNotSupported' => 'GIF images are often not supported due to licensing restrictions. You may have to use JPG or PNG images instead.',
'jpgNotSupported' => 'JPG images are not supported.',
'pngNotSupported' => 'PNG images are not supported.',
'webpNotSupported' => 'WEBP images are not supported.',
'fileNotSupported' => 'The supplied file is not a supported image type.',
'unsupportedImageCreate' => 'Your server does not support the GD function required to process this type of image.',
'jpgOrPngRequired' => 'The image resize protocol specified in your preferences only works with JPEG or PNG image types.',
Expand Down