weixin_33725722 2018-02-10 08:54 采纳率: 0%
浏览 18

按大小压缩图像

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);
    
    评论

报告相同问题?