bool imagefill ( resource image, int x, int y, int color )
imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。

解决方案 »

  1.   

    function addBg($src,$w,$h)
        {
            $bg = imagecreatetruecolor($w,$h);
            $white = imagecolorallocate($bg,255,255,255);
            imagefill($bg,0,0,$white);//填充背景
     
            //获取目标图片信息
            $info=getimagesize($src);
            $width=$info[0];//目标图片宽度
            $height=$info[1];//目标图片高度
            switch ($info[2]){
                case 1:
                    $img = imagecreatefromgif($src);
                break;
                case 2:
                    $img = imagecreatefromjpeg($src);
                    break;
                case 3:
                    $img = imagecreatefrompng($src);
                    break;
                default:
                    exit('不支持的图像格式');
                break;
            }
            if($height < $h)
            {
                $x=0;
                $y=($h-$height)/2;//垂直居中
            }
            if($width < $w)
            {
                $x=($w-$width)/2;//水平居中
                $y=0;
            }
            if($height < $h && $width < $w){
                $x = ($w-$width)/2;
                $y = ($h-$height)/2;
            }
            imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);
            imagejpeg($bg,$src,100);
            imagedestroy($bg);
            imagedestroy($img);
            return $src;
        }
    我代入进去没有任何效果啊
    addBg("97972188.jpg",$pieces[0],$pieces[1]);
      

  2.   

    我对图像操作不是很熟悉。不过可以给一点我的想法:如果是单一背景,那么我建议你创建一个透明的png可能更快捷,我记得在创建png时可以指定一个颜色为透明色的。
      

  3.   

    我找到一段代码,应该感觉比较像,但怎么改还是没有头绪,应该怎么改好呢,请高手指教,谢谢
    $o_pic = '97972188.jpg';//图像中要处理的色阶
    $begin_r = 178;
    $begin_g = 178;
    $begin_b = 178;list($src_w,$src_h,$src_type) = getimagesize($o_pic);// 获取原图像信息$file_ext = get_ext($o_pic);//获取扩展名
    $target_im = imagecreatetruecolor($src_w,$src_h);//新图
    if($file_ext == 'jpg') //转换JPG 开始
    {
        $src_im = ImageCreateFromJPEG($o_pic);
        echo $src_w;
        imagecopymerge($target_im,$src_im,0,0,0,0,$src_w,$src_h,100);    for($x = 0; $x < $src_w; $x++)
        {
            for($y = 0; $y < $src_h; $y++)
            {
                $rgb = imagecolorat($src_im, $x, $y);            $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;
                //将开始设定的色阶值改为白色
                if($r > $begin_r && $g > $begin_g && $b > $begin_b ){   
                    imagecolortransparent($target_im, imagecolorallocate($target_im,$r, $g, $b));                
                }
            }
        }}
    header("Content: image/jpeg"); 
    imagejpeg($target_im,'c.jpg'); 
    imagedestroy($target_im); 
      

  4.   

    http://stackoverflow.com/questions/16660729/php-change-background-color-to-transparentThis maybe help. 
      

  5.   

    Well, it is more or less the same code as you posted.