PNG,GIF缩略图背景透明函数.(都支持,就差一个缺点了[黑色的处理])

Dleno 2009-11-18 02:45:11
突然需要这个功能,找了别人的一个函数来用,才发现gif和png北京都是黑色.
然后就改,改到现在都可以实现了.
但就是gif的时候,对黑色的处理不好.把图片的黑色也给透明了.

有哪位高手解决下这个问题.

<?PHP
/*
$source_img = "../base_img/index.jpg";
$target_dir='../base_img/';
$new_width=500;
$new_height=500;
$if_cut=0;
$target_name='HanroadClass';
$img=new HanroadClass;
$img->HrResize($source_img,$target_dir,$target_name,$new_width,$new_height,$if_cut);
*/
class HanroadClass
{
/**//**********************
利用PHP的GD库生成缩略图。
支持图片格式:jpg,gif,png
$source_img: 源图象完整路径
$target_dir: 目标图象目录
$target_name: 目标图象名称
$new_width: 目标图象宽
$new_height: 目标图象高
$if_cut: 是否裁图
1(裁图): 裁图则按设置的大小生成目标图象
0(不裁): 不裁则按比例生成目标图象
**********************/
function HrResize($source_img,$target_dir,$target_name,$new_width,$new_height,$if_cut) {
//图片类型
$img_type = strtolower(substr(strrchr($source_img,"."),1));

//图象的完整目标路径
$tar_url = $target_dir."/".$target_name.".".$img_type;

//初始化图象
if($img_type=="jpg")$temp_img = imagecreatefromjpeg($source_img);
if($img_type=="gif")$temp_img = imagecreatefromgif($source_img);
if($img_type=="png")$temp_img = imagecreatefrompng($source_img);


//原始图象的宽和高
$old_width = imagesx($temp_img);
$old_height = imagesy($temp_img);

//改变前后的图象的比例
$new_ratio = $new_width/$new_height;
$old_ratio = $old_width/$old_height;

//生成新图象的参数
//情况一:裁图 则按设置的大小生成目标图象
if($if_cut=="1"){
$new_width = $new_width;
$new_height = $new_height;
//高度优先
if($old_ratio>=$new_ratio){
$old_width = $old_height*$new_ratio;
$old_height = $old_height;
}else{//宽度优先
$old_width = $old_width;
$old_height = $old_width/$new_ratio;
}
}else{//情况二:不裁图 则按比例生成目标图象
$old_width = $old_width;
$old_height = $old_height;
//高度优先
if($old_ratio>=$new_ratio) {
$new_width = $new_width;
$new_height = $new_width/$old_ratio;
}else{//宽度优先
$new_width = $new_height*$old_ratio;
$new_height = $new_height;
}
}
//生成新图片
$new_img = imagecreatetruecolor($new_width,$new_height);
if($img_type=="gif"){
imagealphablending($new_img, false);
imagesavealpha($new_img, true);
}
if($img_type=="png"){
imagesavealpha($temp_img,true);
imagealphablending($new_img,false);
imagesavealpha($new_img,true);
}
imagecopyresampled($new_img,$temp_img,0,0,0,0,$new_width,$new_height,$old_width,$old_height);

if($img_type=="jpg")imagejpeg($new_img,$tar_url);
if($img_type=="gif"){
$bgcolor=imagecolorallocate($new_img,0,0,0);
$bgcolor=imagecolortransparent($new_img,$bgcolor);
$bgcolor=imagecolorallocatealpha($new_img, 0, 0, 0,127);
imagefill($new_img, 0, 0, $bgcolor);
imagegif($new_img,$tar_url);
}
if($img_type=="png")imagepng($new_img,$tar_url);
imagedestroy($source_img);
imagedestroy($new_img);
}
}
?>


...全文
435 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
爱学堂 2011-12-05
  • 打赏
  • 举报
回复
去除 if($img_type=="gif"){
imagealphablending($new_img, false);
imagesavealpha($new_img, true);
}
去除 $bgcolor=imagecolorallocatealpha($new_img, 0, 0, 0,127);
imagefill($new_img, 0, 0, $bgcolor);
就行了,我试过了,你准备给我几分
iwantnet 2009-12-02
  • 打赏
  • 举报
回复
帮顶!
Dleno 2009-12-02
  • 打赏
  • 举报
回复
这么久了都还没有人!
顶一下
Dleno 2009-11-18
  • 打赏
  • 举报
回复
没有人?

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧