08/23/2025
PNG and JPG to WebP in PHP: Quality 82, Keep the Smaller
For a handful of files, use the image converter on this site — it runs in your browser, nothing is uploaded, and there is no script to deploy. The rest of this page is for a folder full of them on a server.
<?php
if (!extension_loaded('gd')) {
exit("The GD extension is not enabled. Enable it in php.ini.\n");
}
$directory = '.';
$quality = 82; // see the note below -- 100 is not what you want
$converted = $skipped = 0;
foreach (scandir($directory) as $file) {
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (!in_array($extension, ['jpg', 'jpeg', 'png'], true)) {
continue;
}
$output = pathinfo($file, PATHINFO_FILENAME) . '.webp';
if (file_exists($output)) {
continue;
}
if ($extension === 'png') {
$image = imagecreatefrompng($file);
imagepalettetotruecolor($image);
imagealphablending($image, true);
imagesavealpha($image, true);
} else {
$image = imagecreatefromjpeg($file);
}
if ($image === false) {
continue;
}
imagewebp($image, $output, $quality);
imagedestroy($image);
// KEEP THE SMALLER FILE. WebP does not win on every image.
if (filesize($output) >= filesize($file)) {
unlink($output);
$skipped++;
echo "kept original: {$file}\n";
} else {
$converted++;
$saved = round((1 - filesize($output) / filesize($file)) * 100);
echo "converted {$file} -> {$output} ({$saved}% smaller)\n";
}
}
echo "\n{$converted} converted, {$skipped} left alone.\n";
Run it from the folder you want converted. It writes alongside the originals and deletes nothing you had before.
What the script did on four test images
Tested on PHP 8.3.36 with the bundled GD (WebP support on), running the script above unchanged on four images generated for the test: a noisy 1200x800 photo-like JPEG, a flat two-color PNG logo with a transparent background, a 16x16 PNG icon, and a 1280x720 flat-background screenshot. Sizes are bytes.
| Image | Original | WebP at 82 | WebP at 100 | Script at 82 |
|---|---|---|---|---|
| photo.jpg | 368,154 | 267,764 | 559,566 | converted, 27% smaller |
| logo.png | 1,852 | 2,604 | 3,436 | kept original |
| icon.png | 104 | 64 | 72 | converted, 38% smaller |
| screenshot.png | 10,582 | 24,948 | 37,002 | kept original |
Three things to take from it. At 100 the photo's WebP was 52% larger than its JPEG, so the same script at quality 100 kept the JPEG and converted only the icon. Two of the four files came out larger as WebP even at 82, and the size check is the only reason the script did not make them worse. And the transparent corner of the logo stayed fully transparent after the conversion (alpha 127 in GD's scale).
These are synthetic images, so your ratios will differ, and PNGs from a real tool such as a screenshot utility are often less compressible than the flat ones used here. Run the script on a copy of your own folder and read the per-file output before trusting any average.
Quality 100 is the wrong setting, and it is the common mistake
PHP's manual describes the parameter as ranging from "0 (worst quality,
smaller file) to 100 (best quality, biggest file)", and says that passing
-1 uses a default of 80.
That default is the useful hint. At 100 the encoder is told to preserve everything it can, and the result is frequently larger than the JPEG you started with — which defeats the entire purpose. Somewhere in the high 70s to mid 80s is where WebP earns its reputation: visually indistinguishable on photographs, and meaningfully smaller.
82 is a reasonable starting point. Look at the output on a few of your own images before running it over a thousand.
Why the size check matters
WebP is not smaller than everything. It reliably beats JPEG on photographs and PNG on flat graphics, and it loses on:
- an already-optimized PNG or JPEG, where the previous encoder has already done this work;
- very small images, where the container overhead is a large fraction of the file;
- screenshots of large flat areas, which PNG compresses extremely well.
A conversion loop with no size check will happily make those pages
heavier while reporting success. Comparing filesize() and deleting the
loser costs one line and removes the whole question.
Converting is only half of it
A .webp file next to a .jpg changes nothing until something serves it.
You need either markup that offers both:
<picture>
<source srcset="photo.webp" type="image/webp">
<img src="photo.jpg" alt="..." width="1200" height="800">
</picture>
...or a build step that emits WebP and rewrites the references for you, which is what most static site generators do. Converting the folder and leaving the HTML pointing at the old files is a common way to end up with twice the images and none of the benefit.
This post goes from PNG and JPG to WebP. For the other direction, WebP back to JPG or PNG, see WebP to JPG in PHP.
Questions this keeps raising
What quality should I use for WebP?
Somewhere around 80. PHP's imagewebp uses 80 as its own default when you pass -1, and the manual describes 100 as "best quality, biggest file" rather than as lossless. At 100 the output is often larger than the JPEG it replaced, which is the opposite of the point.
Does this preserve PNG transparency?
Yes, provided you call imagepalettetotruecolor, imagealphablending and imagesavealpha before writing, which the script above does. Skip them and a transparent background comes out black.
Why did WebP make my file bigger?
Because it does not win on everything. Already-optimized images, very small images and screenshots of large flat areas can all come out larger. That is why the script compares the two sizes and keeps the original when WebP loses.
Do I still need the original JPEG or PNG?
Keep it. Browser support for WebP is effectively universal now, but the original is your source: re-encoding from a WebP later compounds the loss. Serve the WebP, archive the original.