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系统流量分析的程序
Oct 09 PHP
phpMyAdmin2.11.6安装配置方法
Aug 24 PHP
不用mod_rewrite直接用php实现伪静态化页面代码
Oct 04 PHP
基于php iconv函数的使用详解
Jun 09 PHP
基于php导出到Excel或CSV的详解(附utf8、gbk 编码转换)
Jun 25 PHP
解析PHP跳出循环的方法以及continue、break、exit的区别介绍
Jul 01 PHP
PHP 字符串长度判断效率更高的方法
Mar 02 PHP
PHP中echo和print的区别
Aug 28 PHP
php mysql_real_escape_string addslashes及mysql绑定参数防SQL注入攻击
Dec 23 PHP
基于Codeigniter框架实现的student信息系统站点动态发布功能详解
Mar 23 PHP
详解PHP的抽象类和抽象方法以及接口总结
Mar 15 PHP
Yii框架的redis命令使用方法简单示例
Oct 15 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
thinkPHP中验证码的简单使用方法
2015/12/26 PHP
PHP+Mysql无刷新问答评论系统(源码)
2016/12/20 PHP
PHP7新特性之抽象语法树(AST)带来的变化详解
2018/07/17 PHP
php+ajax实现文件切割上传功能示例
2020/03/03 PHP
PHP7 参数处理机制修改
2021/03/09 PHP
禁止空格提交表单的js代码
2013/11/17 Javascript
javascript中match函数的用法小结
2014/02/08 Javascript
Jquery获得控件值的三种方法总结
2014/02/13 Javascript
jQuery修改class属性和CSS样式整理
2015/01/30 Javascript
JavaScript使用replace函数替换字符串的方法
2015/04/06 Javascript
举例讲解AngularJS中的模块
2015/06/17 Javascript
JavaScript数组去重的3种方法和代码实例
2015/07/01 Javascript
jQuery+JSON实现AJAX二级联动实例分析
2015/12/18 Javascript
JS获取子窗口中返回的数据实现方法
2016/05/28 Javascript
Bootstrap警告框(Alert)插件使用方法
2017/03/21 Javascript
详解用node-images 打造简易图片服务器
2017/05/08 Javascript
Ajax高级笔记 JavaScript高级程序设计笔记
2017/06/22 Javascript
代码详解JS操作剪贴板
2018/02/11 Javascript
关于Vue在ie10下空白页的debug小结
2018/05/02 Javascript
Python tkinter的grid布局及Text动态显示方法
2018/10/11 Python
pandas DataFrame 交集并集补集的实现
2019/06/24 Python
Python TCP通信客户端服务端代码实例
2019/11/21 Python
PHP基于phpqrcode类库生成二维码过程解析
2020/05/28 Python
python中如何进行连乘计算
2020/05/28 Python
Python爬虫获取页面所有URL链接过程详解
2020/06/04 Python
电大物流学生的自我评价
2013/10/25 职场文书
企业厂长岗位职责
2013/12/17 职场文书
面试后感谢信
2014/02/01 职场文书
端午节活动策划方案
2014/03/09 职场文书
保密承诺书范文
2014/03/27 职场文书
学生会感恩节活动方案
2014/10/11 职场文书
2014年党员发展工作总结
2014/12/02 职场文书
银行资信证明
2015/06/17 职场文书
同学聚会致辞集锦
2015/07/28 职场文书
标准版个人借条怎么写?以及什么是借条?
2019/08/28 职场文书
解决Golang中goroutine执行速度的问题
2021/05/02 Golang