Php 不同质量的图像压缩问题
我正在制作压缩图像大小的代码,如下所示Php 不同质量的图像压缩问题,php,image-compression,Php,Image Compression,我正在制作压缩图像大小的代码,如下所示 <?php function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if ($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source_url); imagejpeg($imag
<?php
function compress_image($source_url, $destination_url, $quality)
{
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source_url);
imagejpeg($image, $destination_url, $quality);
} elseif ($info['mime'] == 'image/gif') {
$image = imagecreatefromgif($source_url);
imagegif($image, $destination_url, $quality);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($source_url);
imagepng($image, $destination_url, $quality);
}
return $destination_url;
}
if ($_POST) {
if ($_FILES["file"]["error"] > 0) {
$error = $_FILES["file"]["error"];
} else if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) {
$url = $_FILES["file"]["name"];
$temp_file = $_FILES["file"]["tmp_name"];
$filename = compress_image($temp_file, $url, 80);
rename($filename, 'image/' . $filename);
$location = "image/" . $url;
if (file_exists($location)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($location));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($location));
ob_clean();
flush();
readfile($location);
exit;
}
} else {
$error = "Uploaded image should be jpg or gif or png";
}
}
?>
压缩图像大小很好,但当我将图像质量(如80)更改为50、30、20时,它会显示相同的结果,例如,如果我为大小为“50、69、650字节”的图像设置图像质量80,它会将图像大小压缩为“40、34、652字节”,但当我设置质量10时,它会给我相同的图像大小“40、34、652字节”
我不知道为什么会这样
有人能帮我吗