php实现图片上传时添加文字和图片水印技巧


Posted in PHP onApril 18, 2020

本文实现的功能特别适用于一些商城和图片站中,分享了图片在上传时添加文字和图片水印的技巧,供大家参考,具体内容如下

1. water.class.php

<?php
header('Content-Type:text/html;charset=utf-8');
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//给图片添加水印
Class Water{
 //开启水印
 private $watermark_on = '1';
  
 public $water_img;
  
 //水印位置
 public $pos = 1; 
  
 //压缩比
 public $pct = 80;
  
 //透明度
 public $quality = 80;
  
 public $text = '乐趣网zlblog.sinaapp.com';
  
 public $size = 12;
  
 public $color = '#000000';
  
 public $font = 'font.ttf';
  
 public function watermark( $img,$pos='',$out_img='',$water_img='',$text='' ){
  if(!$this->check($img) || !$this->watermark_on) return false;
   
  $water_img = $water_img ? $water_img : $this->water_img;
  //水印的开启状态
  $waterimg_on = $this->check($water_img) ? 1 : 0;
  //判断是否在原图上操作
  $out_img = $out_img ? $out_img : $img;
  //判断水印的位置
  $pos = $pos ? $pos : $this->pos;
  //水印文字
  $text = $text ? $text : $this->text;
   
   
  $img_info = getimagesize($img);
  $img_w = $img_info[0];
  $img_h = $img_info[1];
  //判断水印图片的类型
   
   
  if( $waterimg_on ){
   $w_info = getimagesize($water_img);
   $w_w = $w_info[0];
   $w_h = $w_info[1];
   if ( $img_w < $w_w || $img_h < $w_h ) return false;
   switch ( $w_info[2] ){
    case 1:
     $w_img = imagecreatefromgif($water_img);
     break;
    case 2:
     $w_img = imagecreatefromjpeg($water_img);
     break;
    case 3:
     $w_img = imagecreatefrompng($water_img);
     break;
   }
  }else{
   if( empty($text) || strlen($this->color)!=7 ) return FALSE;
   $text_info = imagettfbbox($this->size, 0, $this->font, $text);
   $w_w = $text_info[2] - $text_info[6];
   $w_h = $text_info[3] - $text_info[7];
  }
   
  //建立原图资源
   
  switch ( $img_info[2] ){
   case 1:
    $res_img = imagecreatefromgif($img);
    break;
   case 2:
    $res_img = imagecreatefromjpeg($img);
    break;
   case 3:
    $res_img = imagecreatefrompng($img);
    break;
  }
  //确定水印的位置
  switch ( $pos ){
   case 1:
    $x = $y =25;
    break;
   case 2:
    $x = ($img_w - $w_w)/2; 
    $y = 25;
    break;
   case 3:
    $x = $img_w - $w_w;
    $y = 25;
    break;
   case 4:
    $x = 25;
    $y = ($img_h - $w_h)/2;
    break;
   case 5:
    $x = ($img_w - $w_w)/2; 
    $y = ($img_h - $w_h)/2;
    break;
   case 6:
    $x = $img_w - $w_w;
    $y = ($img_h - $w_h)/2;
    break;
   case 7:
    $x = 25;
    $y = $img_h - $w_h;
    break;
   case 8:
    $x = ($img_w - $w_w)/2;
    $y = $img_h - $w_h;
    break;
   case 9:
    $x = $img_w - $w_w;
    $y = $img_h - $w_h;
    break;
   default :
    $x = mt_rand(25, $img_w - $w_w);
    $y = mt_rand(25, $img_h - $w_h);
  }
   
  //写入图片资源
  if( $waterimg_on ){
   imagecopymerge($res_img, $w_img, $x, $y, 0, 0, $w_w, $w_h, $this->pct); 
 }else{
  $r = hexdec(substr($this->color, 1,2));
  $g = hexdec(substr($this->color, 3,2));
  $b = hexdec(substr($this->color, 5,2));
  $color = imagecolorallocate($res_img, $r, $g, $b);
  imagettftext($res_img, $this->size, 0, $x, $y, $color, $this->font, $text); 
 }
  
 //生成图片类型
 switch ( $img_info[2] ){
  case 1:
   imagecreatefromgif($res_img,$out_img);
   break;
  case 2:
   //imagecreatefromjpeg($res_img,$out_img);
   imagejpeg($res_img,$out_img);
   break;
  case 3:
   imagejpeg($res_img,$out_img);
   break;
 }
 if(isset($res_img)) imagedestroy ($res_img);
 if(isset($w_img)) imagedestroy($w_img);
 return TRUE;
} 
 //验证图片是否存在
  private function check($img){
   $type = array('.jpg','.jpeg','.png','.gif');
   $img_type = strtolower(strrchr($img, '.'));
   return extension_loaded('gd') && file_exists($img) && in_array($img_type, $type);
  } 
}

2. test1.php

<?php
 
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//header('Content-Type:text/html;charset=utf-8');
include 'water.class.php';
$image = new Water();
$image->watermark('12.jpg',5);
//$image->watermark('12.jpg',1);

3.效果图

php实现图片上传时添加文字和图片水印技巧

以上就是本文的全部内容,希望对大家学习PHP程序设计有所帮助。

PHP 相关文章推荐
利用discuz实现PHP大文件上传应用实例代码
Nov 14 PHP
使用XDebug调试及单元测试覆盖率分析
Jan 27 PHP
php中通过curl模拟登陆discuz论坛的实现代码
Feb 16 PHP
php中取得文件的后缀名?
Feb 20 PHP
php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法
Nov 04 PHP
PHP将页面中点击数量高的链接进行高亮显示的方法
May 30 PHP
php cookie工作原理与实例详解
Jul 18 PHP
使用PHP免费发送定时短信的实例
Oct 24 PHP
php的laravel框架快速集成微信登录的方法
Dec 12 PHP
Thinkphp框架中D方法与M方法的区别
Dec 23 PHP
基于PHP实现微信小程序客服消息功能
Aug 12 PHP
php设计模式之正面模式实例分析【星际争霸游戏案例】
Mar 24 PHP
PHP实现适用于文件内容操作的分页类
Jun 15 #PHP
PHP实现适用于自定义的验证码类
Jun 15 #PHP
php实现常见图片格式的水印和缩略图制作(面向对象)
Jun 15 #PHP
使用JavaScript创建新样式表和新样式规则
Jun 14 #PHP
PHP list() 将数组中的值赋给变量的简单实例
Jun 13 #PHP
PHP处理二进制数据的实现方法
Jun 13 #PHP
PHP 在数组中搜索给定的简单实例 array_search 函数
Jun 13 #PHP
You might like
PHP 文件上传功能实现代码
2009/06/24 PHP
js类 from qq
2006/11/13 Javascript
JQuery循环滚动图片代码
2011/12/08 Javascript
JavaScript三元运算符的多种使用技巧
2015/04/16 Javascript
JS实现带缓冲效果打开、关闭、移动一个层的方法
2015/05/09 Javascript
让JavaScript中setTimeout支持链式操作的方法
2015/06/19 Javascript
Javascript获取随机数的实现方法
2016/06/22 Javascript
大型JavaScript应用程序架构设计模式
2016/06/29 Javascript
AngularJS  ng-table插件设置排序
2016/09/21 Javascript
jQuery File Upload文件上传插件使用详解
2016/12/06 Javascript
ES6学习教程之块级作用域详解
2017/10/09 Javascript
React Native AsyncStorage本地存储工具类
2017/10/24 Javascript
vue项目环境变量配置的实现方法
2018/10/12 Javascript
微信小程序结合Storage实现搜索历史效果
2019/05/18 Javascript
Node.js开发之套接字(socket)编程入门示例
2019/11/05 Javascript
JS实现页面数据懒加载
2020/02/13 Javascript
JS获取表格视图所选行号的ids过程解析
2020/02/21 Javascript
[03:21]【TI9纪实】Old Boys
2019/08/23 DOTA
python字符类型的一些方法小结
2016/05/16 Python
Python+微信接口实现运维报警
2016/08/27 Python
Python构建网页爬虫原理分析
2017/12/19 Python
基于Python Numpy的数组array和矩阵matrix详解
2018/04/04 Python
Python pyinotify模块实现对文档的实时监控功能方法
2018/10/13 Python
在python中,使用scatter绘制散点图的实例
2019/07/03 Python
django 模版关闭转义方式
2020/05/14 Python
如何使用python-opencv批量生成带噪点噪线的数字验证码
2020/12/21 Python
Javascript 高级手势使用介绍
2013/04/21 HTML / CSS
英国、欧洲和全球租车服务:Avis英国
2016/08/29 全球购物
贸易跟单员英文求职信
2014/04/19 职场文书
入党介绍人评语
2014/05/06 职场文书
小学生环保倡议书
2014/05/15 职场文书
慰问信格式
2015/02/14 职场文书
学习弘扬焦裕禄精神心得体会
2016/01/23 职场文书
redis通过6379端口无法连接服务器(redis-server.exe闪退)
2021/05/08 Redis
python单元测试之pytest的使用
2021/06/07 Python
python中的装饰器该如何使用
2021/06/18 Python