This is fun. This works really well, so if you just need to convert images to webp, this works perfectly. I tested it a few times and had no issues. Backup everything as always. If you want this to update images, IT NEEDS TO BE IN THE IMAGES FOLDER THAT YOU WANT TO ALTER OR EDIT. for example mysite.burp/images/webpconverter.php. Make sure this file is in the folder you want the images converted, then run it, and all images are now webp....yaaayyyy
<?php
// Convert images in the current folder to WebP format
// WARNING: This will DELETE the original images after conversion. MAKE A BACKUP FIRST!
// Delete this file after use. Do not leave it on a public server.
// This uses the gd library, which must be installed with WebP support.
while (ob_get_level()) {
ob_end_flush();
}
ob_implicit_flush(true);
set_time_limit(0);
$quality = 85;
$allowed = ['jpg', 'jpeg', 'png', 'bmp', 'gif'];
$confirmed = isset($_POST['confirm']) && $_POST['confirm'] === 'yes';
function is_animated_gif(string $file): bool
{
if (!($fh = fopen($file, 'rb'))) {
return false;
}
$frames = 0;
while (!feof($fh) && $frames < 2) {
$chunk = fread($fh, 1024);
$frames += preg_match_all('#\x00\x21\xF9\x04#', $chunk);
}
fclose($fh);
return $frames > 1;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebP Folder Converter</title>
<style>
body {
font-family: system-ui, sans-serif;
background: #111;
color: #ddd;
padding: 20px;
}
button {
padding: 10px 16px;
font-size: 16px;
background: #c0392b;
border: none;
color: #fff;
cursor: pointer;
}
button:hover {
background: #e74c3c;
}
pre {
background: #000;
padding: 10px;
overflow: auto;
max-height: 60vh;
}
.warning { color: #f39c12; }
.success { color: #2ecc71; }
.spinner {
display: inline-block;
width: 12px;
height: 12px;
border: 2px solid #444;
border-top-color: #2ecc71;
border-radius: 50%;
animation: spin 0.8s linear infinite;
margin-right: 8px;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<h2>WebP Converter (Current Folder)</h2>
<p class="warning">
⚠ This will permanently delete original images after conversion.<br>
Animated GIFs will lose animation.
</p>
<?php if (!$confirmed): ?>
<form method="post">
<p>Folder: <strong><?= htmlspecialchars(__DIR__) ?></strong></p>
<button type="submit" name="confirm" value="yes">
Convert images to WebP
</button>
</form>
<?php else: ?>
<p id="workingMsg"><span class="spinner"></span><strong>Working… do not close this page</strong></p>
<pre>
<?php
$files = scandir(__DIR__);
$count = 0;
foreach ($files as $file) {
if (!is_file($file)) {
continue;
}
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (!in_array($ext, $allowed, true)) {
continue;
}
$count++;
echo "Processing: $file\n";
@ob_flush(); @flush();
$webp = pathinfo($file, PATHINFO_FILENAME) . '.webp';
if (file_exists($webp)) {
echo "[SKIP] $file (webp exists)\n\n";
@ob_flush(); @flush();
continue;
}
if ($ext === 'gif' && is_animated_gif($file)) {
echo "[SKIP] $file (animated GIF)\n\n";
@ob_flush(); @flush();
continue;
}
switch ($ext) {
case 'jpg':
case 'jpeg':
$img = @imagecreatefromjpeg($file);
break;
case 'png':
$img = @imagecreatefrompng($file);
if ($img) {
imagepalettetotruecolor($img);
imagealphablending($img, true);
imagesavealpha($img, true);
}
break;
case 'bmp':
$img = @imagecreatefrombmp($file);
break;
case 'gif':
$img = @imagecreatefromgif($file);
if ($img) {
imagepalettetotruecolor($img);
}
break;
default:
$img = false;
}
if (!$img) {
echo "[FAIL] $file (read error)\n\n";
@ob_flush(); @flush();
continue;
}
if (imagewebp($img, $webp, $quality)) {
unlink($file);
echo "[OK] $file → $webp\n\n";
} else {
echo "[FAIL] $file (convert error)\n\n";
}
imagedestroy($img);
@ob_flush(); @flush();
}
echo "Done. Files processed: $count\n";
?>
</pre>
<script>
document.addEventListener("DOMContentLoaded", function() {
var working = document.getElementById('workingMsg');
if (working) {
working.innerHTML = '<strong>✅ CONVERSION COMPLETE. YOU CAN CLOSE THIS PAGE NOW</strong>';
}
});
</script>
<p class="success">✅ Conversion complete</p>
<?php endif; ?>
</body>
</html>
TTv3 Micro Edition
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Last edited by MicroMonkey on 2026-02-19 20:23:52