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 相关文章推荐
不用iconv库的gb2312与utf-8的互换函数
Oct 09 PHP
用PHP实现登陆验证码(类似条行码状)
Oct 09 PHP
在PHP中操作Excel实例代码
Apr 29 PHP
PHP延迟静态绑定示例分享
Jun 22 PHP
php socket实现的聊天室代码分享
Aug 16 PHP
php通过递归方式复制目录和子目录的方法
Mar 13 PHP
php通过baihui网API实现读取word文档并展示
Jun 22 PHP
PHP实现C#山寨ArrayList的方法
Jul 16 PHP
php使用正则验证中文
Apr 06 PHP
[原创]php实现数组按拼音顺序排序的方法
May 03 PHP
PHP区块查询实现方法分析
May 12 PHP
PHP实现链表的定义与反转功能示例
Jun 09 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中利用XML技术构造远程服务(下)
2006/10/09 PHP
Linux下将excel数据导入到mssql数据库中的方法
2010/02/08 PHP
基于jquery实现的服务器验证控件的启用和禁用代码
2010/04/27 Javascript
小米公司JavaScript面试题
2014/12/29 Javascript
JavaScript中扩展Array contains方法实例
2020/08/23 Javascript
AngularJS中如何使用$parse或$eval在运行时对Scope变量赋值
2016/01/25 Javascript
JavaScript弹窗基础篇
2016/04/27 Javascript
微信小程序 高德地图SDK详解及简单实例(源码下载)
2017/01/11 Javascript
vue scroller返回页面记住滚动位置的实例代码
2018/01/29 Javascript
JS实现数组的增删改查操作示例
2018/08/29 Javascript
深入理解react-router 路由的实现原理
2018/09/26 Javascript
vue组件实践之可搜索下拉框功能
2018/11/25 Javascript
Python如何判断数独是否合法
2016/09/08 Python
通过Python爬虫代理IP快速增加博客阅读量
2016/12/14 Python
Python实现PS图像调整之对比度调整功能示例
2018/01/26 Python
Python3实现的简单验证码识别功能示例
2018/05/02 Python
python3 写一个WAV音频文件播放器的代码
2019/09/27 Python
python入门之基础语法学习笔记
2020/02/08 Python
如何对python的字典进行排序
2020/06/19 Python
Python 实现微信自动回复的方法
2020/09/11 Python
Scrapy中如何向Spider传入参数的方法实现
2020/09/28 Python
python实现ping命令小程序
2020/12/28 Python
python 多线程爬取壁纸网站的示例
2021/02/20 Python
Html5 FileReader实现即时上传图片功能实例代码
2014/09/01 HTML / CSS
简述Html5 IphoneX 适配方法
2018/02/08 HTML / CSS
Crocs美国官方网站:卡骆驰洞洞鞋
2017/08/04 全球购物
香港交友网站:be2香港
2018/07/22 全球购物
英国休闲奢华的缩影:Crew Clothing
2019/05/05 全球购物
PHP面试题集
2016/12/18 面试题
别名指示符是什么
2012/10/08 面试题
体育教育专业自荐信范文
2013/12/20 职场文书
商场中秋节活动方案
2014/02/07 职场文书
css3 filter属性的使用简介
2021/03/31 HTML / CSS
Python中Cookies导出某站用户数据的方法
2021/05/17 Python
JS 基本概念详细介绍
2021/10/16 Javascript
Mongodb 迁移数据块的流程介绍分析
2022/04/18 MongoDB