Chinaunix首页 | 论坛 | 博客
  • 博客访问: 11567
  • 博文数量: 4
  • 博客积分: 105
  • 博客等级: 民兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-25 21:44
文章分类
文章存档

2011年(4)

我的朋友
最近访客

分类: 系统运维

2011-12-01 21:22:28

大家好,提供大家一个方便的函数,将一张图片增加一个透明的背景,所生成的图片是PNG格式。

1.如何引用
  1. <?
  2. #参数一:所要修改图片地址及名称;
  3. #参数二:所要生成图片的名称(不需要后缀);
  4. #参数三:宽度;
  5. #参数四:高度;
  6. overlayjpg("icon_error.gif","1234","800","800");
  7. ?>
2.具体方法
  1. <?php
  2. function overlayjpg($imgsrc,$imgdst,$width,$height="")
  3. {
  4. //$imgsrc jpg格式图像路径 $imgdst 图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度
  5. //取得图片的宽度,高度值
  6. $arr = getimagesize($imgsrc);
  7. //计算图片X轴位置
  8. $img_X = ($width - $arr[0])/2;
  9. if($height == ""){
  10.     $heights = $arr[1];
  11.     $img_Y = 0;
  12. }
  13. else{
  14.     if($height <= $arr[1]){
  15.         $heights = $arr[1];
  16.         $img_Y = 0;
  17.     }
  18.     else{
  19.         $heights = $height;
  20.         $img_Y = ($height - $arr[1])/2;
  21.     }
  22. }

  23. $image = imagecreatetruecolor($width,$heights); //创建一个彩色的底图
  24. imagealphablending($image, false);
  25. imagesavealpha($image, true);
  26. $white = imagecolorallocatealpha($image,255,255,255,127);
  27. imagefill($image,0,0,$white);
  28. $imgsrc = LoadIMG($imgsrc,$arr['mime']);
  29. imagecopy($image,$imgsrc,$img_X,$img_Y,0,0,$arr[0],$arr[1]);
  30. //imagejpeg($image,$imgdst,90);
  31. imagepng($image,$imgdst.".png");
  32. imagedestroy($image);
  33. }

  34. function LoadIMG($imgname,$mime)
  35. {
  36.     if($mime == "image/gif"){
  37.         $im = @imagecreatefromgif($imgname); /* Attempt to open */
  38.     }
  39.     elseif ($mime == "image/png"){
  40.         $im = @imagecreatefrompng($imgname); /* Attempt to open */
  41.     }
  42.     else{
  43.         $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
  44.     }
  45.     if(!$im) { /* See if it failed */
  46.         $im = imagecreatetruecolor(150, 30); /* Create a blank image */
  47.         $bgc = imagecolorallocate($im, 255, 255, 255);
  48.         $tc = imagecolorallocate($im, 0, 0, 0);
  49.         imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
  50.         /* Output an errmsg */
  51.         imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
  52.     }
  53.     return $im;
  54. }
  55. ?>




阅读(1956) | 评论(0) | 转发(0) |
0

上一篇:PHP生成透明背景

下一篇:没有了

给主人留下些什么吧!~~