User Contributed Notes 2 notes

WebP is a great file format, but it's basically supported only by Chrome. For WebP files with transparency it's necessary to have PNG fallback for other browsers (otherwise it won't work in iOS, Firefox, IE, etc.).

Regular truecolor PNG with alpha gives pretty large files, but there's a special smaller PNG file variant that can be created by pngquant - a command line utility.

If you have pngquant 1.8 on your server (just get package from official pngquant website), then you can create small fallback images (with quality better than from PHP's libgd):

// '-' makes it use stdout, required to save to $compressed_png_content variable // '<' makes it read from the given file path // escapeshellarg() makes this safe to use with any path$compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg( $path_to_png_file));

if (!$compressed_png_content) { throw new Exception("Conversion to compressed PNG failed. Is pngquant 1.8+ installed on the server?"); }