$dir = '/var/www/html/uploads';
$exts = ['png', 'gif', 'jpg', 'jpeg'];
$images = new DirectoryIterator($dir);
foreach ($images as $image) {
if ($image->isFile() && in_array($image->getExtension(), $exts, true)) {
$name = explode('.', $image->getFilename());
$webp = new Imagick($dir . DIRECTORY_SEPARATOR . $image);
$webp->setImageFormat('webp');
$webp->writeImage($dir . DIRECTORY_SEPARATOR . $name[0] . '.webp');
//unlink($dir . DIRECTORY_SEPARATOR . $image->getFilename()); // удаляем после обработки
}
}
$webpImage = str_replace($allowedExtensions, 'webp', $image);
FROM php:7.4-cli
...
CMD [ "php", "./convertImages.php" ]