dph19153 2016-04-15 16:33
浏览 53
已采纳

当图像调整大小时,Codeigniter图像旋转

I have an image with the dimension of (W-3000 X H-4000). When i upload it and resize, it always show like landscape mode, mean new dimension is w-1067 X h-800. I want to create this picture either 800X600 for landscape or 600X800 for portrait. Here is my code:

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'jpg|jpeg|gif|png';
    $config['max_size'] = '5000';
    $this->load->library('upload', $config);

    //check if a file is being uploaded
    if(strlen($_FILES["testimg"]["name"])>0){

        if ( !$this->upload->do_upload("testimg"))//Check if upload is unsuccessful
        {
            $error = array('error' => $this->upload->display_errors());
            print_r($errors);
        }
        else
        {

            $config['image_library'] = 'gd2';
            $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
            $config['width'] = '1';
            $config['height'] = '800';
            $config['maintain_ratio'] = TRUE;
            $config['master_dim'] = 'height';
            $this->load->library('image_lib',$config); 

            if (!$this->image_lib->resize()){  
                echo "error";
            }else{
                echo "success";
            }
       }      
   }  

In my code height is always 800px in size. It is ok for 3000X4000 dimension image. But what about when i use 4000X3000 dimension image? Can any one help me about this issue? Thanks

  • 写回答

2条回答 默认 最新

  • dsgoj7457 2016-04-16 05:15
    关注

    Thanks @Rpojka for your answer. But I fixed it with exif_read_data() . Here is my code:

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'jpg|jpeg|gif|png';
        $config['max_size'] = '6048';
        $this->load->library('upload', $config);
    
        //check if a file is being uploaded
        if(strlen($_FILES["testimg"]["name"])>0){
    
            if ( !$this->upload->do_upload("testimg"))//Check if upload is unsuccessful
            {
                $error = array('error' => $this->upload->display_errors());
                print_r($errors);
            }
            else
            {
    
                $config['image_library'] = 'gd2';
                $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
                $filename = $_FILES['testimg']['tmp_name'];
    
    
                $imgdata=exif_read_data($this->upload->upload_path.$this->upload->file_name, 'IFD0');
    
    
                list($width, $height) = getimagesize($filename);
                if ($width >= $height){
                    $config['width'] = 800;
                }
                else{
                    $config['height'] = 800;
                }
                $config['master_dim'] = 'auto';
    
    
                $this->load->library('image_lib',$config); 
    
                if (!$this->image_lib->resize()){  
                    echo "error";
                }else{
    
                    $this->image_lib->clear();
                    $config=array();
    
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
    
    
                    switch($imgdata['Orientation']) {
                        case 3:
                            $config['rotation_angle']='180';
                            break;
                        case 6:
                            $config['rotation_angle']='270';
                            break;
                        case 8:
                            $config['rotation_angle']='90';
                            break;
                    }
    
                    $this->image_lib->initialize($config); 
                    $this->image_lib->rotate();
    
                }
           }      
       }  
    

    Here is the exif orientation chart

     1        2       3      4         5            6           7          8
    
    888888  888888      88  88      8888888888  88                  88  8888888888
    88          88      88  88      88  88      88  88          88  88      88  88
    8888      8888    8888  8888    88          8888888888  8888888888          88
    88          88      88  88
    88          88  888888  888888
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用