Created
January 27, 2022 15:10
-
-
Save Mrjavaci/66e36b0d2137419242e2b4f1c709b642 to your computer and use it in GitHub Desktop.
PHP Convert and Compress Webp All Image Files (Recursive) in Folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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