如何减少图像文件大小而不失去质量或分辨率使用php或任何其他语言


How to reduce Image file size without losing quality or resolution using php or any other language?

我有1mb高清分辨率的图像。我希望文件大小将减少到50% min (512KB)。我已经尝试过使用PHP,但它不适合我。如果有人知道,请帮助我。

像https://tinyjpg.com/这个网站。

$filename = "image.jpg"; // 1 mb image file
header('Content-Type: image/jpeg');
list($width_orig, $height_orig) = getimagesize($filename);
$width = $width_orig;
$height = $height_orig;
$ratio_orig = $width_orig / $height_orig;
if ($width / $height > $ratio_orig) {
 $width = $height * $ratio_orig;
} else {
 $height = $width / $ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, "image_resized.jpg", 100);

function compress_image($source_url, $destination_url, $quality) {

    $info = getimagesize($source_url);
        if ($info['mime'] == 'image/jpeg')
                $image = imagecreatefromjpeg($source_url);
        elseif ($info['mime'] == 'image/gif')
                $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png')
                $image = imagecreatefrompng($source_url);
        imagejpeg($image, $destination_url, $quality);
    return $destination_url;
}

您可能想要查看Trimage或ImageOtim。此外,您可以尝试将图像转换为不同的文件格式;有些格式比其他格式更能压缩某些类型的图像。

除非你的图像中有很多冗余,否则你似乎不太可能在不丢失一些信息的情况下将大小减少50%。