1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| <?php
function convertToWebp($source, $destination, $quality = 60) { $info = getimagesize($source);
if ($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source); } elseif ($info['mime'] == 'image/gif') { return false; } else { return false; } $width = imagesx($image); $height = imagesy($image); $maxWidth = 2500; $maxHeight = 1600; if ($width > $maxWidth || $height > $maxHeight) { $ratio = min($maxWidth / $width, $maxHeight / $height); $newWidth = round($width * $ratio); $newHeight = round($height * $ratio); $newImage = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imagedestroy($image); $image = $newImage; } $result = imagewebp($image, $destination, $quality); imagedestroy($image); gc_collect_cycles(); return $result; }
function convertPngWithImagick($source, $destination, $quality = 60) { try { $image = new Imagick($source);
if ($image->getImageAlphaChannel()) { $image->setImageBackgroundColor(new ImagickPixel('transparent')); $image->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE); $image = $image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); }
$image->setImageFormat('webp'); $image->setImageCompressionQuality($quality);
$width = $image->getImageWidth(); $height = $image->getImageHeight(); $maxWidth = 2500; $maxHeight = 1600;
if ($width > $maxWidth || $height > $maxHeight) { $ratio = min($maxWidth / $width, $maxHeight / $height); $newWidth = round($width * $ratio); $newHeight = round($height * $ratio); $image->resizeImage($newWidth, $newHeight, Imagick::FILTER_LANCZOS, 1); }
$result = $image->writeImage($destination); $image->clear(); $image->destroy(); return $result; } catch (Exception $e) { logMessage('Imagick转换PNG失败: ' . $e->getMessage()); return false; } }
function convertGifToWebp($source, $destination, $quality = 60) { try { $image = new Imagick(); $image->readImage($source); $image = $image->coalesceImages(); foreach ($image as $frame) { $frame->setImageFormat('webp'); $frame->setImageCompressionQuality($quality); } $image = $image->optimizeImageLayers(); $result = $image->writeImages($destination, true); $image->clear(); $image->destroy(); return $result; } catch (Exception $e) { logMessage('GIF转换WebP失败: ' . $e->getMessage()); return false; } }
function processImageCompression($fileMimeType, $newFilePath, $newFilePathWithoutExt, $quality) { $convertSuccess = true; $finalFilePath = $newFilePath;
if ($fileMimeType === 'image/png') { $convertSuccess = convertPngWithImagick($newFilePath, $newFilePathWithoutExt . '.webp', $quality); if ($convertSuccess) { $finalFilePath = $newFilePathWithoutExt . '.webp'; unlink($newFilePath); } } elseif ($fileMimeType === 'image/gif') { $convertSuccess = convertGifToWebp($newFilePath, $newFilePathWithoutExt . '.webp', $quality); if ($convertSuccess) { $finalFilePath = $newFilePathWithoutExt . '.webp'; unlink($newFilePath); } } elseif ($fileMimeType !== 'image/webp' && $fileMimeType !== 'image/svg+xml') { $convertSuccess = convertToWebp($newFilePath, $newFilePathWithoutExt . '.webp', $quality); if ($convertSuccess) { $finalFilePath = $newFilePathWithoutExt . '.webp'; unlink($newFilePath); } }
return [$convertSuccess, $finalFilePath]; } ?>
|