2016-04-15 10 views
1

Ich habe ein Bild mit der Dimension von (W-3000 X H-4000). Wenn ich es hochlade und die Größe verändere, wird es immer als Landscape-Modus angezeigt, meine neue Dimension ist w-1067 X h-800. Ich möchte dieses Bild entweder 800X600 für Querformat oder 600X800 für Hochformat erstellen. Hier ist mein Code:Codeigniter Bild drehen, wenn Bildgröße ändern

$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 meinem Code Höhe ist immer 800px groß. Es ist in Ordnung für 3000X4000 Dimensionsbild. Aber was ist, wenn ich 4000X3000 Dimension Image verwende? Kann mir jemand zu diesem Thema helfen? Dank

+0

Verwendung getimagesize() – Vickel

Antwort

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'; 
} 

Noch kürzere, können Sie ‚auto‘ Parameter für master_dim verwenden, die größer ist, welcher Wert bestimmen, - Breite oder Höhe.

$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

Ich habe versucht, es mit 2448x3264 Dimension Bild. Wenn ich die Größe ändere, wird ein 800X600 Bild angezeigt. Das geänderte Bild wird jedoch gedreht. Ich denke, ich sollte Exif verwenden. –

2

Dank @Rpojka für Ihre Antwort. Aber ich habe es mit exif_read_data() behoben. Hier ist mein 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(); 

      } 
     }  
    } 

Hier wird die EXIF-Orientierung Diagramm

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

Schöne Lösung. Seien Sie wegen des Dateityps vom Dateityp awware und es wäre gut, ihn mit etwas wie 'if (exif_imagetype (' image.gif ') == IMAGETYPE_JPEG ODER exif_imagetype (' image.gif ') == IMAGETYPE_TIFF_II ODER exif_imagetype ('image.gif') == IMAGETYPE_TIFF_MM) { // Rest des Codes } else {// Handle Ausnahme} ' – Tpojka

+0

Danke für Ihre Info :) –

+0

Kein Problem, ich fand es in [PHP Dokumente] (http://php.net/manual/en/function.exif-imagetype.php). Gute Arbeit für dich. – Tpojka