2016-09-16 77 views
-1

我使用以下php代碼來保存來自各種設備的圖像。所有的工作正常,除了iPhone的圖像出現橫盤。我已經找到了一種方法來解決這個問題,在保存之前旋轉圖像。但是,當我上傳圖片時,它並沒有出現在我的網頁和我的文件管理器中,它仍然出現在側面。我的目標是錯誤的文件旋轉?還是我錯誤地使用了別的東西? 這裏是我的代碼:PHP旋轉圖像爲空

$file = $_FILES["newsnap"]; 
    $id = $_SESSION['id']; 
    $username = $_SESSION['username']; 
    $aboutitems = nl2br(mysqli_real_escape_string($database, $_POST['about-snap'])); 
    $uploadloc = mkdir("../$username/"); 
    $image_temp = $_FILES["newsnap"]['tmp_name'];//Temporary location 
    $filename = mysqli_real_escape_string($database, htmlentities($file["name"])); 


      $sourcePath = $image_temp; // source path of the file 


      $exif = exif_read_data($sourcePath); 
      $orientation = $exif['Orientation']; 

      switch($orientation) 
       { 
        case 3: 
         $sourcePath = imagerotate($sourcePath, 180, 0); 
         break; 
        case 6: 
         $sourcePath = imagerotate($sourcePath, -90, 0); 
         break; 
        case 8: 
         $sourcePath = imagerotate($sourcePath, 90, 0); 
         break; 
       } 

      $targetPath = "../$username/$filename"; // Target path where file is to be stored 

      move_uploaded_file($sourcePath, $targetPath) ; // Moving Uploaded file 

      $added = date("y.m.d"); 

      mysqli_query($database, "INSERT INTO piqs(userid, chicpiq, aboutpic, added) VALUES('$id', '$targetPath', '$aboutitems', '$added')"); 

代碼工作完全正常,而不下面的代碼。我在下面添加代碼只是爲了旋轉橫向圖像:

  $exif = exif_read_data($sourcePath); 
      $orientation = $exif['Orientation']; 

      switch($orientation) 
       { 
        case 3: 
         $sourcePath = imagerotate($sourcePath, 180, 0); 
         break; 
        case 6: 
         $sourcePath = imagerotate($sourcePath, -90, 0); 
         break; 
        case 8: 
         $sourcePath = imagerotate($sourcePath, 90, 0); 
         break; 
       } 

感謝您的幫助。

回答

1

當我正確地看到它$sourcePath是把文件路徑,不能旋轉的變量...

http://php.net/manual/en/function.imagerotate.php,你必須通過圖片的打開resourcere。所以你必須這樣做

$oldImage = ImageCreateFromJPEG($sourcePath); 
switch($orientation){ 
    case 3: 
     $newImage = imagerotate($oldImage, 180, 0); 
     break; 
    case 6: 
     $newImage = imagerotate($oldImage, -90, 0); 
     break; 
    case 8: 
     $newImage = imagerotate($oldImage, 90, 0); 
     break; 
    default: 
     $newImage = $oldImage; 
} 
imagejpeg($newImage, $targetPath, 90); 
+0

嗨法比安。感謝您指出這一點。我現在正在測試這個。到目前爲止,我可以看到一個小的變化,但現在圖像也顯示在iPhone和桌面上(早期它在桌面上橫向,但在iPhone上可以)。我會嘗試一些事情,並會更新我的代碼。你能再看一遍嗎?歡呼的人 – davidb

+0

不,你先調用'$ source = imagecreatefromjpeg($ image_temp)'(忘記var名稱中的路徑,它只會混淆),它會返回一個「資源」。然後你旋轉這個資源,所以你調用'$ imageRotated = imagerotate($ source,-90,0);'。在這個變量中,你有旋轉的圖像資源。使用'imagejpeg($ imageRotated,$ targetPath,90);',你會將新圖像寫入'$ targetPath'(計算時不要緊)。之後,您不必移動該文件,因爲您已經通過圖形處理複製了該文件。 –