2015-08-24 153 views
0

我做了一個應用程序,允許用戶拍攝一張照片,然後將其存儲在在線服務器上。我的問題是,根據手機的某些圖片收到旋轉。在PHP中旋轉圖像

這是我的PHP文件:(我收到的圖片用base64編碼)

<?php 

    $file_path = "photos/"; 
    $image = $_REQUEST['base64']; 
    $name=$_REQUEST['ImageName']; 

    // Decode Image 
    $binary=base64_decode($image); 

    $success = file_put_contents($file_path.$name, $binary); 

    if($success === false) { 
     echo "Couldn't write file"; 
    } else { 
     echo "Wrote $success bytes"; 
    } 
    echo $name; 
?> 

我嘗試使用此代碼,但它不會改變我的照片方向(也許是因爲我要送它的錯誤參數?)

$exif = exif_read_data($binary); 
$ort = $exif['IFD0']['Orientation']; 
    switch($ort) 
    { 
     case 1: // nothing 
     break; 

     case 2: // horizontal flip 
      $binary->flipImage($public,1); 
     break; 

     case 3: // 180 rotate left 
      $binary->rotateImage($public,180); 
     break; 

     case 4: // vertical flip 
      $binary->flipImage($public,2); 
     break; 

     case 5:// vertical flip + 90 rotate right 
      $binary->flipImage($public, 2); 
       $binary->rotateImage($public, -90); 
     break; 

     case 6: // 90 rotate right 
      $binary->rotateImage($public, -90); 
     break; 

     case 7: // horizontal flip + 90 rotate right 
      $binary->flipImage($public,1);  
      $binary->rotateImage($public, -90); 
     break; 

     case 8: // 90 rotate left 
      $binary->rotateImage($public, 90); 
     break; 
    } 

任何想法我應該改變? 我很抱歉,如果這聽起來像一個愚蠢的問題,但我是新來的PHP。

回答

2

我會去GD圖像處理庫進行旋轉。下面是一個例子:

// Load the original file into GD 
$path = __DIR__ . '/original.png'; 
$original = imagecreatefrompng($path); 

// Rotate the image by 90 degrees 
$rotated = imagerotate($original, 90, 0); 

// Save the rotated image 
imagepng($rotated, __DIR__ . '/rotated.png');