最新下载
热门教程
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 
PHP给图片加水印实例函数教程
时间:2022-06-24 21:01:33 编辑:袖梨 来源:一聚教程网
在php中要实现图片增加水印我们要用到的函数有很多,imagecreatefromjpeg,imagecreatefrompng,getimagesize等等函数,这些都是属于php GD库的函数,所以我们必须在php.ini中打开GD库才可以让PHP使用这些函数生成图片水印了。
实现水印功能主要就是靠这些函数功能操作。
1.imagecreatefromjpeg //打开JPG图片
2.imagecreatefromgif //打开GIF图片
3.imagecreatefrompng //打开PNG图片
4.imagecreatefromwbmp //打开WBMP图片(比较少用)
5.getimagesize //获取图片大小信息
6.imagecopymerge //把多张图片整合(添加水印的主要函数)
7.imagejpeg //保存JPG图片
8.imagegif //保存GIF图片
9.imagepng //保存PNG图片
PHP实例代码如下:
- echo img_water_mark("1.jpg","walter.gif",null,"2.jpg",5,80);
 - /**
 - * 图片加水印(适用于png/jpg/gif格式)
 - *
 - * @author flynetcn
 - *
 - * @param $srcImg 原图片
 - * @param $waterImg 水印图片
 - * @param $savepath 保存路径
 - * @param $savename 保存名字
 - * @param $positon 水印位置
 - * 1:顶部居左, 2:顶部居右, 3:居中, 4:底部局左, 5:底部居右
 - * @param $alpha 透明度 -- 0:完全透明, 100:完全不透明
 - *
 - * @return 成功 -- 加水印后的新图片地址
 - * 失败 -- -1:原文件不存在, -2:水印图片不存在, -3:原文件图像对象建立失败
 - * -4:水印文件图像对象建立失败 -5:加水印后的新图片保存失败
 - */
 - function img_water_mark($srcImg, $waterImg, $savepath=null, $savename=null, $positon=5, $alpha=30)
 - {
 - $temp = pathinfo($srcImg);
 - $name = $temp[basename];
 - $path = $temp[dirname];
 - $exte = $temp[extension];
 - $savename = $savename ? $savename : $name;
 - $savepath = $savepath ? $savepath : $path;
 - $savefile = $savepath ."/". $savename;
 - $srcinfo = @getimagesize($srcImg);
 - if (!$srcinfo) {
 - return -1; //原文件不存在
 - }
 - $waterinfo = @getimagesize($waterImg);
 - if (!$waterinfo) {
 - return -2; //水印图片不存在
 - }
 - $srcImgObj = image_create_from_ext($srcImg);
 - if (!$srcImgObj) {
 - return -3; //原文件图像对象建立失败
 - }
 - $waterImgObj = image_create_from_ext($waterImg);
 - if (!$waterImgObj) {
 - return -4; //水印文件图像对象建立失败
 - }
 - switch ($positon) {
 - //1顶部居左
 - case 1: $x=$y=0; break;
 - //2顶部居右
 - case 2: $x = $srcinfo[0]-$waterinfo[0]; $y = 0; break;
 - //3居中
 - case 3: $x = ($srcinfo[0]-$waterinfo[0])/2; $y = ($srcinfo[1]-$waterinfo[1])/2; break;
 - //4底部居左
 - case 4: $x = 0; $y = $srcinfo[1]-$waterinfo[1]; break;
 - //5底部居右
 - case 5: $x = $srcinfo[0]-$waterinfo[0]; $y = $srcinfo[1]-$waterinfo[1]; break;
 - default: $x=$y=0;
 - }
 - imagecopymerge($srcImgObj, $waterImgObj, $x, $y, 0, 0, $waterinfo[0], $waterinfo[1], $alpha);
 - switch ($srcinfo[2]) {
 - case 1: imagegif($srcImgObj, $savefile); break;
 - case 2: imagejpeg($srcImgObj, $savefile); break;
 - case 3: imagepng($srcImgObj, $savefile); break;
 - default: return -5; //保存失败
 - }
 - imagedestroy($srcImgObj);
 - imagedestroy($waterImgObj);
 - return $savefile;
 - }//开源代码phpfensi.com
 - function image_create_from_ext($imgfile)
 - {
 - $info = getimagesize($imgfile);
 - $im = null;
 - switch ($info[2]) {
 - case 1: $im=imagecreatefromgif($imgfile); break;
 - case 2: $im=imagecreatefromjpeg($imgfile); break;
 - case 3: $im=imagecreatefrompng($imgfile); break;
 - }
 - return $im;
 - }
 
目前支持JPG,GIF,PNG等图片格式。
用法举例,代码如下:
- if($pic = watermark('./image.jpg','./watermark.png'))
 - {
 - echo ' . '" border=0 />' ;
 - }
 - else
 - {
 - echo '';
 - }
 
下面演示一个完整全水印增加函数,代码如下:
- /**************************************************************
 - 参数说明:
 - $max_file_size : 上传文件大小限制, 单位BYTE
 - $destination_folder : 上传文件路径
 - $watermark : 是否附加水印(1为加水印,其他为不加水印);
 - 使用说明:
 - 1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;
 - 2. 将extension_dir =改为你的php_gd2.dll所在目录;
 - **************************************************************/
 - //上传文件类型列表
 - $uptypes=array(
 - 'image/jpg',
 - 'image/jpeg',
 - 'image/png',
 - 'image/pjpeg',
 - 'image/gif',
 - 'image/bmp',
 - 'image/x-png'
 - );
 - $max_file_size=2000000; //上传文件大小限制, 单位BYTE
 - $destination_folder="uploadimg/"; //上传文件路径
 - $watermark=1; //是否附加水印(1为加水印,其他为不加水印);
 - $watertype=1; //水印类型(1为文字,2为图片)
 - $waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
 - $waterstring="https://www.111cn.net/"; //水印字符串
 - $waterimg="xplore.gif"; //水印图片
 - $imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
 - $imgpreviewsize=1/1; //缩略图比例
 - ?>
 图片打水印程序演示!WWW.MOP8.COM - >
 - method="post" name="upform">
 - 上传文件:
 - "upfile" type="file">
 - "submit" value="上传">
 
- 允许上传的文件类型为:=implode(', ',$uptypes)?>
 - "#FF0000">本演示空间由TuWoo提供,本程序采用文字水印的方式.
 - if ($_SERVER['REQUEST_METHOD'] == 'POST')
 - {
 - if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
 - //是否存在文件
 - {
 - echo "图片不存在!";
 - exit;
 - }
 - $file = $_FILES["upfile"];
 - if($max_file_size $file["size"])
 - //检查文件大小
 - {
 - echo "文件太大!";
 - exit;
 - }
 - if(!in_array($file["type"], $uptypes))
 - //检查文件类型
 - {
 - echo "文件类型不符!".$file["type"];
 - exit;
 - }
 - if(!file_exists($destination_folder))
 - {
 - mkdir($destination_folder);
 - }
 - $filename=$file["tmp_name"];
 - $image_size = getimagesize($filename);
 - $pinfo=pathinfo($file["name"]);
 - $ftype=$pinfo['extension'];
 - $destination = $destination_folder.time().".".$ftype;
 - if (file_exists($destination) && $overwrite != true)
 - {
 - echo "同名文件已经存在了";
 - exit;
 - }
 - if(!move_uploaded_file ($filename, $destination))
 - {
 - echo "移动文件出错";
 - exit;
 - }
 - $pinfo=pathinfo($destination);
 - $fname=$pinfo[basename];
 - echo " 已经成功上传
 
文件名: ".$destination_folder.$fname."
";- echo " 宽度:".$image_size[0];
 - echo " 长度:".$image_size[1];
 - echo "
 
大小:".$file["size"]." bytes";- if($watermark==1)
 - {
 - $iinfo=getimagesize($destination,$iinfo);
 - $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
 - $white=imagecolorallocate($nimage,255,255,255);
 - $black=imagecolorallocate($nimage,0,0,0);
 - $red=imagecolorallocate($nimage,255,0,0);
 - imagefill($nimage,0,0,$white);
 - switch ($iinfo[2])
 - {
 - case 1:
 - $simage =imagecreatefromgif($destination);
 - break;
 - case 2:
 - $simage =imagecreatefromjpeg($destination);
 - break;
 - case 3:
 - $simage =imagecreatefrompng($destination);
 - break;
 - case 6:
 - $simage =imagecreatefromwbmp($destination);
 - break;
 - default:
 - die("不支持的文件类型");
 - exit;
 - }
 - imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
 - imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
 - switch($watertype)
 - {
 - case 1: //加水印字符串
 - imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
 - break;
 - case 2: //加水印图片
 - $simage1 =imagecreatefromgif("xplore.gif");
 - imagecopy($nimage,$simage1,0,0,0,0,85,15);
 - imagedestroy($simage1);
 - break;
 - }
 - switch ($iinfo[2])
 - {
 - case 1:
 - //imagegif($nimage, $destination);
 - imagejpeg($nimage, $destination);
 - break;
 - case 2:
 - imagejpeg($nimage, $destination);
 - break;
 - case 3:
 - imagepng($nimage, $destination);
 - break;
 - case 6:
 - imagewbmp($nimage, $destination);
 - //imagejpeg($nimage, $destination);
 - break;
 - }
 - //覆盖原上传文件
 - imagedestroy($nimage);
 - imagedestroy($simage);
 - }
 - if($imgpreview==1)
 - {
 - echo "
 
图片预览:
";- echo "
 ".$destination."" vars">$image_size[0]*$imgpreviewsize)." vars">$image_size[1]*$imgpreviewsize);
- echo " alt="图片预览:r文件名:".$destination."r上传时间:">";
 - }
 - }
 - ?>
 
相关文章
- 洛克王国世界火神怎么觉醒-火神觉醒攻略 11-04
 - 王者万象棋公孙离强度如何 11-04
 - 航海王壮志雄心阿保有什么玩法技巧 11-04
 - 星塔旅人菈露有什么技能 11-04
 - 潮汐守望者希瑟拉强度怎么样 11-04
 - 洛克王国世界刺电环如何进化 11-04