2016-04-15 149 views
1

我有一個尺寸爲(W-3000 X H-4000)的圖像。當我上傳並調整大小時,它總是顯示爲橫向模式,意味着新的尺寸是w-1067 X h-800。我想創建這張照片800X600的風景或600X800的肖像。這裏是我的代碼:圖像調整大小時,Codeigniter圖像旋轉

$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"; 
      } 
     }  
    } 

在我的代碼高度總是800px的大小。 3000X4000尺寸圖像可以。但是,當我使用4000X3000尺寸圖像時呢?任何人都可以幫我解決這個問題嗎?由於

+0

使用和getimagesize() – Vickel

回答

2
$filename = $_FILES['testing']['tmp_name']; 
list($width, $height) = getimagesize($filename); 

if ($width >= $height) 
{ 
    $config['width'] = 800; 
    $config['master_dim'] = 'width'; 
} 
else 
{ 
    $config['height'] = 800; 
    $config['master_dim'] = 'height'; 
} 

更短,您可以使用master_dim「自動」的參數,將決定其值越大 - 寬度或高度。

$filename = $_FILES['testing']['tmp_name']; 
list($width, $height) = getimagesize($filename); 

if ($width >= $height) 
{ 
    $config['width'] = 800; 
} 
else 
{ 
    $config['height'] = 800; 
} 

$config['master_dim'] = 'auto'; 
+0

我已經和2448X3264尺寸圖像嘗試過。當我調整大小時,它顯示800X600圖像。但調整大小的圖像旋轉。我想我應該使用exif。 –

2

感謝@Rpojka爲您解答。但我用exif_read_data()修復了它。這裏是我的代碼:

$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(); 

      } 
     }  
    } 

這裏是EXIF方向圖

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

很好的解決方案。只是文件類型的awware,因爲文件類型,這將是很好的處理它像'if(exif_imagetype('image.gif')== IMAGETYPE_JPEG或exif_imagetype('image.gif')== IMAGETYPE_TIFF_II或exif_imagetype( 'image.gif')== IMAGETYPE_TIFF_MM){ //其餘代碼 } else {// handle exception}' – Tpojka

+0

感謝您的信息:) –

+0

沒問題,我在[PHP文檔](http: /php.net/manual/en/function.exif-imagetype.php)。雖然對你有好處。 – Tpojka