2015-08-21 94 views
3

我上傳的圖像出現奇怪的問題。當我在iPhone和iPad上查看時,它們會正確旋轉,但每當我嘗試在桌面上查看它們時,都會顯示錯誤的方向。 我找不到錯誤,花了幾個小時搞亂了EXIF數據後,我已經放棄了。 修復方向後,我還調整圖像大小,但不應該干擾其他代碼。如果是這樣,我會包括它。圖像在移動設備上正確旋轉,而不是在桌面上

我沒有足夠的聲譽上傳圖片,但這裏是他們的鏈接:
http://i.imgur.com/ARwSUuV.png
http://i.imgur.com/00yj7OJ.png

下面是我用上傳的代碼:

$path_parts = pathinfo($_FILES["file"]["name"]); 
$filepath = $_FILES['file']['tmp_name']; 
$image = imagecreatefromstring(file_get_contents($filepath)); 

// Rotate image correctly! 
$exif = exif_read_data($image); 
if(!empty($exif['Orientation'])) { 
    switch($exif['Orientation']){ 
     case 1: // nothing 
     break; 
     case 2: // horizontal flip 
     $image = imageflip($image, IMG_FLIP_HORIZONTAL); 
     break; 
     case 3: // 180 rotate left 
     $image = imagerotate($image,180,0); 
     break; 
     case 4: // vertical flip 
     $image = imageflip($image, IMG_FLIP_VERTICAL); 
     break; 
     case 5: // vertical flip + 90 rotate right 
     $image = imageflip($image, IMG_FLIP_VERTICAL); 
     $image = imagerotate($image,-90,0); 
     break; 
     case 6: // 90 rotate right 
     $image = imagerotate($image,-90,0); 
     break; 
     case 7: // horizontal flip + 90 rotate right 
     $image = imageflip($image, IMG_FLIP_HORIZONTAL); 
     $image = imagerotate($image,-90,0); 
     break; 
     case 8: // 90 rotate left 
     $image = imagerotate($image,90,0); 
     break; 
    } 
} 

switch ($path_parts['extension']) { 
    case 'gif' : 
    $im = imagecreatefromgif($image); 
    break; 
    case 'jpg' : 
    $im = imagecreatefromjpeg($image); 
    break; 
    case 'png' : 
    $im = imagecreatefrompng($image); 
    break; 
    case 'bmp' : 
    $im = imagecreatefrombmp($image); 
    break; 
} 
if($im){ 
    imagejpeg($im, $_FILES['file']['tmp_name'], 40);  
} 
$image_path = 'd_'.time() . "." . $path_parts['extension']; 
$move_result = move_uploaded_file($_FILES['file']['tmp_name'], '../img/results/' . $image_path); 

如果你有任何想法,爲什麼它只能在某些平臺上正確旋轉,我會非常感激!

編輯:應該澄清,圖像將經常從智能手機或平板電腦上傳。

+0

也許不直接關係到你陳述的問題,而是'imageflip()''返回bool',不是資源,所以你不應該把它分配給'$ image'然後嘗試'imagerotate()'。 – timclutton

+0

哦。對。謝謝,蒂姆! 我應該使用哪種方法來翻轉圖像? –

+0

只需使用'imageflip()'而不分配返回值。它直接操縱資源。 – timclutton

回答

1

有一些錯誤,停止代碼工作。嘗試開啓error reporting以幫助您調試這樣的問題。

  • exif_read_data()作品上的文件,而不是一個GD資源,所以通過$filepath,而不是$image
  • imageflip()直接操縱資源並返回bool,因此將返回值分配給$image會破壞資源。
  • 第二個switch()聲明根本不需要。 imagecreatefrom___()函數從一個文件創建一個資源,但是你將一個已經創建的資源傳遞給它們 - 你所要做的就是輸出它。

否則的方向校正似乎準確,應該爲你工作(它對我用我的手機拍攝的各種測試照片)。

下面是更正後的代碼:

$path_parts = pathinfo($_FILES["file"]["name"]); 
$filepath = $_FILES['file']['tmp_name']; 
$image = imagecreatefromstring(file_get_contents($filepath)); 

// Rotate image correctly! 
$exif = exif_read_data($filepath); 
if (!empty($exif['Orientation'])) { 
    switch ($exif['Orientation']) { 
     case 1: // nothing 
      break; 
     case 2: // horizontal flip 
      imageflip($image, IMG_FLIP_HORIZONTAL); 
      break; 
     case 3: // 180 rotate left 
      $image = imagerotate($image, 180, 0); 
      break; 
     case 4: // vertical flip 
      imageflip($image, IMG_FLIP_VERTICAL); 
      break; 
     case 5: // vertical flip + 90 rotate right 
      imageflip($image, IMG_FLIP_VERTICAL); 
      $image = imagerotate($image, -90, 0); 
      break; 
     case 6: // 90 rotate right 
      $image = imagerotate($image, -90, 0); 
      break; 
     case 7: // horizontal flip + 90 rotate right 
      imageflip($image, IMG_FLIP_HORIZONTAL); 
      $image = imagerotate($image, -90, 0); 
      break; 
     case 8: // 90 rotate left 
      $image = imagerotate($image, 90, 0); 
      break; 
    } 
} 

imagejpeg($image, $_FILES['file']['tmp_name'], 40); 

$image_path = 'd_'.time() . "." . $path_parts['extension']; 
$move_result = move_uploaded_file($_FILES['file']['tmp_name'], '../img/results/' . $image_path); 
+0

你是我的英雄蒂姆! –

相關問題