例1,很简单图下添加水印函数
- <?php
-
-
-
-
-
- function addWaterMap($source_img, $water_map)
- {
- $img = imagecreatefromjpeg($source_img);
- $img_x = imagesx($img);
- $img_y = imagesy($img);
- imagealphablending($img, true);
-
- $img_water_map = imagecreatefrompng($water_map);
- $water_x = imagesx($img_water_map);
- $water_y = imagesy($img_water_map);
- $wimg_x = $img_x - $water_x - 10;
- $wimg_y = $img_y - $water_y - 10;
- imagecopy($img, $img_water_map, $wimg_x, $wimg_y, 0, 0, $water_x, $water_y);
- imagejpeg($img, $source_img, 95);
- imagedestroy($img);
- imagedestroy($img_water_map);
- return true;
- }
- ?>
实例2,一个比较完整图片增加水印类
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
- }
(责任编辑:admin) |