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和XSL stylesheets转换XML文档
Oct 09 PHP
一些PHP Coding Tips(php小技巧)[2011/04/02最后更新]
May 02 PHP
php array_filter除去数组中的空字符元素
Jun 21 PHP
php无限分类且支持输出树状图的详细介绍
Jun 19 PHP
使用PHP获取当前url路径的函数以及服务器变量
Jun 29 PHP
php实现的通用图片处理类
Mar 24 PHP
php实现改变图片直接打开为下载的方法
Apr 14 PHP
PHP中的魔术方法总结和使用实例
May 11 PHP
再推荐十款免费的php开发工具
Nov 09 PHP
php similar_text()函数的定义和用法
May 12 PHP
Yii框架中jquery表单验证插件用法示例
Oct 18 PHP
Yii实现文章列表置顶功能示例
Oct 18 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&amp;MYSQL服务器配置说明
2006/10/09 PHP
php jsonp单引号转义
2014/11/23 PHP
PHP微信开发之二维码生成类
2015/06/26 PHP
php自定义函数实现统计中文字符串长度的方法小结
2017/04/15 PHP
Thinkphp5.0 框架的请求方式与响应方式分析
2019/10/14 PHP
javascript 利用Image对象实现的埋点(某处的点击数)统计
2012/12/28 Javascript
jquery弹出框的用法示例(2)
2013/08/26 Javascript
Javascript表格翻页效果的具体实现
2013/10/05 Javascript
SuperSlide标签切换、焦点图多种组合插件
2015/03/14 Javascript
JavaScript基本语法学习教程
2016/01/14 Javascript
常用的js方法合集
2017/03/10 Javascript
Bootstrap进度条与AJAX后端数据传递结合使用实例详解
2017/04/23 Javascript
VueJs里利用CryptoJs实现加密及解密的方法示例
2019/04/29 Javascript
p5.js临摹动态图形实现方法详解
2019/10/23 Javascript
Python选择排序、冒泡排序、合并排序代码实例
2015/04/10 Python
python 实现对数据集的归一化的方法(0-1之间)
2018/07/17 Python
浅谈django三种缓存模式的使用及注意点
2018/09/30 Python
用python实现刷点击率的示例代码
2019/02/21 Python
Python Excel处理库openpyxl使用详解
2019/05/09 Python
python3射线法判断点是否在多边形内
2019/06/28 Python
30行Python代码实现高分辨率图像导航的方法
2020/05/22 Python
将不规则的Python多维数组拉平到一维的方法实现
2021/01/11 Python
达拉斯牛仔官方商店:Dallas Cowboys Pro Shop
2018/02/10 全球购物
香港通票:Hong Kong Pass
2019/02/26 全球购物
sleep()方法和wait()方法的区别是什么
2012/11/17 面试题
群众路线党课主持词
2014/04/01 职场文书
终止劳动合同协议书
2014/04/14 职场文书
活动倡议书范文
2014/05/13 职场文书
室内设计专业自荐信
2014/05/31 职场文书
教师反腐倡廉演讲稿
2014/09/03 职场文书
房产授权委托书范本
2014/09/22 职场文书
老兵退伍标语
2014/10/07 职场文书
2014年人民调解工作总结
2014/12/08 职场文书
紧急通知
2015/04/17 职场文书
污水处理保证书
2015/05/09 职场文书
导游词之太原天龙山
2020/01/02 职场文书