<?php $dir = $_GET['dir']; $compression = $_GET['compression']; // 0 - самое худшее; 100 - наилучшее $update = $_GET['update']; function AllFiles($dir) { $files = glob($dir."/*.*",GLOB_NOSORT); do{ $dir = $dir."/*"; $files2 = glob($dir."/*.*",GLOB_NOSORT); $files = array_merge($files,$files2); }while(sizeof($files2)>0); return $files; } function getExtension($filename) { $format = preg_replace('/^.*\.(.*)$/U', '$1', $filename); $file = basename($filename, '.'.$format); return array('name' => $file, 'format' => $format, 'path' => $filename, 'dir' => dirname($filename), 'new_file' => dirname($filename).'/'.$file.'.'.'webp'); } $jpeg_formats = ['jpg', 'jpeg']; foreach (AllFiles($dir) as $filename){ $fileinfo = getExtension($filename); if ($fileinfo['format'] == 'png') { if ($update == 'true'){ $img = imagecreatefrompng($filename); imagepalettetotruecolor($img); imagealphablending($img, true); imagesavealpha($img, true); imagewebp($img, $fileinfo['new_file'], $compression); imagedestroy($img); $file = $fileinfo['path']; echo "$file - complete<br>"; } else { if (file_exists($fileinfo['new_file'])){ $file = $fileinfo['path']; echo "$file - exists<br>"; } else { $img = imagecreatefrompng($filename); imagepalettetotruecolor($img); imagealphablending($img, true); imagesavealpha($img, true); imagewebp($img, $fileinfo['new_file'], $compression); imagedestroy($img); $file = $fileinfo['path']; echo "$file - complete<br>"; } } } elseif (in_array($fileinfo['format'], $jpeg_formats)) { if ($update == 'true') { $pngfilepath = $fileinfo['dir'] . '/' . $fileinfo['name'] . '.' . 'png'; imagepng(imagecreatefromstring(file_get_contents($filename)), $pngfilepath); $img = imagecreatefrompng($pngfilepath); imagepalettetotruecolor($img); imagealphablending($img, true); imagesavealpha($img, true); imagewebp($img, $fileinfo['new_file'], $compression); imagedestroy($img); $file = $fileinfo['path']; unlink($pngfilepath); echo "$file - complete<br>"; } else { if (file_exists($fileinfo['new_file'])){ $file = $fileinfo['path']; echo "$file - exists<br>"; } else { $pngfilepath = $fileinfo['dir'] . '/' . $fileinfo['name'] . '.' . 'png'; imagepng(imagecreatefromstring(file_get_contents($filename)), $pngfilepath); $img = imagecreatefrompng($pngfilepath); imagepalettetotruecolor($img); imagealphablending($img, true); imagesavealpha($img, true); imagewebp($img, $fileinfo['new_file'], $compression); imagedestroy($img); $file = $fileinfo['path']; unlink($pngfilepath); echo "$file - complete<br>"; } } } } ?> PHP <?php $dir = $_GET['dir']; $compression = $_GET['compression']; // 0 - самое худшее; 100 - наилучшее $update = $_GET['update']; function AllFiles($dir) { $files = glob($dir."/*.*",GLOB_NOSORT); do{ $dir = $dir."/*"; $files2 = glob($dir."/*.*",GLOB_NOSORT); $files = array_merge($files,$files2); }while(sizeof($files2)>0); return $files; } function getExtension($filename) { $format = preg_replace('/^.*\.(.*)$/U', '$1', $filename); $file = basename($filename, '.'.$format); return array('name' => $file, 'format' => $format, 'path' => $filename, 'dir' => dirname($filename), 'new_file' => dirname($filename).'/'.$file.'.'.'webp'); } $jpeg_formats = ['jpg', 'jpeg']; foreach (AllFiles($dir) as $filename){ $fileinfo = getExtension($filename); if ($fileinfo['format'] == 'png') { if ($update == 'true'){ $img = imagecreatefrompng($filename); imagepalettetotruecolor($img); imagealphablending($img, true); imagesavealpha($img, true); imagewebp($img, $fileinfo['new_file'], $compression); imagedestroy($img); $file = $fileinfo['path']; echo "$file - complete<br>"; } else { if (file_exists($fileinfo['new_file'])){ $file = $fileinfo['path']; echo "$file - exists<br>"; } else { $img = imagecreatefrompng($filename); imagepalettetotruecolor($img); imagealphablending($img, true); imagesavealpha($img, true); imagewebp($img, $fileinfo['new_file'], $compression); imagedestroy($img); $file = $fileinfo['path']; echo "$file - complete<br>"; } } } elseif (in_array($fileinfo['format'], $jpeg_formats)) { if ($update == 'true') { $pngfilepath = $fileinfo['dir'] . '/' . $fileinfo['name'] . '.' . 'png'; imagepng(imagecreatefromstring(file_get_contents($filename)), $pngfilepath); $img = imagecreatefrompng($pngfilepath); imagepalettetotruecolor($img); imagealphablending($img, true); imagesavealpha($img, true); imagewebp($img, $fileinfo['new_file'], $compression); imagedestroy($img); $file = $fileinfo['path']; unlink($pngfilepath); echo "$file - complete<br>"; } else { if (file_exists($fileinfo['new_file'])){ $file = $fileinfo['path']; echo "$file - exists<br>"; } else { $pngfilepath = $fileinfo['dir'] . '/' . $fileinfo['name'] . '.' . 'png'; imagepng(imagecreatefromstring(file_get_contents($filename)), $pngfilepath); $img = imagecreatefrompng($pngfilepath); imagepalettetotruecolor($img); imagealphablending($img, true); imagesavealpha($img, true); imagewebp($img, $fileinfo['new_file'], $compression); imagedestroy($img); $file = $fileinfo['path']; unlink($pngfilepath); echo "$file - complete<br>"; } } } } ?> На входе: index.php?dir=scr/img&compression=100&update=true На выходе scr/img/imgae.png - complete dir - папка входа compression - сжатие(от 0 до 100, где 0 - наихудшее) update - обновлять ранее созданные файлы(true/false)