dqwh1203 2016-11-20 21:44
浏览 23
已采纳

无法通过php调整图像大小

i`m noobie in php, just creating image resize script. Following code is about what i try to do. Particularly - try to understand how to resize an image after addition , but face with some issue. Please, help me , what is wrong with my code ?

        $temp = getimagesize($_FILES['file']['tmp_name']); 
        $name = '/upload/'.date('Ymd-His').'img'. rand(10000, 99999).'.jpg';

        if (!in_array($matches[1], $array2)) {
            $errors['file'] = 'Wrong file extension';
        } elseif (!in_array($temp['mime'], $array)) { 
            $errors['file'] = 'Wrong type of file';
        } elseif (!move_uploaded_file($_FILES['file']['tmp_name'],'.'.$name)){ 
            $errors['file'] = 'image is not loaded';
        } else {

           // there is a problem

            $tmp = imagecreatetruecolor(200, 150);    
            $image = imagecreatefromjpeg($name);      
            imagecopyresampled($tmp, $image, 0,0,0,0, 200, 150, $temp[0], $temp[1]);
            imagejpeg($tmp, 100);

     // which i can`t fix

        }

    } else {
        $errors['file'] = 'The file is not an image. Valid file types: jpg, png, gif';
    }
}
}
  • 写回答

1条回答 默认 最新

  • ds3422222222 2016-11-21 02:30
    关注

    use these functions :

        #####  This function will proportionally resize image ##### 
        function do_resize_image($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality){
    
            if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize
    
            //do not resize if image is smaller than max size
            if($image_width <= $max_size && $image_height <= $max_size){
                if(save_image($source, $destination, $image_type, $quality)){
                    return true;
                }
            }
    
            //Construct a proportional size of new image
            $image_scale    = min($max_size/$image_width, $max_size/$image_height);
            $new_width      = ceil($image_scale * $image_width);
            $new_height     = ceil($image_scale * $image_height);
    
            $new_canvas     = imagecreatetruecolor( $new_width, $new_height ); //Create a new true color image
    
            //Copy and resize part of an image with resampling
            if(imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height)){
                save_image($new_canvas, $destination, $image_type, $quality); //save resized image
            }
    
            return true;
        }
    
    ##### Saves image resource to file ##### 
    function save_image($source, $destination, $image_type, $quality){
        switch(strtolower($image_type)){//determine mime type     
            case 'image/jpeg': case 'image/pjpeg': 
                imagejpeg($source, $destination, $quality); return true; //save jpeg file
                break;
            default: return false;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看