去掉图片转换后的黑色背景

来源:互联网 发布:python框架有哪些 编辑:程序博客网 时间:2024/05/17 07:44

在使用php的GD函数生成thumbnail后,如果原图片中有白色区域,生成的新图片会有黑色的,并不是透明的


原图片


thumbnail

显然不希望有这种黑色的背景出现,应该是透明的

那么如何处理呢

        $im = imagecreatefrompng($url);        if ($im) {//              echo '<p>created image handle<br /></p>';                global $width, $height;                $width = imagesx($im);                $height = imagesy($im);                $x = $width/2;                $y = $height/2;                $dst = imagecreatetruecolor($x, $y);//                $background = imagecolorallocate($im, 0, 0, 0);//                imagecolortransparent($im, $background);//                imagealphablending($im, false);//                imagesavealpha($im, true);                imagecopyresampled($dst, $im, 0, 0, 0, 0, $x, $y, $width, $height);//              header("Content-Type:image/jpeg");                imagepng($dst, 'imgdst.png');//              imagejpeg($dst);                imagedestroy($dst);                imagedestroy($im);        }

标红的代码段就是可已经黑色背景去掉,并且将起设为透明的处理方法

参考了http://stackoverflow.com/questions/2611852/imagecreatefrompng-makes-a-black-background-instead-of-transparent

原创粉丝点击