I found a way to save images in WebP format. I searched EVERYWHERE and could not find ANYTHING about this, so I had to do it by my self. I had to do this to improve the speed of my website. If you follow this method you will be able to save images as webP and serve them as such. This is verey early stage and rough, implement with caution.
Prerequisites:
-you need to be running on php 7.1
- you need to have ImageMagic installed on your server.
- you need to have opencart 3.0(not required, but this is my version so I dont know about other versions).
The How:
- find this line in filemanager.php:
Code: Select all
// Get files
$files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
Code: Select all
// Get files
$files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF,svg,SVG,webp}', GLOB_BRACE);
Code: Select all
if (!$json) {
move_uploaded_file($file['tmp_name'], $directory . '/' . $filename);
}
Code: Select all
if (!$json) {
move_uploaded_file($file['tmp_name'], $directory . '/' . $filename);
$dir = 'image/catalog/products/' .$target_file .'/'.$filename;
$im = new Imagick($dir);
$im->writeImage(substr_replace($dir , 'webp', strrpos($dir , '.') +1));
unlink($dir);
}
Code: Select all
elseif ($extension == 'png') {
imagepng($this->image, $file);
} elseif ($extension == 'gif') {
imagegif($this->image, $file);
}
imagedestroy($this->image);
Code: Select all
elseif ($extension == 'png') {
imagepng($this->image, $file);
} elseif ($extension == 'gif') {
imagegif($this->image, $file);
}elseif ($extension == 'webp') {
imagewebp($this->image, $file);
}
imagedestroy($this->image);
That's it, I changed these lines and now every save to the website will save into webP. Please be careful when applying this, we need more developers to try this out and tweak it before it is safe for everyone. Also, be sure to clear your cache and browser cache and every other place that an image could be cashed it before saying that it did not work for you.
Let me know if this works for you or if it works for other versions.
Cheers.