在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);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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) {
-
- case 1: $x=$y=0; break;
-
- case 2: $x = $srcinfo[0]-$waterinfo[0]; $y = 0; break;
-
- case 3: $x = ($srcinfo[0]-$waterinfo[0])/2; $y = ($srcinfo[1]-$waterinfo[1])/2; break;
-
- case 4: $x = 0; $y = $srcinfo[1]-$waterinfo[1]; break;
-
- 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;
- }
-
- 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 '<img src="' . $pic . '" border=0 />' ;
- }
- else
- {
- echo '<img src="./image.jpg" border=0 />';
- }
下面演示一个完整全水印增加函数,代码如下:
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
- $uptypes=array(
- 'image/jpg',
- 'image/jpeg',
- 'image/png',
- 'image/pjpeg',
- 'image/gif',
- 'image/bmp',
- 'image/x-png'
- );
-
- $max_file_size=2000000;
- $destination_folder="uploadimg/";
- $watermark=1;
- $watertype=1;
- $waterposition=1;
- $waterstring="http://www.111cn.net/"; //水印字符串
- $waterimg="xplore.gif";
- $imgpreview=1;
- $imgpreviewsize=1/1;
- ?>
- <html>
- <head>
- <title>图片打水印程序演示!WWW.MOP8.COM</title>
- <style type="text/css">
- <!--
- body
- {
- font-size: 9pt;
- }
- input
- {
- background-color: #66CCFF;
- border: 1px inset #CCCCCC;
- }
- -->
- </style>
- </head>
-
- <body>
- <center>
- <form enctype="multipart/form-data" method="post" name="upform">
- 上传文件:
- <input name="upfile" type="file">
- <input type="submit" value="上传"><P>
- 允许上传的文件类型为:<?=implode(', ',$uptypes)?>
- </form>
- <FONT COLOR="#FF0000">本演示空间由TuWoo提供,本程序采用文字水印的方式.</FONT></CENTER>
- <?php
- 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 " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";
- echo " 宽度:".$image_size[0];
- echo " 长度:".$image_size[1];
- echo "<br> 大小:".$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:
-
- imagejpeg($nimage, $destination);
- break;
- case 2:
- imagejpeg($nimage, $destination);
- break;
- case 3:
- imagepng($nimage, $destination);
- break;
- case 6:
- imagewbmp($nimage, $destination);
-
- break;
- }
-
-
- imagedestroy($nimage);
- imagedestroy($simage);
- }
-
- if($imgpreview==1)
- {
- echo "<br>图片预览:<br>";
- echo "<img src="".$destination."" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
- echo " alt="图片预览:r文件名:".$destination."r上传时间:">";
- }
- }
- ?>
- </body>
- </html>
|