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控制网页过期时间的代码
Sep 28 PHP
PHP parse_url 一个好用的函数
Oct 03 PHP
php 将excel导入mysql
Nov 09 PHP
PHP中的strtr函数使用介绍(str_replace)
Oct 20 PHP
关于PHP实现异步操作的研究
Feb 03 PHP
php过滤敏感词的示例
Mar 31 PHP
php写的AES加密解密类分享
Jun 20 PHP
PHP Header失效的原因分析及解决方法
Nov 16 PHP
php 静态属性和静态方法区别详解
Apr 09 PHP
PHP后端银联支付及退款实例代码
Jun 23 PHP
php数据序列化测试实例详解
Aug 12 PHP
php微信公众号开发之关键词回复
Oct 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 自定义错误处理函数的使用详解
2013/05/10 PHP
XAMPP安装与使用方法详细解析
2013/11/27 PHP
PHP用mb_string函数库处理与windows相关中文字符及Win环境下开启PHP Mb_String方法
2015/11/11 PHP
PHP4和PHP5版本下解析XML文档的操作方法实例分析
2017/05/20 PHP
PHP5.6新增加的可变函数参数用法分析
2017/08/25 PHP
js判断鼠标同时离开两个div的思路及代码
2013/05/31 Javascript
JS定义回车事件(实现代码)
2013/07/08 Javascript
jQuery表格插件datatables用法总结
2014/09/05 Javascript
jQuery中:gt选择器用法实例
2014/12/29 Javascript
js+HTML5基于过滤器从摄像头中捕获视频的方法
2015/06/16 Javascript
js实现文字滚动效果
2016/03/03 Javascript
Nodejs下用submit提交表单提示cannot post错误的解决方法
2016/11/21 NodeJs
JS中的数组转变成JSON格式字符串的方法
2017/05/09 Javascript
JS 判断某变量是否为某数组中的一个值的3种方法(总结)
2017/07/10 Javascript
原生js调用json方法总结
2018/02/22 Javascript
状态机的概念和在Python下使用状态机的教程
2015/04/11 Python
Python中取整的几种方法小结
2017/01/06 Python
Python简单实现自动删除目录下空文件夹的方法
2017/08/29 Python
python安装模块如何通过setup.py安装(超简单)
2018/05/05 Python
Python2和Python3中urllib库中urlencode的使用注意事项
2018/11/26 Python
python 并发下载器实现方法示例
2019/11/22 Python
Python使用Turtle库绘制一棵西兰花
2019/11/23 Python
详解tf.device()指定tensorflow运行的GPU或CPU设备实现
2021/02/20 Python
中国宠物用品商城:E宠商城
2016/08/27 全球购物
香港万宁官方海外旗舰店:香港健与美连锁店
2018/09/27 全球购物
马来西亚太阳镜、眼镜和隐形眼镜网上商店:Focus Point
2018/12/13 全球购物
音乐教学案例
2014/01/30 职场文书
2014年商场超市庆元旦活动方案
2014/02/14 职场文书
社区党员公开承诺书
2014/08/30 职场文书
公务员保密工作承诺书
2015/05/04 职场文书
狂人日记读书笔记
2015/06/30 职场文书
初中同学会致辞
2015/08/01 职场文书
护士旷工检讨书
2015/08/15 职场文书
导游词之嵊泗列岛
2019/10/30 职场文书
python基础详解之if循环语句
2021/04/24 Python
「海贼王」112.9万粉丝纪念图标公布
2022/03/21 日漫