Пишу конвертацию файлов .png и gif в webp вот мой код:
<?php
// Directory containing article images
$imagesDir = '/var/www/html/uploads';
// Get all image files
$pngImages = glob($imagesDir . '/*.png');
$gifImages = glob($imagesDir . '/*.gif');
// Merge PNG and GIF images
$images = array_merge($pngImages, $gifImages);
foreach ($images as $image) {
// Check if the image file exists
if (file_exists($image)) {
// Convert the image to WebP format
$webpImage = str_replace(['.png', '.gif'], '.webp', $image);
$escapedImage = escapeshellarg($image);
$escapedWebpImage = escapeshellarg($webpImage);
exec("cwebp -q 80 $escapedImage -o $escapedWebpImage", $output, $returnCode);
if ($returnCode === 0) {
echo "Image converted: $image\n";
} else {
echo "Error converting image: $image\n";
}
} else {
echo "Image not found: $image\n";
}
}
echo "Conversion completed.";
Ошибка только при конвертации gif файлов:
docker run -p 8888:8888 test-task
ArraySaving file '/var/www/html/uploads/How to extract text from PDF offline.webp'
File: /var/www/html/uploads/How to extract text from PDF offline.png
Dimension: 800 x 526
Output: 18016 bytes Y-U-V-All-PSNR 45.55 47.05 47.85 46.09 dB
block count: intra4: 463
intra16: 1187 (-> 71.94%)
skipped block: 1137 (68.91%)
bytes used: header: 296 (1.6%)
mode-partition: 2571 (14.3%)
Residuals bytes |segment 1|segment 2|segment 3|segment 4| total
macroblocks: | 2%| 12%| 25%| 59%| 1650
quantizer: | 27 | 26 | 22 | 16 |
filter level: | 8 | 5 | 4 | 3 |
Image converted: /var/www/html/uploads/How to extract text from PDF offline.png
Saving file '/var/www/html/uploads/how-to.webp'
File: /var/www/html/uploads/how-to.png
Dimension: 800 x 384
Output: 16758 bytes Y-U-V-All-PSNR 44.53 48.26 49.32 45.54 dB
block count: intra4: 272
intra16: 928 (-> 77.33%)
skipped block: 889 (74.08%)
bytes used: header: 316 (1.9%)
mode-partition: 1628 (9.7%)
Residuals bytes |segment 1|segment 2|segment 3|segment 4| total
macroblocks: | 1%| 8%| 13%| 76%| 1200
quantizer: | 27 | 27 | 24 | 17 |
filter level: | 8 | 5 | 5 | 2 |
Image converted: /var/www/html/uploads/how-to.png
Saving file '/var/www/html/uploads/ppt converter.webp'
File: /var/www/html/uploads/ppt converter.png
Dimension: 800 x 420
Output: 9320 bytes Y-U-V-All-PSNR 47.96 48.39 48.94 48.18 dB
block count: intra4: 138
intra16: 1212 (-> 89.78%)
skipped block: 1145 (84.81%)
bytes used: header: 285 (3.1%)
mode-partition: 1262 (13.5%)
Residuals bytes |segment 1|segment 2|segment 3|segment 4| total
macroblocks: | 1%| 2%| 11%| 84%| 1350
quantizer: | 27 | 27 | 24 | 18 |
filter level: | 8 | 6 | 5 | 2 |
Image converted: /var/www/html/uploads/ppt converter.png
Error! Could not process file /var/www/html/uploads/How to convert eBook to PDF.gif
Error! Cannot read input picture file '/var/www/html/uploads/How to convert eBook to PDF.gif'
Error converting image: /var/www/html/uploads/How to convert eBook to PDF.gif
Error! Could not process file /var/www/html/uploads/How to extract text from PDF online.gif
Error! Cannot read input picture file '/var/www/html/uploads/How to extract text from PDF online.gif'
Error converting image: /var/www/html/uploads/How to extract text from PDF online.gif
Conversion completed.
Нужно сделать так, чтобы в момент запуска скрипта, преобразовывались изображения статей *.jpg, *.jpeg, *.png в веб-формат (с заменой URL изображения). У меня есть ещё статьи в формате md
Как можно преобразовать gif, в чём может быть проблема?