2013-03-03 18 views
2

我從這裏獲得了一個代碼http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php以獲取用戶發送的圖像大小調整。exif_read_data()表示給出的資源

這是它如何處理圖像發送。

  include('image_resize.php'); 
      $image = new SimpleImage(); 
      $image->load($upload_dir.$filename); 
      $image->resizeToWidth(190); 
      $image->save($upload_dir.$filename); 

這裏的image_resize.php的一部分:

function resizeToWidth($width) { 
    $ratio = $width/$this->getWidth(); 
    $height = $this->getheight() * $ratio; 
    $this->resize($width,$height); 
} 
function resize($width,$height) { 
    $new_image = imagecreatetruecolor($width, $height); 
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
    $this->image = $new_image; 
}  

我不會粘貼的所有代碼,原因到這裏eveything工作正常。

的事情是,我遇到直接從手機相機上傳照片時的取向問題,所以我寫了這個:

function resize($width,$height) { 
    $exif = exif_read_data($this->image, 0, true); 
    if(!empty($exif['IFD0']['Orientation'])) { 
     switch($exif['Orientation']) { 
      case 3: // 180 rotate left 
      $this->image = imagerotate($this->image, 180, 0); 
      break; 

      case 6: // 90 rotate right 
      $this->image = imagerotate($this->image, -90, 0); 
      break; 

      case 8: // 90 rotate left 
      $this->image = imagerotate($this->image, 90, 0); 
      break; 
     } 
    } 
    $new_image = imagecreatetruecolor($width, $height); 
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
$this->image = $new_image; 
}  

但是當我運行它,服務器說:

exif_read_data() expects parameter 1 to be string, resource given in /home/…/public_html/image_resize.php on line 101 

這是行101:$exif = exif_read_data($this->image, 0, true);

我搜索過的問題exif_read_data(),但我找不到什麼「資源給予」的意思,因爲我在其他問題和文檔中可以看到,您可以使用臨時圖像作爲參數。如何處理$image->this使其不被視爲資源?

回答

0

$this->image是一個圖像資源,由像imagecreatetruecolor()這樣的函數創建,它是圖像的表示。對於你的exif函數,你必須指定(字符串)文件名。

因此,它應該是這樣的:

function load($filename) { 

    $this->filename = $filename; 
    // ... 
} 

function resize($width,$height) { 
    $exif = exif_read_data($this->filename, 0, true); 
    // ... 
} 
+0

的感謝!我不知道。我剛剛寫了一個處理'$ _FILES'圖像的函數,它工作。 – fksr86 2013-03-04 00:20:08