2014-09-10 33 views
0

試圖解決從IOS設備上傳圖像的問題,exif方向會導致它們有時會被旋轉。imagerotate使用move_uploaded_file時

我發現了很多使用imagerotate的片段來解決這個問題,但是試圖實現它們。

對於我使用的是圖像的保存:

$moveUploadedFile = move_uploaded_file($fileToUpload["tmp_name"], $this->uploadDir . "/" . $newFileName); 

(從防彈圖像上傳類拍攝)

我沒事用做switch語句來檢查的EXIF數據,但不能使move_uploaded_file的工作。

我已經試過(測試)e.g:

$image = $fileToUpload["tmp_name"]; 
$image = imagerotate($image, 90, 0); 
$moveUploadedFile = move_uploaded_file($image, $this->uploadDir . "/" . $newFileName); 

這是給我的move_uploaded_file請求字符串的錯誤,但接收的資源來代替。

任何幫助?

回答

0

在這裏你去先生

public function moveUploadedFile($tmp_name, $destination) 
    { 
     if(move_uploaded_file($tmp_name, $destination)){ 
      $this->image_rotation(); 
      return true; 
     } 
} 
    public function image_rotation(){ 
     /* Check if the image is rotated, 
     * and if it's rotates. Fix it! 
     */ 
     // $filename = __DIR__ . '/'.$this->location .'/'. $this->name . '.' . $this->mime; 
     $filename = $this ->fullPath; 

     $exif = exif_read_data($filename); 
     if (isset($exif['Orientation'])) 
     { 
      switch ($exif['Orientation']) 
      { 
      case 3: 
      // Need to rotate 180 deg 
        $degrees = 180; 
        break ; 

      case 6: 
       // Need to rotate 90 deg clockwise 
        $degrees = -90; 
        break ; 

      case 8: 
       // Need to rotate 90 deg counter clockwise 
        $degrees = 90; 
        break ; 
      } 

      if (preg_match("/jpg|jpeg/", pathinfo($filename, PATHINFO_EXTENSION))) 
      { 
       $image_source = imagecreatefromjpeg($filename); 
       $rotate = imagerotate($image_source, $degrees, 0); 
       imagejpeg($rotate, $filename, 100); 

      } 
      if (preg_match("/png/", pathinfo($filename, PATHINFO_EXTENSION))) 
      { 
       $image_source = imagecreatefrompng($filename); 
       $rotate = imagerotate($image_source, $degrees, 0); 
       imagepng($rotate, $filename, 100); 
      } 
      if (preg_match("/gif/", pathinfo($filename, PATHINFO_EXTENSION))) 
      { 
       $image_source = imagecreatefromgif($filename); 
       $rotate = imagerotate($image_source, $degrees, 0); 
       imagepng($rotate, $filename, 100); 
      } 

      imagedestroy($image_source); //free up the memory 
      imagedestroy($rotate); //free up the memory 

     } 
} 
0

$圖像是一種資源。但是,move_uploaded_file採用上傳文件的文件名以及目標文件夾(http://php.net/manual/en/function.move-uploaded-file.php

您有一個要保存的資源,而不是特定路徑上的上傳文件。

使用imagejpeg($ image,$ destination)或imagepng($ image,$ destination),取決於您想要將其存儲爲。

希望這會有所幫助!