求教:读入一幅gif图片,如何把其中5种颜色替换成透明?
时间:2013-05-28
来源:互联网
如题,有一张名为‘1.gif’的512*512的图片,要把图中的5种颜色替换成透明,然后输出为1.png的图片,五种颜色如下:
1、rgb=4,2,4
2、rgb=252,230,180
3、rgb=252,254,252
4、rgb=252,162,124
5、rgb=124,254,212
真心求教,谢谢高手!
1、rgb=4,2,4
2、rgb=252,230,180
3、rgb=252,254,252
4、rgb=252,162,124
5、rgb=124,254,212
真心求教,谢谢高手!
作者: kvkvman 发布时间: 2013-05-28
class MyImage{
static function ImageColorAllocateHEX($im, $s){
if($s{0} == "#") $s = substr($s,1);
$bg_dec = hexdec($s);
return imagecolorallocate($im,
($bg_dec & 0xFF0000) >> 16,
($bg_dec & 0x00FF00) >> 8,
($bg_dec & 0x0000FF)
);
}
static function imagetextouter(&$im, $size, $x, $y, $color, $fontfile, $text, $outer){
$ttf = false;
if (is_file($fontfile)){
$ttf = true;
$area = imagettfbbox($size, 0, $fontfile, $text);
$width = $area[2] - $area[0] + 2;
$height = $area[1] - $area[5] + 2;
} else {
$width = strlen($text) * 10;
$height = 16;
}
$im_tmp = imagecreate($width, $height);
$white = imagecolorallocate($im_tmp, 255, 255, 255);
$black = imagecolorallocate($im_tmp, 0, 0, 0);
$color = self::ImageColorAllocateHEX($im, $color);
$outer = self::ImageColorAllocateHEX($im, $outer);
if ($ttf){
imagettftext($im_tmp, $size, 0, 0, $height - 2, $black, $fontfile, $text);
imagettftext($im, $size, 0, $x, $y, $color, $fontfile, $text);
$y = $y - $height + 2;
}
else{
imagestring($im_tmp, $size, 0, 0, $text, $black);
imagestring($im, $size, $x, $y, $text, $color);
}
for ($i = 0; $i < $width; $i ++){
for ($j = 0; $j < $height; $j ++){
$c = ImageColorAt($im_tmp, $i, $j);
if ($c !== $white){
ImageColorAt ($im_tmp, $i, $j - 1) != $white || imagesetpixel($im, $x + $i, $y + $j - 1, $outer);
ImageColorAt ($im_tmp, $i, $j + 1) != $white || imagesetpixel($im, $x + $i, $y + $j + 1, $outer);
ImageColorAt ($im_tmp, $i - 1, $j) != $white || imagesetpixel($im, $x + $i - 1, $y + $j, $outer);
ImageColorAt ($im_tmp, $i + 1, $j) != $white || imagesetpixel($im, $x + $i + 1, $y + $j, $outer);
// 取消注释,与Fireworks的发光效果相同
ImageColorAt ($im_tmp, $i - 1, $j - 1) != $white || imagesetpixel($im, $x + $i - 1, $y + $j - 1, $outer);
ImageColorAt ($im_tmp, $i + 1, $j - 1) != $white || imagesetpixel($im, $x + $i + 1, $y + $j - 1, $outer);
ImageColorAt ($im_tmp, $i - 1, $j + 1) != $white || imagesetpixel($im, $x + $i - 1, $y + $j + 1, $outer);
ImageColorAt ($im_tmp, $i + 1, $j + 1) != $white || imagesetpixel($im, $x + $i + 1, $y + $j + 1, $outer);
}
}
}
imagedestroy($im_tmp);
}
}你要的东西在这个例子里可以找到思路,具体的自己动下手呗,呵呵
作者: asmodai 发布时间: 2013-05-28
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28















