png与jpg图片必须是Photoshop导出web原图php合并jpg,且不能用第三软件或web压缩php合并jpg,否则会出错。
<?php
$dir = __DIR__ . '/seomuyun.com/';
imgtype($dir);
function imgtype($dir)
{
$dir = rtrim($dir, '/') . '/';
$hd = opendir($dir);
while ($hf = readdir($hd)) {
if ($hf == '.' || $hf == '..') {
continue;
}
turnType($dir.$hf);
}
}
function turnType($file)
{
if (is_file($file)) {
$ext = pathinfo($file, PATHINFO_EXTENSION);;
if ($ext == 'jpeg' || $ext == 'jpg' || $ext == 'png') {
$newpic = rtrim($file, $ext) . 'webp';
if ($ext == 'jpg') {
$ext = 'jpeg';
}
$funName = 'imagecreatefrom' . $ext;
$hImg = $funName($file);
imagewebp($hImg, $newpic);
imagedestroy($hImg);
}
}
}