Created
July 15, 2023 13:37
-
-
Save kubiqsk/0daa003b99d46bd54e90557f575076f0 to your computer and use it in GitHub Desktop.
PHP snippet to convert all WebP images back to original formats - works only with WordPress plugin Images to WebP
This file contains hidden or 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 convertWebPImages( $directory = '.' ){ | |
$files = glob( $directory . '/*.webp' ); | |
foreach( $files as $file ){ | |
$original_file = substr( $file, 0, -5 ); | |
$original_ext = pathinfo( $original_file, PATHINFO_EXTENSION ); | |
if( ! file_exists( $original_file ) ){ | |
$webp = imagecreatefromwebp( $file ); | |
if( $webp !== false ){ | |
switch( $original_ext ){ | |
case 'jpg': | |
case 'jpeg': | |
imagejpeg( $webp, $original_file, 95 ); | |
break; | |
case 'png': | |
imagepng( $webp, $original_file, 9 ); | |
break; | |
case 'gif': | |
imagegif( $webp, $original_file ); | |
break; | |
} | |
imagedestroy( $webp ); | |
echo '<br>Converted ' . $file; | |
}else{ | |
echo '<br>Can not convert ' . $file; | |
} | |
}else{ | |
echo '<br>Original file exists for ' . $file; | |
} | |
} | |
$directories = glob( $directory . '/*', GLOB_ONLYDIR ); | |
foreach( $directories as $subdirectory ){ | |
convertWebPImages( $subdirectory ); | |
} | |
} | |
convertWebPImages(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment