2011-03-21 49 views
0

我有一個腳本來調整上傳的圖像,但是當我使用它,它只是返回一個黑色的正方形。所有錯誤消息都指向這個函數:Imagecreatefromjpeg返回黑色圖像後調整

function resizeImage($image,$width,$height,$scale) { 
    $newImageWidth = ceil($width * $scale); 
    $newImageHeight = ceil($height * $scale); 
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); 
    $source = imagecreatefromjpeg($image); 
    imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height); 
    imagejpeg($newImage,$image,90); 
    chmod($image, 0777); 
    return $image; 
} 

我的錯誤日誌:

PHP Warning: imagecreatefromjpeg() [<a href='function.imagecreatefromjpeg'>function.imagecreatefromjpeg</a>]: gd-jpeg: JPEG library reports unrecoverable error 
PHP Warning: imagecreatefromjpeg() [<a href='function.imagecreatefromjpeg'>function.imagecreatefromjpeg</a>]: 'img/[hidden].jpg' is not a valid JPEG file 
PHP Warning: imagecopyresampled(): supplied argument is not a valid Image resource 

回答

0

根據Marc B的回答,你可能會檢查該文件是否爲JPG文件。 (JPEG,JPG,jpg,jpeg擴展名)。

它可能是這樣的:

$file = explode(".", $_POST['file']); 
$file_ext = $file[count($file)]; // Get the last thing in the array - in this way the filename can containg dots (.) 
$allowed_ext = array('jpg', 'JPG', 'jpeg', 'jpg'); 

if(in_array($file_ext, $allowed_ext) 
{ 
    // The code for creating the image here. 
} 
+0

感謝您的支持! – iamandrus 2011-03-21 21:51:54

0
  1. 檢查是否imagecreatetruecolor成功。如果新圖像「大」,它可能會超過PHP memory_limit。如果因任何原因失敗,此函數返回FALSE。
  2. 同上同imagecreatefromjpeg()。這兩個單獨的圖像可能符合內存限制,但可能會過大。源圖像也可能不存在。該函數返回FALSE失敗,如果有任何理由
  3. 檢查是否imagecopyresampled()失敗 - 它也失敗時返回FALSE。
  4. 檢查imagejpeg()失敗 - 也許你沒有在任何文件你在$image指定寫權限。再次,這個函數在失敗時返回FALSE。
+0

我剛添加的錯誤日誌。 – iamandrus 2011-03-21 20:19:30

+0

你走了......你想要縮放的原始圖像不能被GD讀取。或者它不是JPG,或者是(也許)是CMYK jpeg,或者在文件中包含其他內容以使GD認爲它不是jpg。 – 2011-03-21 20:20:54

相關問題