Skip to content

Instantly share code, notes, and snippets.

@Mrjavaci
Created January 27, 2022 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mrjavaci/66e36b0d2137419242e2b4f1c709b642 to your computer and use it in GitHub Desktop.
Save Mrjavaci/66e36b0d2137419242e2b4f1c709b642 to your computer and use it in GitHub Desktop.
PHP Convert and Compress Webp All Image Files (Recursive) in Folder
<?php
function getDirContents($dir, &$results = array()) {
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$results[] = $path;
} else if ($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}
return $results;
}
foreach(getDirContents("./") as $imagePath){
$exp = explode(".",$imagePath);
if($exp[count($exp) - 1 ] == "png" || $exp[count($exp) - 1 ] == "jpg" ){
echo $imagePath."\n";
exec('cwebp -m 6 -pass 10 -mt -lossless '.$imagePath.' -o '.$exp[0].'.webp');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment