Is it possible to compress an image by its size using PHP? I mean I let the user enter the size of the image in KB and they get the image in that size.
1条回答 默认 最新
H_MZ 2018-04-01 07:17关注You could compress the image of sorts by recreating the image with new dimensions.
$image_name = $_FILES['image']['name']; $image_name_tmp = $_FILES['image']['tmp_name']; $ext = pathinfo($image_name, PATHINFO_EXTENSION); if($ext=='jpg' || $ext=='jpeg' || $ext=='JPG' || $ext=='JPEG') { $src = imagecreatefromjpeg($image_name_tmp); } else if($ext=="png" || $ext=="PNG") { $src = imagecreatefrompng($image_name_tmp); } else { $src = imagecreatefromgif($image_name_tmp); } list($width, $height) = getimagesize($image_name_tmp); $n_width = 320; $n_height = ($height / $width) * $n_width; $temp_image = imagecreatetruecolor($n_width, $n_height); imagecopyresampled($temp_image, $src, 0, 0, 0, 0, $n_width, $n_height, $width, $height); imagejpeg($temp_image, $target, 100);解决 无用评论 打赏 举报