2013-03-12 42 views
0

當我將由imagecreatetruecolor生成的黑色圖像背景傳遞給imagecopyresampled時,在imagecopyresampled中給出錯誤作爲參數1的錯誤預期爲資源,但是是給定的整數。PHP imagecopyresampled僅爲gif圖像提供資源錯誤

這僅發生在gif圖片上。此外,我使用gif的imagecolortransparent,以便動畫的不會顯示爲黑色背景的靜態圖像,但它也傳遞給imagecopyresampled時會出現相同的錯誤。

而不imagecolortransparent

function resizeImageGIF($image,$width,$height,$scale) { 
    $newImageWidth = ceil($width * $scale); 
    $newImageHeight = ceil($height * $scale); 
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); 
    $source = imagecreatefromgif($image); 
    imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height); 
    imagegif($newImage,$image); 
    chmod($image, 0777); 
    return $image; 
} 

上面生成與黑背景中的靜態GIF,並給出在參數1

給出隨着imagecolortransparent整數的相同PHP錯誤:

function resizeImageGIF($image,$width,$height,$scale) { 
    $newImageWidth = ceil($width * $scale); 
    $newImageHeight = ceil($height * $scale); 
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); 
    $black=imagecolorallocate($newImage,0,0,0); 
    $newImageTransparent = imagecolortransparent($newImage,$black); 
    $source = imagecreatefromgif($image); 
    imagecopyresampled($newImageTransparent,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height); 
    imagegif($newImageTransparent,$image); 
    chmod($image, 0777); 
    return $image; 
} 

此一個生成一個動畫gif但仍然有上述錯誤,並且不要將圖像縮放成固定寬度容器彈出的圖像。

回答

1

試試這個腳本,該腳本爲我工作

$size = 200; // the thumbnail height 
$filedir = 'uploads/'; // the directory for the original image 
$thumbdir = 'uploads/'; // the directory for the thumbnail image 
$prefix = 'small0_'; // the prefix to be added to the original name 
$maxfile = '2000000'; 
$mode = '0666'; 
$rnd_1 = rand(11111,99999); 

$userfile_name= $rnd_1.'_'.$_FILES['image']["name"]; 
$userfile_tmp = $_FILES['image']['tmp_name']; 
$userfile_size = $_FILES['image']['size']; 
$userfile_type = $_FILES['image']['type']; 
if (isset($_FILES['image']['name'])) 
{ 
$prod_img = $filedir.$userfile_name; 
$prod_img_thumb = $thumbdir.$prefix.$userfile_name; 
move_uploaded_file($userfile_tmp, $prod_img); 
chmod ($prod_img, octdec($mode)); 
$sizes = getimagesize($prod_img); 
$aspect_ratio = $sizes[1]/$sizes[0]; 
if ($sizes[1] <= $size) 
{ 

$new_width = '500'; 
$new_height = '500'; 
}else{ 

$new_width = '500'; 
$new_height = '500'; 
} 
$destimg=ImageCreateTrueColor($new_width,$new_height) 
or die('Problem In Creating image'); 

$srcimg=ImageCreateFromJPEG($prod_img) 
or $srcimg=ImageCreateFromPNG($prod_img) 
or $srcimg=ImageCreateFromGIF($prod_img) 
or die('Problem In opening Source Image0'); 
if(function_exists('imagecopyresampled')) 
{ 
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 
or die('Problem In resizing'); 
}else{ 
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 
or die('Problem In resizing'); 
} 
ImageJPEG($destimg,$prod_img_thumb,90) 
or die('Problem In saving'); 
} 
imagesavealpha($prod_img_thumb, true); 
//setting completely transparent color 
$transparent = imagecolorallocatealpha($prod_img_thumb, 0, 0, 0, 127); 
//filling created image with transparent color 
imagefill($prod_img_thumb, 0, 0, $transparent); 
imagepng($prod_img_thumb); 
} 
+0

您的代碼使用的是相同的邏輯,我的,我看不出它怎麼會不給我同樣的錯誤。 – coder101 2013-03-12 08:02:49

+0

coder101這個腳本正在爲我工​​作 – 2013-03-12 08:05:14

+0

這是非常奇怪的'imagecopyresampled'如何返回一個整數的gif圖像只在我的情況下 – coder101 2013-03-12 08:10:13