Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert png file to webp file

I need to convert an image (png) to (webp) file.

After uploading a png file, the webp image has been generated, but the webp file didn't copy the transparency of a png file, instead it creates a black background.

This is my php code:

$type = wp_check_filetype($file, null);
$ext = $type['ext'];
if ($ext === 'png') {
    $im = imagecreatefrompng($file);
    imagepalettetotruecolor($im);
    $webp = imagewebp($im, str_replace('png', 'webp', $file));
}
imagedestroy($im);

The version of PHP is 5.6

like image 303
Jordan Lipana Avatar asked Sep 02 '19 13:09

Jordan Lipana


People also ask

How do I convert a file to WebP?

Right click on an image file or a folder containing a number of images files, and then click Convert to WebP. The Converting Images to WebP dialog opens. The default settings depend on the minSdkVersion setting for the current module. Select either lossy or lossless encoding.

Can Photoshop convert PNG to WebP?

Once installed, start Photoshop and open an image. Once opened, you can export an image to WebP through the Save As… dialog. At the bottom of the dialog where you choose a format, you'll notice two options: WebP, and WebP Lossless.

How do I save a PNG as a transparent WebP?

Right-click on the WEBP image and select the new option Save Image As PNG. That's it, now you can save the image in PNG format.

Is WebP and PNG same?

WebP is a modern image format that provides superior lossless and lossy compression for images on the web. Using WebP, webmasters and web developers can create smaller, richer images that make the web faster. WebP lossless images are 26% smaller in size compared to PNGs.


1 Answers

Tested on 7.3.0 -- works.

DISCLAIMER: May only work on later or some PHP versions.

Only tested on 5.6.15 (didn't work, black background) and 7.3.0 (worked, transparent background).

Here's the code:

// get png in question

$pngimg = imagecreatefrompng($file);

// get dimens of image

$w = imagesx($pngimg);
$h = imagesy($pngimg);;

// create a canvas

$im = imagecreatetruecolor ($w, $h);
imageAlphaBlending($im, false);
imageSaveAlpha($im, true);

// By default, the canvas is black, so make it transparent

$trans = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefilledrectangle($im, 0, 0, $w - 1, $h - 1, $trans);

// copy png to canvas

imagecopy($im, $pngimg, 0, 0, 0, 0, $w, $h);

// lastly, save canvas as a webp

imagewebp($im, str_replace('png', 'webp', $file));

// done

imagedestroy($im);  

Edit 1. *** PROOF

The PHP GD library relies on the libgd library.

Link:

https://github.com/libgd/libgd

Relevant code on saves (file: gd_webp.c), Excerpt showing respect of Alpha channel when present:

            c = im->tpixels[y][x];
            a = gdTrueColorGetAlpha(c);
            if (a == 127) {
                a = 0;
            } else {
                a = 255 - ((a << 1) + (a >> 6));
            }
            *(p++) = gdTrueColorGetRed(c);
            *(p++) = gdTrueColorGetGreen(c);
            *(p++) = gdTrueColorGetBlue(c);
            *(p++) = a;

In regards to static int _gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality)

The PHP code I presented relies on the fact that alpha is indeed respected in the GD library and as such works if tested in later PHP version than you are using, specifically I tested in 7.3.0 but may work in early releases after your version.

like image 111
GetSet Avatar answered Oct 20 '22 12:10

GetSet