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 相关文章推荐
图形数字验证代码
Oct 09 PHP
php中session_unset与session_destroy的区别分析
Jun 16 PHP
解析isset与is_null的区别
Aug 09 PHP
PHP mysql与mysqli事务使用说明 分享
Aug 17 PHP
Yii不依赖Model的表单生成器用法实例
Dec 04 PHP
ThinkPHP路由详解
Jul 27 PHP
PHP防盗链的基本思想 防盗链的设置方法
Sep 25 PHP
PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)
Jan 07 PHP
PHP实现腾讯与百度坐标转换
Aug 05 PHP
ThinkPHP开发--使用七牛云储存
Sep 14 PHP
Laravel框架表单验证操作实例分析
Sep 30 PHP
PHP使用openssl扩展实现加解密方法示例
Feb 20 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 $_FILES函数详解
2011/03/09 PHP
php遍历树的常用方法汇总
2015/06/18 PHP
Javascript 面向对象 重载
2010/05/13 Javascript
javascript学习笔记(四) Number 数字类型
2012/06/19 Javascript
js arguments对象应用介绍
2012/11/28 Javascript
JS实现判断碰撞的方法
2015/02/11 Javascript
jQuery unbind()方法实例详解
2016/01/19 Javascript
浅谈javascript的url参数parse和build函数
2017/03/04 Javascript
百度地图JavascriptApi Marker平滑移动及车头指向行径方向
2017/03/13 Javascript
解决vue-cli3 使用子目录部署问题
2018/07/19 Javascript
JS基于ES6新特性async await进行异步处理操作示例
2019/02/02 Javascript
vue从一个页面跳转到另一个页面并携带参数的解决方法
2019/08/12 Javascript
微信小程序封装分享与分销功能过程解析
2019/08/13 Javascript
Python深入学习之特殊方法与多范式
2014/08/31 Python
详解Python网络爬虫功能的基本写法
2016/01/28 Python
Python 常用 PEP8 编码规范详解
2017/01/22 Python
Python交互环境下实现输入代码
2018/06/22 Python
python 数字类型和字符串类型的相互转换实例
2018/07/17 Python
python版大富翁源代码分享
2018/11/19 Python
python实现得到当前登录用户信息的方法
2019/06/21 Python
Python数据类型之列表和元组的方法实例详解
2019/07/08 Python
python Dijkstra算法实现最短路径问题的方法
2019/09/19 Python
使用Python获取当前工作目录和执行命令的位置
2020/03/09 Python
python 实现关联规则算法Apriori的示例
2020/09/30 Python
python中scrapy处理项目数据的实例分析
2020/11/22 Python
美国在线奢侈品寄售商店:Luxury Garage Sale
2018/08/19 全球购物
动物学专业毕业生求职信
2013/10/11 职场文书
平民服装店创业计划书
2014/01/17 职场文书
廉洁使者实施方案
2014/03/29 职场文书
《第一朵杏花》教学反思
2014/04/16 职场文书
中学生演讲稿
2014/04/26 职场文书
小区环境卫生倡议书
2015/04/29 职场文书
复活读书笔记
2015/06/29 职场文书
redis三种高可用方式部署的实现
2021/05/11 Redis
SpringBoot整合JWT的入门指南
2021/06/29 Java/Android
Redis 持久化 RDB 与 AOF的执行过程
2021/11/07 Redis