#1 August 9, 2021 9:18am

joeshu
Member
Registered: June 13, 2018
Posts: 16

WebP image support

Hello,

We'd like to being using one of the next-gen file formats like WebP.  Does BigTree have native support, or the ability to add this for image uploads?

If not what is a good way to override (if possible) the upload function to allow it.

Thanks,
Joe

Offline

#2 August 9, 2021 11:54am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 978

Re: WebP image support

The trouble with WebP support is that delivering WebP relies on user agent sniffing if you still want to support IE11 and older versions of macOS. Only the most recent macOS supports Safari w/ WebP, unfortunately. For that reason, it's not baked into BigTree at the moment as it's best left up to CDNs to process automatically and serve next-gen formats.

If you were interested in doing strictly WebP delivery anyway, I'd create an override of the BigTreeStorage class (making a copy in /custom/inc/bigtree/apis/storage.php). There's a "convertJPEG" method that is called on all image uploads that you can change to always convert images to WebP instead of JPEG. You'll just want to remove the config and image type checks and then use imagewebp (and switch the extension to .webp).

I haven't tested this, but here's a quick method body change:

// Try to figure out what this file is
list($iwidth,$iheight,$itype,$iattr) = @getimagesize($file);

if ($itype == IMAGETYPE_PNG || $itype == IMAGETYPE_JPEG) {
	// See if this PNG has any alpha channels, if it does we're not doing a conversion.
	$alpha = ($itype == IMAGETYPE_PNG) ? ord(@file_get_contents($file,null,null,25,1)) : null;
	
	if ($alpha != 4 && $alpha != 6) {
		// Convert the image to WebP
		if ($itype == IMAGETYPE_PNG) {
			$source = imagecreatefrompng($file);
		} else if ($itype == IMAGETYPE_JPEG) {
			$source = imagecreatefromjpeg($file);
		}

		imagewebp($source,$file,$bigtree["config"]["image_quality"]);
		imagedestroy($source);

		$extension = pathinfo($name, PATHINFO_EXTENSION);
		$name = substr($name, 0, -1 * strlen($extension))."webp";
	}
}

return $name;

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.