Post by anas_daved » Tue Feb 26, 2019 12:52 pm

OK guys,
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);
and change it to :

Code: Select all

// Get files
$files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF,svg,SVG,webp}', GLOB_BRACE);
-find the code for upload() in filemanager.php and change this

Code: Select all

 if (!$json) {
                    move_uploaded_file($file['tmp_name'], $directory . '/' . $filename);
                    }
to this :

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);
                }
- find the save() method in system/library/image.php and cahnge this :

Code: Select all

elseif ($extension == 'png') {
				imagepng($this->image, $file);
			} elseif ($extension == 'gif') {
				imagegif($this->image, $file);
			}
			imagedestroy($this->image);
to this :

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);
Conclusion:

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.

Newbie

Posts

Joined
Mon Feb 25, 2019 9:33 pm

Post by straightlight » Sat Mar 02, 2019 8:57 pm

An additional validation can be found here as for PHP v7.1.0: viewtopic.php?f=110&t=143082#p748232 .

Note: Not a replacement solution, simply an addition to it.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by anas_daved » Sun Mar 03, 2019 1:44 pm

straightlight wrote:
Sat Mar 02, 2019 8:57 pm
An additional validation can be found here as for PHP v7.1.0: viewtopic.php?f=110&t=143082#p748232 .

Note: Not a replacement solution, simply an addition to it.
Excellent catch, great addition. Thank you for enhancing this. I'll update the extension I did on this.

Newbie

Posts

Joined
Mon Feb 25, 2019 9:33 pm

Post by haxcop » Tue Sep 24, 2019 10:58 pm

I have a question:

I see this xml file uploaded by @anas_dawood works pretty well (Thanks) when uploading the files manually using the UI of OpenCart.
But when using an automated tool to import the catalog with images via URL or anything else it won't work, have someone already have a solution to iT?

New member

Posts

Joined
Tue Jan 14, 2014 9:38 pm
Location - Dublin
Who is online

Users browsing this forum: No registered users and 6 guests