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 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?