2013-10-23 42 views
3

編輯爲什麼這個基本的imagejpeg()調整器會返回黑色圖像?

感謝所有的答案,尤其是@Mailerdaimon誰發現我不是在imagecopyresampled功能使用的計算值。

我不再得到黑色圖像,但我仍然得到一些黑色部分,所以我想我的比例公式應該更新:如果我上傳風景圖像,新圖像的高度小於170px,並且然後有一些黑色的表現。

我怎樣才能確保圖像的高度一路走來?


下面是一個簡單的腳本允許用戶上傳圖片。上傳完成後,圖片將顯示爲170px(h)x 150px(w)的縮略圖。

容量調整部分不工作因爲輸出圖像是170x150px,但我仍然得到,如果

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{ 

$maxWidth = 150; 
$maxHeight = 170; 

$name = $_FILES ['image'] ['name']; 
$type = $_FILES ["image"] ["type"]; 
$size = $_FILES ["image"] ["size"]; 
$tmp_name = $_FILES ['image'] ['tmp_name']; 
list($originalWidth, $originalHeight) = getimagesize($tmp_name); 


if ($originalWidth > $originalHeight) 
{ 
    $thumbnail_height = floor(($originalHeight/$originalWidth)*$maxWidth); 
    $thumbnail_width = $maxWidth; 
} else { 
    $thumbnail_width = floor(($originalWidth/$originalHeight)*$maxHeight); 
    $thumbnail_height = $maxHeight; 
} 

// Resample 
    $image_p = imagecreatetruecolor($maxWidth, $maxHeight); 
    imagecreatefrompng($tmp_name); 
    imagecopyresampled($image_p, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, 
    $originalWidth, $originalHeight); 

//start upload process 
$RandomNumber = uniqid(); 
$location = "uploads/$RandomNumber"; 
imagejpeg($image_p, $location, 100);  

$sql=query("UPDATE users SET image = '".$location."' WHERE id = '$id'"); 

     } 
    } 

任何想法,我做錯了一些黑色的區域?

+0

你確定上傳的圖片是PNG文件嗎? (mime image/png)? –

+0

@ mly0是的,我只上傳PNG –

+0

您是否找到了解決方案? – Mailerdaimon

回答

1

我從來沒有使用PHP的,所以這可能是不正確的,但我不能看到你用你的計算值的寬度和高度!

if ($originalWidth > $maxWidth || $originalHeight > $maxHeight) 
{ 
     if ($originalWidth/$maxWidth > $originalHeight/$maxHeight) 
    { 
     // width is the limiting factor 
     $width = $maxWidth; 
     $height = floor($width * $originalHeight/$originalWidth); 

    } else { 
     // height is the limiting factor 
     $height = $maxHeight; 
     $width = floor($height * $originalWidth/$originalHeight); 
    } 

在這裏,你計算的高度和寬度,之後他們沒有在你的代碼的任何地方出現...

編輯: 您使用:

$image_p = imagecreatetruecolor($maxWidth, $maxHeight); 
imagecreatefrompng($tmp_name); 
imagecopyresampled($image_p, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height,$originalWidth, $originalHeight); 

imagecopyresmapledDocumentation指出,該功能將在後續

imagecopyresampled ($dst_image ,$src_image ,$dst_x ,$dst_y ,$src_x ,$src_y ,$dst_w , $dst_h ,$src_w ,$src_h) 

但我認爲你的代碼應該是

imagecopyresampled($image_p,$tmp_name, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height,$originalWidth, $originalHeight); 

如已經@Manolo Salsas(和其他幾個人)

EDIT2提到: 另一個問題在於你如何計算你Heigth和寬度值。 如果$height被計算喜歡這個

$height = floor($width * $originalHeight/$originalWidth); 

它可以比$maxHeight小,但創造中的黑色產生的圖像(填充)區,因爲你已經使用了最大創建此圖像值

$image_p = imagecreatetruecolor($maxWidth, $maxHeight); 

爲了解決這個問題用你的計算$ thumbnail_height和$ thumbnail_width價值創造的形象

$image_p = imagecreatetruecolor($thumbnail_width, $thumbnail_height); 

或始終將圖像大小調整爲最大值

imagecopyresampled($image_p,$tmp_name, 0, 0, 0, 0, $maxWidth, $maxHeight,$originalWidth, $originalHeight); 

這可能會使圖像變形。如果您不想扭曲圖像,請首先考慮調整大小,使尺寸適合縮略圖,然後裁剪較長的一面。

+0

+1。這實際上解決了問題的一半!我不能相信我沒有使用$ height和$ width。我猜我的公式有些問題,因爲現在寬度是受到尊重的,但取決於原始圖像的長度,仍然會顯示一些黑色。 –

+0

您能否使用此刻的代碼更新您的答案?比我可以看看它;) – Mailerdaimon

+0

剛更新它。 –

1
  1. 確保文件夾「上傳」是可寫的或由Web服務器
  2. 嘗試使用絕對路徑資:在/ var/WWW/mysite的/上傳
  3. 嘗試使用不同的東西隨機ID ,就像行ID或圖像本身的散列或md5一樣,以避免重複。

您可以更改的最後一部分:

...

$ RandomNumber = MD5($ image_p); $ location =「/ var/www/mysite/uploads/$ RandomNumber」;如果(imagejpeg($ image_p,$ location,100)){
query(「UPDATE users SET image ='」。$ location。「'WHERE id ='$ id'」); }

...

+0

感謝您的回答,在決定調整圖像大小之前,此腳本工作正常。所以路徑,隨機ID和文件夾可能不會對我當前的問題負責。 –

0

首先,

@getimagesize($_FILES['userfile']['tmp_name']))

此功能檢查,如果該文件是真實的圖像(不只是擴展名)。如果文件不是圖像,它將返回false(併發出警告,這就是爲什麼at)。

如果該文件是,如果你想有一個真正的圖像可以返回此:

list($width, $height, $type, $attributes) = @getimagesize($_FILES['userfile']['tmp_name'])) 

或只是大小:

$size = @getimagesize($_FILES['userfile']['tmp_name'])) 

因此,如果您要使用此功能,請記得添加在,並使用if條件來檢查您是否正在接收真實圖像。

在另一方面,您收到比你想要的大小真實的圖像和更小的驗證後,你可以這樣做:

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $imagePath) 

其中$imagePath是要安全的圖像的路徑。

然後,你可以這樣做:

$image = imagecreatefromjpeg($imagePath); 
$canvas=imagecreatetruecolor($maxwidth,$maxheight); 
imagecopyresampled($canvas, $image ,0,0,0,0,$maxwidth, $maxheight, $originalWidth, $originalHeight); 

而且你有合適的圖像大小。

+0

已編輯。現在這是一個答案 – Manolo

+0

謝謝,但我不知道明白。爲什麼在'(move_uploaded_file($ _ FILES ['userfile'] ['tmp_name'],$ imagePath)''之前有if條件?什麼是$ dimx,$ dimy? –

+0

條件是確保文件已上傳。如果不是,你會得到錯誤的,所以你可以給客戶端發送錯誤信息,'$ dimx'和'$ dimy'應該是'$ maxwidth'和'$ maxheight',我會更新答案。 – Manolo

相關問題