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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵