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 相关文章推荐
删除无限分类并同时删除它下面的所有子分类的方法
Aug 08 PHP
php将fileterms函数返回的结果变成可读的形式
Apr 21 PHP
php中利用post传递字符串重定向的实现代码
Apr 21 PHP
php实现建立多层级目录的方法
Jul 19 PHP
如何通过Linux命令行使用和运行PHP脚本
Jul 29 PHP
PHP new static 和 new self详解
Feb 19 PHP
php 猴子摘桃的算法
Jun 20 PHP
详解PHP字符串替换str_replace()函数四种用法
Oct 13 PHP
Laravel 批量更新多条数据的示例
Nov 27 PHP
php fread函数使用方法总结
May 28 PHP
PHP设计模式入门之状态模式原理与实现方法分析
Apr 26 PHP
基于PHP+mysql实现新闻发布系统的开发
Aug 06 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
真正根据utf8编码的规律来进行截取字符串的函数(utf8版sub_str )
2012/10/24 PHP
PHP中判断文件存在使用is_file还是file_exists?
2015/04/03 PHP
Laravel中七个非常有用但很少人知道的Carbon方法
2017/09/21 PHP
PHP封装的XML简单操作类完整实例
2017/11/13 PHP
php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例
2019/05/09 PHP
javascript实现日历控件(年月日关闭按钮)
2012/12/12 Javascript
jQuery焦点图切换特效插件封装实例
2013/08/18 Javascript
jquery 鼠标滑动显示详情应用示例
2014/01/24 Javascript
使用正则表达式的格式化与高亮显示json字符串
2014/12/03 Javascript
JavaScript不使用prototype和new实现继承机制
2014/12/29 Javascript
Node.js中process模块常用的属性和方法
2016/12/13 Javascript
页面间固定参数,通过cookie传值的实现方法
2017/05/31 Javascript
javascript连接mysql与php通过odbc连接任意数据库的实例
2017/12/27 Javascript
angularJS实现动态添加,删除div方法
2018/02/27 Javascript
javascript变量提升和闭包理解
2018/03/12 Javascript
JS使用数组实现的队列功能示例
2019/03/04 Javascript
vue登录注册实例详解
2019/09/14 Javascript
JavaScript实现拖拽和缩放效果
2020/08/24 Javascript
[02:20]DOTA2亚洲邀请赛 EHOME战队出场宣传片
2015/02/07 DOTA
Python实现国外赌场热门游戏Craps(双骰子)
2015/03/31 Python
利用Fn.py库在Python中进行函数式编程
2015/04/22 Python
Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
2015/05/22 Python
Python的爬虫包Beautiful Soup中用正则表达式来搜索
2016/01/20 Python
Python正则表达式教程之三:贪婪/非贪婪特性
2017/03/02 Python
Python数据结构之顺序表的实现代码示例
2017/11/15 Python
Python cookbook(数据结构与算法)对切片命名清除索引的方法
2018/03/13 Python
django 外键创建注意事项说明
2020/05/20 Python
Python如何使用27行代码绘制星星图
2020/07/20 Python
H5最强接口之canvas实现动态图形功能
2019/05/31 HTML / CSS
YSL圣罗兰美妆美国官网:Yves Saint Lauret US
2016/11/21 全球购物
新西兰最大的在线设计师眼镜店:SmartBuyGlasses新西兰
2017/10/20 全球购物
FC-Moto美国:欧洲最大的摩托车服装和头盔商店之一
2019/08/24 全球购物
2016年心理学教育培训学习心得体会
2016/01/12 职场文书
英语版自我评价,35句话轻松搞定
2019/10/08 职场文书
导游词之沈阳清昭陵
2019/12/28 职场文书
SpringRetry重试框架的具体使用
2021/07/25 Java/Android