PHP图片添加水印

北风网AD
一共有3个文件: 1、functions.php 2、water.config.php 3、water.class.php 代码如下: functions.php
  1. <?php
  2. /*
  3. *
  4. */
  5. //获得数据库模型的对象
  6. function M($table){
  7. return new Model($table);
  8. }
  9. //打印信息
  10. function p($msg){
  11. echo "<pre>";
  12. print_r($msg);
  13. echo "</pre>";
  14. }http://kmnk03.com/hxpfk/mny/48.html
  15. //提示错误信息
  16. function error($msg){
  17. echo "<div align='center' style='border:solid 3px #dcdcdc;padding:20px;margin:0 auto;'>
  18. $msg
  19. </div>";
  20. exit;
  21. }
  22. //自动加载类文件
  23. function __autoload($className){
  24. if(strlen($className)>7 && substr($className,-7) == 'Control'){
  25. $classFile = PATH_APP.'/control/'.$className.'.php';
  26. }else{
  27. $classFile = PATH_XL.'/libs/bin/'.$className.'.class.php';
  28. }http://kmnk03.com/hxpfk/bpy/119.html
  29. if(!is_file($classFile)){
  30. exit($classFile."文件不存在");
  31. }
  32. include $classFile;
  33. }
  34. //加载、设置配置项
  35. function C($name=null,$value=null){
  36. static $config =array();
  37. if(is_array($name)){
  38. $config = array_merge($config,array_change_key_case($name));
  39. }
  40. if(is_string($name)){
  41. $name = strtolower($name);
  42. if(!is_null($value)){
  43. $congfig[$name]=$value;
  44. return true;
  45. }
  46. return isset($config[$name])?$config[$name]:null;
  47. }
  48. if(is_null($name)){
  49. return $config;
  50. }
  51. }
  52. //加载、设置字体
  53. function L($name=null,$value=null){
  54. static $lang = array();
  55. if(is_array($name)){
  56. $lang = array_merge($lang,array_change_key_case($name));
  57. }
  58. if(is_string($name)){
  59. $name = strtolower($name);
  60. if(!is_null($value)){
  61. $lang[$name]=$value;
  62. return true;
  63. }
  64. return isset($lang[$name])?$lang[$name]:null;
  65. }
  66. if(is_null($name)){
  67. return $lang;
  68. }
  69. }http://kmnk03.com/hxpfk/bpy/120.html
  70. //获得文件、文件夹的大小
  71. function getsize($path,$type=null){
  72. if(is_file($path)) return filesize($path);
  73. $type = is_null($type)?"*":"*.$type";
  74. $files = glob($path.'/'.$type);
  75. $size = 0;
  76. foreach($files as $f){
  77. $size+=is_dir($f)?getsize($f):filesize($f);
  78. }
  79. return $size;
  80. }
  81. ?>
复制代码

water.config.php
  1. <?php
  2. return array(
  3. //水印处理配置项
  4. 'water_img' => 'water.jpg',//水印图片
  5. 'water_pos' => '9',//水印位置 取值范围:1-9
  6. /* 1-9的位置分别为http://kmnk03.com/hxpfk/npx/123.html
  7. ↖ ↑ ↗
  8. ← · →
  9. ↙ ↓ ↘
  10. */
  11. 'water_pct' => 60,//水印透明度 取值范围:0-100 (值越大、水印越清晰)
  12. );
  13. ?>
复制代码

water.class.php
  1. <?php
  2. include "functions.php";//加载小工具函数
  3. C(include "water.config.php");//读取水印类配置项
  4. //水印处理类
  5. class waterd{
  6. public $water_pos='';//水印位置
  7. public $water_pct='';//水印透明度
  8. private $res;//图像资源
  9. //构造函数
  10. function __construct($pos=null,$pct=null){
  11. $this->water_pos = is_null($pos)?C('water_pos'):$pos;
  12. $this->water_pct = is_null($pct)?C('water_pct'):$pct;
  13. }http://kmnk03.com/hxpfk/bpy/122.html
  14. //添加水印方法
  15. public function water($img){
  16. //检测
  17. if(!$this->check($img)){
  18. return false;
  19. }
  20. //水印图片资源
  21. $water_res = $this -> getres(C('water_img'));
  22. //原始图片资源
  23. $img_res = $this -> getres($img);
  24. //水印位置
  25. $posArr = $this -> getpos($img_res,$water_res);
  26. imagecopymerge($img_res,$water_res,$posArr[0],$posArr[1],0,0,imagesx($water_res),imagesy($water_res),$this->water_pct);
  27. $info = getimagesize($img);
  28. //打印图片信息 测试时可开启当前打印 p($info);
  29. $func = str_replace('/','',$info['mime']);
  30. $func($img_res,$img);
  31. }http://kmnk03.com/hxpfk/npx/128.html
  32. //检测
  33. private function check($img){
  34. return is_file($img) && extension_loaded('GD') && getimagesize($img);
  35. }
  36. //获得图片资源
  37. private function getres($img){
  38. $info = getimagesize($img);
  39. $type = trim(image_type_to_extension($info[2]),'.');
  40. $func = 'imagecreatefrom'.$type;
  41. return $func($img);
  42. }
  43. //获得水印位置
  44. private function getpos($img_res,$water_res){
  45. $img_x = imagesx($img_res);//原图宽度
  46. $img_y = imagesy($img_res);//原图宽度
  47. $water_x = imagesx($water_res);//水印宽度
  48. $water_y = imagesy($water_res);//水印宽度
  49. $pos = $this -> water_pos;//水印位置
  50. $x=15;$y=15;
  51. switch($pos){
  52. case 1:
  53. break;
  54. case 2:
  55. $x = ($img_x-$water_x)/2;
  56. break;
  57. case 3:
  58. $x = $img_x-$water_x-15;
  59. break;
  60. case 4:
  61. $y = ($img_y-$water_y)/2;
  62. break;http://kmnk03.com/hxpfk/npx/125.html
  63. case 5:
  64. $x = ($img_x-$water_x)/2;
  65. $y = ($img_y-$water_y)/2;
  66. break;
  67. case 6:
  68. $x = $img_x-$water_x-15;
  69. $y = ($img_y-$water_y)/2;
  70. break;
  71. case 7:http://kmnk03.com/hxpfk/bpy/127.html
  72. $y = $img_y-$water_y-15;
  73. break;
  74. case 8:
  75. $x = ($img_x-$water_x)/2;
  76. $y = $img_y-$water_y-15;
  77. break;
  78. case 9:
  79. $x = $img_x-$water_x-15;
  80. $y = $img_y-$water_y-15;
  81. break;
  82. }
  83. return array($x,$y);
  84. }http://kmnk03.com/hxpfk/hhb/126.html
  85. }kmnk03.com
  86. //new一个对象然后 调用 水印的方法 water()即可、传进要添加水印的图片即可
  87. $x = new waterd();
  88. $x->water('img.jpg');
  89. www.kmnk03.com
  90. ?>
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值