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 相关文章推荐
PHP实现文件安全下载
Oct 09 PHP
PHP新手上路(四)
Oct 09 PHP
PHP开发框架总结收藏
Apr 24 PHP
php file_exists 检查文件或目录是否存在的函数
May 10 PHP
php中OR与|| AND与&amp;&amp;的区别总结
Oct 26 PHP
php获取网卡的MAC地址支持WIN/LINUX系统
Apr 30 PHP
ThinkPHP表单自动提交验证实例教程
Jul 18 PHP
php和editplus正则表达式去除空白行
Apr 17 PHP
Symfony实现行为和模板中取得request参数的方法
Mar 17 PHP
PHP实现对数组分页处理实例详解
Feb 07 PHP
asp函数split()对应php函数explode()
Feb 27 PHP
php异常处理捕获错误整理
Sep 23 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和ACCESS写聊天室(五)
2006/10/09 PHP
关于PHP模板Smarty的初级使用方法以及心得分享
2013/06/21 PHP
php批量更改数据库表前缀实现方法
2013/10/26 PHP
PHP+Mysql树型结构(无限分类)数据库设计的2种方式实例
2014/07/15 PHP
php实现生成验证码实例分享
2016/04/10 PHP
PHP实现链式操作的原理详解
2016/09/16 PHP
PHP7标量类型declare用法实例分析
2016/09/26 PHP
php项目中类的自动加载实例讲解
2019/09/12 PHP
js 实现无干扰阴影效果 简单好用(附文件下载)
2009/12/27 Javascript
js获取本机的外网/广域网ip地址完整源码
2013/08/12 Javascript
javascript 表格内容排序 简单操作示例代码
2014/01/03 Javascript
jQuery选择器源码解读(五):tokenize的解析过程
2015/03/31 Javascript
js实现滑动触屏事件监听的方法
2015/05/05 Javascript
Js数组排序函数sort()介绍
2015/06/08 Javascript
浅谈vue项目重构技术要点和总结
2018/01/23 Javascript
详解vue项目中调用百度地图API使用方法
2019/04/25 Javascript
Servlet返回的数据js解析2种方法
2019/12/12 Javascript
浅谈webpack和webpack-cli模块源码分析
2020/01/19 Javascript
python基础教程之实现石头剪刀布游戏示例
2014/02/11 Python
Python进程通信之匿名管道实例讲解
2015/04/11 Python
python实现的简单窗口倒计时界面实例
2015/05/05 Python
python实现的正则表达式功能入门教程【经典】
2017/06/05 Python
Python图像处理之gif动态图的解析与合成操作详解
2018/12/30 Python
PyQt5 QTableView设置某一列不可编辑的方法
2019/06/25 Python
Django框架之DRF 基于mixins来封装的视图详解
2019/07/23 Python
python操作gitlab API过程解析
2019/12/27 Python
Python3.6安装卸载、执行命令、执行py文件的方法详解
2020/02/20 Python
领先的钻石和订婚戒指零售商:Diamonds-USA
2016/12/11 全球购物
美国高级音响品牌:Master&Dynamic
2018/07/05 全球购物
欧洲最古老的鞋厂:Peter Kaiser
2019/11/05 全球购物
机械电子工程毕业生自荐信
2013/11/23 职场文书
财务会计专业个人求职信范本
2014/01/08 职场文书
2015年国庆节活动总结
2015/03/23 职场文书
2015年乡镇发展党员工作总结
2015/03/31 职场文书
2016年党支部公开承诺书
2016/03/25 职场文书
基于Go Int转string几种方式性能测试
2021/04/28 Golang