0

imagecreatetruecolor当我传递由to生成的黑色图像背景时imagecopyresampled,它作为参数 1 给出的错误imagecopyresampled预计是资源,但给出了一个整数。

gif仅适用于图像。此外,我正在使用imagecolortransparentgif,以便动画不会显示为具有黑色背景的静态,但它也会在传递给时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 中给出的整数的相同 PHP 错误

imagecolortransparent

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,但仍然存在上述错误,并且也不会缩放图像,因为结果图像会从固定宽度的容器中弹出。

4

1 回答 1

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);
}
于 2013-03-12T07:59:48.360 回答