Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select功能無法創建從PNG和GIF文件類型的圖像
這裏是要創造有史以來正在上傳什麼頭圖像,並通過將其存儲到指定的目錄將其用於一般的網站佈局的函數,即在瀏覽器上重新緩存圖像時會執行其他操作。該功能可以輕鬆處理JPG,並且事實上我需要將所有圖像格式PNG/GIF/BMP轉換爲JPG,因爲文件被重命名爲header.jpg沒有JPEG,但只有.JPG。看起來與PNG文件沒有什麼關係,因爲它們沒有創建成功能必須完成的功能。下面我提供了這個功能,讓我知道我能做些什麼來改善它,並可能使它更快?
function createHeader($new_file) {
$path = "uploads/";
$image = "cache/";
$width = 800;
if(preg_match('/.(jpg|jpeg)$/', $new_file)) {
$im = imagecreatefromjpeg($image . $new_file);
} else if (preg_match('/.gif$/', $new_file)) {
$im = imagecreatefromgif($image . $new_file);
} else if (preg_match('/.png$/', $new_file)) {
$im = imagecreatefrompng($image . $new_file);
}
imagealphablending($im, false);
imagesavealpha($im, true);
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $width;
$ny = floor($oy * ($width/$ox));
$nm = imagecreatetruecolor($nx, $ny);
imagealphablending($im, false);
imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
imagealphablending($im, true);
if(!file_exists($path)) {
if(!mkdir($path)) {
die("There was a problem. Please try again!");
}
}
if(!is_writable($path)) die("unable to write to specified path");
imagejpeg($nm, $path . $new_file, 100);
imagedestroy($im);
imagedestroy($nm);
}
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in D:\wamp\www\cms\cms\create_header.php on line 13
Warning: imagesy() expects parameter 1 to be resource, boolean given in D:\wamp\www\cms\cms\create_header.php on line 28
Warning: Division by zero in D:\wamp\www\cms\cms\create_header.php on line 31
Warning: imagecreatetruecolor() [function.imagecreatetruecolor ]: Invalid image dimensions in D:\wamp\www\cms\cms\create_header.php on line 33
Warning: imagealphablending() expects parameter 1 to be resource, boolean given in D:\wamp\www\cms\cms\create_header.php on line 34
Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in D:\wamp\www\cms\cms\create_header.php on line 36
Warning: imagealphablending() expects parameter 1 to be resource, boolean given in D:\wamp\www\cms\cms\create_header.php on line 37
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in D:\wamp\www\cms\cms\create_header.php on line 45
Warning: imagedestroy() expects parameter 1 to be resource, boolean given in D:\wamp\www\cms\cms\create_header.php on line 47
Warning: imagedestroy() expects parameter 1 to be resource, boolean given in D:\wamp\www\cms\cms\create_header.php on line 48
您是否收到任何錯誤? – 2012-02-11 01:56:03
@保羅不,它只是不會創建任何東西,舊的header.jpg圖像會被留下。對於PNG或GIF沒有任何影響,腳本運行並刷新新圖像應顯示爲標題但沒有任何內容,並且我可以在本地主機上的目錄中看到舊圖像,因此它不是我的緩存,更像是功能很難使用PNG/GIF轉換爲JPG – 2012-02-11 01:58:43
好吧,在它不處理的地方調試一下。是否因爲擴展條件不匹配而導致不能創建'$ im'資源,圖像是否未正確重採樣,文件寫入不正確?一個簡單的「什麼都不做」對我們來說有點太粗糙,我們無法遠程調試你的代碼。 – deceze 2012-02-11 02:01:43