php实现图片加水印
来源:互联网 发布:unity3d鼠标点击物体 编辑:程序博客网 时间:2024/06/11 18:40
1) 判断GD库是否加载,文件是否存在
2)获取图片的宽和高,并计算出水印字符的宽和高,进行比较$img_height<$font_width等时,直接返回,不加水印
3)根据图片类型加载响应的文件imagecreatefromXXXXX
4)如果目标文件不存在,则把水印加载在原始图片,如果保存为jpeg时的压缩小于0 或者大于100时,取边界值,
如果水印字符的大小小于0或者大于5时,取边界值,如果水印颜色不是7位的16进制格式时,直接返回
5)根据加水印的位置1,2,3,4,5,6,7非别对应左上,右上,中间,左下,右下,随机位置,单个水印字符的随机颜色
6)根据原始文件的类型,保存为对应的格式,如果目标文件未指定,则把水印加载在原始文件上面
<?phpfunction water($r_img, $d_img='',$gzip=80,$str="www.verycd.com", $f_size=5, $pos=5,$f_color="#ff0000"){ if(extension_loaded("gd") && file_exists($r_img)) { $img_info= getimagesize($r_img); $img_width= $img_info[0]; $img_height= $img_info[1]; if($f_size <1 ) $f_size=1; if($f_size>5) $f_size=5; $font_width= imagefontwidth($f_size)*strlen($str); $font_height= imagefontheight($f_size); if($img_width < $font_width && $img_height < $font_height) return false; if(strlen($f_color)!=7) return false; switch($img_info[2]) { case 1: $img = imagecreatefromgif($r_img); break; case 2: $img= imagecreatefromjpeg($r_img); break; case 3: $img= imagecreatefrompng($r_img); break; default: return false; } $r=hexdec( substr($f_color, 1,2)); $g =hexdec( substr($f_color, 3,2)); $b =hexdec(substr($f_color, 5,2)); $font_color= imagecolorallocate($img, $r, $g, $b); /* * pos=1 left_top * pos=2 right_top * pos=3 middle * pos=4 left_bottom * pos=5 right_bottom * pos=6 mt_rand * default mt_rand with color */ switch($pos) { case 1: imagestring($img, $f_size, 5, 5, $str, $font_color); break; case 2: imagestring($img, $f_size, $img_info[0]-$font_width, $font_height, $str, $font_color); break; case 3: imagestring($img, $f_size, ceil(($img_info[0]-$font_width)/2),ceil( ($img_info[1]-$font_height)/2), $str, $font_color); break; case 4: imagestring($img, $f_size,5, $img_info[1]-$font_height, $str, $font_color ); break; case 5: imagestring($img, $f_size, $img_info[0]-$font_width, $img_info[1]-$font_height, $str, $font_color); break; case 6: imagestring($img, $f_size, mt_rand(5, $img_info[0]-$font_width), mt_rand(5, $img_info[1]-$font_height), $str, $font_color); break; default : $len =strlen($str); for($i=0; $i<$len; $i++) { imagechar($img, $f_size, ceil(($img_info[0]-$font_width)/2),ceil( ($img_info[1]-$font_height)/2),$str[$i], imagecolorallocate($img, mt_rand(0,255),mt_rand(0,255), mt_rand(0,255))); } break; } $d_img = $d_img==''?$r_img:$d_img; if($gzip<0) $gzip=0; if($gzip>100) $gzip=100; switch($img_info[2]) { case 1: imagegif($img, $d_img); break; case 2: imagejpeg($img, $d_img, $gzip); break; case 3: imagepng($img, $d_img); break; } return true; } return false;}if(water("./image/1.jpeg")){ echo "add water successed";}else{ echo "add water failed";}?>
0 0
- php实现图片加水印
- PHP实现给图片加图片水印
- PHP实现给图片加文字水印
- php图片加水印
- PHP图片上传加水印
- 图片加水印上传(PHP)
- PHP-给图片加水印
- php 图片批量加水印
- PHP实例:实现给上传图片加水印图案
- PHP实例解析:实现给上传图片加水印图案
- PHP代码实现给图片加文字水印
- 图片加水印JAVA实现
- android实现图片加水印
- android 实现图片加水印
- Android 实现图片加水印
- 图片缩放 图片加水印 php类
- php给上传图片加图片水印
- PHP给图片加文字水印和图片水印
- Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tool
- [LeetCode] Remove Nth Node From End of List
- 给UIlabel设置不同字体 颜色 下划线
- 第十三周 项目四:数组的排序(1.4选择排序)
- 嗨图SDK,让你的APP”嗨“起来!
- php实现图片加水印
- dubbo的安装和使用
- JPA project Change Event Handler导致工具卡顿解决
- Codeforces Round #277.5 (Div. 2) A
- Productivity Power Tools for vs2010(代码预览工局 插件for vs2010)
- Spring声明式事务配置管理方法
- 配置SQL Server的身份验证方式
- HTML5学习之必记
- EasyDarwin开源团队招募开发组成员