我正在编写用于压缩图像大小的代码,如下所示。
<?php
function compress_image($source_url,$destination_url,$quality)
{
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source_url);
imagejpeg($image,$quality);
} elseif ($info['mime'] == 'image/gif') {
$image = imagecreatefromgif($source_url);
imagegif($image,$quality);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($source_url);
imagepng($image,$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时,它会显示相同的结果,就像我将图像质量80设置为大小为'50,69, 650字节”将压缩我的图像大小为“ 40、34、652字节”,但是当我设置质量10时,它将给我相同的图像大小“ 40、34、652字节”。
我不知道为什么会这样。
有人可以帮我吗?