dongliang9576 2014-08-26 19:36
浏览 29
已采纳

根据最大尺寸(字节)上传和自动调整图像大小

I have already uploaded the image from a form and I can resize according a value and a level of quality. But I want to Auto resize an image according to a max size(byte). So,

IF (IMAGE_UPLOADED_SIZE > MAX_SIZE)
{
   WHILE(IMAGE_UPLOADED_SIZE > MAX_SIZE)
   {
     // MAIN CALCULTATIONS
     imagecreatetruecolor(); // CREATION NEW RESIZED IMAGE
     imagecopyresampled(); // COMPOSE THE NEW IMAGE
     IMAGE_UPLOADED_SIZE = getNewSize();
   }
}

The loop works but it becomes wrong because I have to get the new SIZE of the new resized image generated from "imagecreatetruecolor()" that with a print_r() command return RESOURCE#[id].

  • 写回答

1条回答 默认 最新

  • duandao6414 2014-08-26 21:41
    关注

    One issue that would be breaking this code is these: IMAGE_UPLOADED_SIZE and MAX_SIZE. Are those supposed to be variables like $image_size and $max_image_size? Or have you declared them constants with define? See here for what that is: PHP manual - Define.

    If they are variables then all you just need to tweak the code. If they are constants your approach will not work the way it is coded. you can not just change the value of a constant like you did in the While loop. See the link above. Here is the code tweaked with variables:

    NOTE TO READERS: This code is cut down and is missing parameters that normally should be a part of the functions being run in the loop. See the examples that explain it further in the PHP manual.

     while ($image_upload_size > $max_size){
        imagecreatetruecolor(); 
        imagecopyresampled(); 
        // Removed $getNewSize() what was that? Your own function?
        $image_upload_size = getimagesize('IMAGE NAME/PATH HERE');
        // Now you have the value of the new size, no resource ID issue, but its temporary
        // This while loop is about to run again and wipe it with another new value
        // ADD CODE HERE to do what you need with the new image size
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题