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

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求