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 获取本机外网/公网IP的代码
May 09 PHP
php更改目录及子目录下所有的文件后缀的代码
Sep 24 PHP
PHP setcookie指定domain参数后,在IE下设置cookie失效的解决方法
Sep 09 PHP
PHP 文本文章分页代码 按标记或长度(不涉及数据库)
Jun 07 PHP
5种PHP创建数组的实例代码分享
Jan 17 PHP
PHP中addcslashes与stripcslashes函数用法分析
Jan 07 PHP
Yii中的cookie的发送和读取
Jul 27 PHP
php字符串操作针对负值的判断分析
Jul 28 PHP
PHP实现图片批量打包下载功能
Mar 01 PHP
网站被恶意镜像怎么办 php一段代码轻松搞定(全面版)
Oct 23 PHP
小程序微信支付功能配置方法示例详解【基于thinkPHP】
May 05 PHP
php实现大文件断点续传下载实例代码
Oct 01 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/09/30 PHP
php 字符串替换的方法
2012/01/10 PHP
php图形jpgraph操作实例分析
2017/02/22 PHP
Thinkphp 3.2框架使用Redis的方法详解
2019/10/24 PHP
Laravel 自动转换长整型雪花 ID 为字符串的实现
2020/10/27 PHP
在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
2011/06/02 Javascript
jQuery实现3D文字特效的方法
2015/03/10 Javascript
javascript实现点击单选按钮链接转向对应网址的方法
2015/08/12 Javascript
你一定会收藏的Nodejs代码片段
2016/02/04 NodeJs
JavaScript使用FileReader实现图片上传预览效果
2020/03/27 Javascript
AngularJS使用ui-route实现多层嵌套路由的示例
2018/01/10 Javascript
JS实现为动态创建的元素添加事件操作示例
2018/03/17 Javascript
Vue表单类的父子组件数据传递示例
2018/05/03 Javascript
微信小程序实现提交input信息到后台的方法示例
2019/01/19 Javascript
JS随机密码生成算法
2019/09/23 Javascript
Vue.js获取手机系统型号、版本、浏览器类型的示例代码
2020/05/10 Javascript
vue组件系列之TagsInput详解
2020/05/14 Javascript
[56:57]LGD vs VP 2019DOTA2国际邀请赛淘汰赛 胜者组赛BO3 第一场 8.20.mp4
2019/08/22 DOTA
[05:11]TI9战队采访——VIRTUSPRO
2019/08/22 DOTA
对Python进行数据分析_关于Package的安装问题
2017/05/22 Python
python实现支付宝当面付(扫码支付)功能
2018/05/30 Python
Pycharm安装Qt Design快捷工具的详细教程
2020/11/18 Python
HTML5 body设置全屏背景图片的示例代码
2020/12/08 HTML / CSS
StubHub澳大利亚:购买或出售您的门票
2019/08/01 全球购物
展会邀请函范文
2014/01/26 职场文书
升旗仪式主持词
2014/03/19 职场文书
《长相思》听课反思
2014/04/10 职场文书
企业指导教师评语
2014/04/28 职场文书
禁止酒驾标语
2014/06/25 职场文书
人大调研汇报材料
2014/08/14 职场文书
仲裁协议书
2014/09/26 职场文书
党员教师批评与自我批评发言稿
2014/10/15 职场文书
2014年度个人总结范文
2015/03/09 职场文书
旗帜观后感
2015/06/08 职场文书
2015军训通讯稿大全
2015/07/18 职场文书
创业方案:赚钱的烧烤店该怎样做?
2019/07/05 职场文书