Created
September 4, 2022 18:17
-
-
Save ahmedch1/120ceb180a81c04ab1303fb17547fbed to your computer and use it in GitHub Desktop.
Load Webp Version if exists
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 | |
// directory | |
$directory=__DIR__ . '\gif'; | |
function replace_extension($filename, $extension) { | |
if (($pos = strrpos($filename , '.')) !== false) { | |
$filename = substr($filename, 0, $pos); | |
} | |
return $filename . '.' . $extension; | |
} | |
function webpImage($source, $quality = 100, $removeOld = false) | |
{ | |
$dir = pathinfo($source, PATHINFO_DIRNAME); | |
$name = pathinfo($source, PATHINFO_FILENAME); | |
$destination = $dir . DIRECTORY_SEPARATOR . $name . '.webp'; | |
$info = getimagesize($source); | |
$isAlpha = false; | |
if ($info['mime'] == 'image/jpeg') | |
$image = imagecreatefromjpeg($source); | |
elseif ($isAlpha = $info['mime'] == 'image/gif') { | |
$image = imagecreatefromgif($source); | |
} elseif ($isAlpha = $info['mime'] == 'image/png') { | |
$image = imagecreatefrompng($source); | |
} else { | |
return $source; | |
} | |
if ($isAlpha) { | |
imagepalettetotruecolor($image); | |
imagealphablending($image, true); | |
imagesavealpha($image, true); | |
} | |
imagewebp($image, $destination, $quality); | |
if ($removeOld) | |
unlink($source); | |
return $destination; | |
} | |
//check if webp is supported | |
function webpsupport() | |
{ | |
if (strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== false) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function displayImage($image=''){ | |
if(webpsupport()===true){ | |
//serve web image | |
webpImage($image,70,false); | |
$webpimage=replace_extension($image,'webp'); | |
echo '<img src=' . $webpimage . '>'; | |
}else{ | |
//serve image as it is format | |
} | |
} | |
//return all images in specific folder as array type | |
function getDir4JpgR($directory) { | |
$allimages=[]; | |
if ($handle = opendir($directory)) { | |
while (false !== ($entry = readdir($handle))) { | |
if($entry != "." && $entry != "..") { | |
$str1 = "$entry"; | |
if(preg_match("/\.jpg$/i", $entry) || (preg_match("/\.jpeg$/i", $entry)) || (preg_match("/\.png$/i", $entry)) || (preg_match("/\.gif$/i", $entry))) { | |
$allimages[]=$str1; | |
} else { | |
if(is_dir($str1)) { | |
getDir4JpgR($str1); | |
} | |
} | |
} | |
} | |
closedir($handle); | |
} | |
return $allimages; | |
} | |
// subfolder __DIR__ . '/subfolder' | |
function getSubFolders($directory=__DIR__) | |
{ | |
$subfolders=[]; | |
$allFiles = scandir($directory); // Or any other directory | |
$files = array_diff($allFiles, array('.', '..')); | |
foreach ($files as $file) { | |
if (!strpos($file, '.', 0)) { | |
$subfolders[]=$file; | |
} | |
} | |
return $subfolders; | |
} | |
//get array containing all images in the folder | |
$allimagesinfolder=getDir4JpgR($directory); | |
// | |
////optimize all images | |
foreach ($allimagesinfolder as $item){ | |
displayImage($directory . '\\' . $item ,70,false); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment