- 论坛徽章:
- 0
|
本帖最后由 zcstrong 于 2011-12-04 20:54 编辑
PHP增加图片透明背景 - 众长思壮工作室
大家好,提供大家一个方便的函数,将一张图片增加一个透明的背景,所生成的图片是PNG格式。
博客地址:PHP增加图片透明背景
1.如何引用- <?PHP
- #参数一:所要修改图片地址及名称;
- #参数二:所要生成图片的名称(不需要后缀);
- #参数三:宽度;
- #参数四:高度;
- overlayjpg("icon_error.gif","1234","800","800");
- ?>
复制代码 2.具体方法- <?php
- function overlayjpg($imgsrc,$imgdst,$width,$height="")
- {
- //$imgsrc jpg格式图像路径 $imgdst 图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度
- //取得图片的宽度,高度值
- $arr = getimagesize($imgsrc);
- //计算图片X轴位置
- $img_X = ($width - $arr[0])/2;
- if($height == ""){
- $heights = $arr[1];
- $img_Y = 0;
- }
- else{
- if($height <= $arr[1]){
- $heights = $arr[1];
- $img_Y = 0;
- }
- else{
- $heights = $height;
- $img_Y = ($height - $arr[1])/2;
- }
- }
- $image = imagecreatetruecolor($width,$heights); //创建一个彩色的底图
- imagealphablending($image, false);
- imagesavealpha($image, true);
- $white = imagecolorallocatealpha($image,255,255,255,127);
- imagefill($image,0,0,$white);
- $imgsrc = LoadIMG($imgsrc,$arr['mime']);
- imagecopy($image,$imgsrc,$img_X,$img_Y,0,0,$arr[0],$arr[1]);
- //imagejpeg($image,$imgdst,90);
- imagepng($image,$imgdst.".png");
- imagedestroy($image);
- }
- function LoadIMG($imgname,$mime)
- {
- if($mime == "image/gif"){
- $im = @imagecreatefromgif($imgname); /* Attempt to open */
- }
- elseif ($mime == "image/png"){
- $im = @imagecreatefrompng($imgname); /* Attempt to open */
- }
- else{
- $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
- }
- if(!$im) { /* See if it failed */
- $im = imagecreatetruecolor(150, 30); /* Create a blank image */
- $bgc = imagecolorallocate($im, 255, 255, 255);
- $tc = imagecolorallocate($im, 0, 0, 0);
- imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
- /* Output an errmsg */
- imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
- }
- return $im;
- }
- ?>
复制代码 |
|