2010-03-27 265 views
0

我有一個類來讀取和輸出圖像內容,如果$寬度設置,它將調整圖像大小,然後輸出它。php調整大小圖像

如果我調用這樣的函數$ image-> readImage('123.jpg'); ,它可以正確輸出圖像文件,但是當我調用$ image-> readImage('123.jpg',300);調整它的大小,它只是顯示一個黑色的圖像,調整後的寬度爲&高度。

我試圖從

@imagejpeg($thumb, null, 100); 

更換代碼

@imagejpeg($image, null, 100); 

意志的作品〜

-

protected function readImage($fileName, $width = 0) 
{ 
    if ($width <= 0) { 
     return @file_get_contents($this->destination . '/' . $fileName); 
    } else { 
     $imageSize = @getimagesize($this->destination . '/' . $fileName); 
     $actualWidth = $imageSize[0]; 
     $actualHeigth = $imageSize[1]; 

     if ($actualWidth <= $width) { 
      return @file_get_contents($this->destination . '/' . $fileName); 
     } 
     $height = (100/($actualWidth/$width)) * .01; 
     $height = @round($actualHeigth * $height); 

     $image = @imagecreatefromjpeg($this->destination . '/' . $fileName); 
     $thumb = @imagecreatetruecolor($width, $height); 
     @imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $actualWidth, $actualHeight); 

     ob_start(); 
     @imagejpeg($thumb, null, 100); 
     $bits = ob_get_contents(); 
     ob_end_clean(); 

     return $bits; 
    } 
} 

哪位高手知道發生了什麼,並幫助我解決它?

謝謝。

+1

如果你從函數調用前刪除了所有的'@'符號,你可能會得到一個體面的錯誤信息,可能會給你一個線索。使用'@'是一個壞習慣,除非你確實需要它。 – zombat 2010-03-27 00:08:33

+0

你的第一段有點令人困惑,因爲你聲稱相同的確切代碼會做兩件不同的事情......你可以澄清你說你稱呼'$ image-> readImage('123.jpg')的意思嗎? ? – 2010-03-27 00:29:09

+0

對不起,我編輯過它。 – haohan 2010-03-27 00:35:51

回答

8

您已在$的ActualHeight的拼寫VS $ actualHeigth

被不一致的,如果你沒有那麼多@無處不在,那麼PHP會告訴你這一點。

+0

謝謝,我將在未來編碼@。 – haohan 2010-03-27 00:55:35

+0

如果你不需要他們爲什麼他們在那裏hahan? – 2010-03-27 03:11:09