PHP可以使用GD庫來實現圖像的無損壓縮。下面是一個簡單的示例代碼:
<?php
// 原始圖像文件
$source_file = 'input.jpg';
// 目標圖像文件
$target_file = 'output.jpg';
// 打開原始圖像文件
$source = imagecreatefromjpeg($source_file);
// 創建一個空白的目標圖像
$target = imagecreatetruecolor(imagesx($source), imagesy($source));
// 無損壓縮
imagecopy($target, $source, 0, 0, 0, 0, imagesx($source), imagesy($source));
// 保存目標圖像到文件
imagejpeg($target, $target_file);
// 釋放資源
imagedestroy($source);
imagedestroy($target);
echo '圖像無損壓縮完成';
?>
以上代碼會將原始圖像文件input.jpg進行無損壓縮,并保存為output.jpg文件。您可以根據實際需求調整壓縮比例和輸出文件格式。