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将向Java靠拢
Oct 09 PHP
最小化数据传输――在客户端存储数据
Oct 09 PHP
php+dojo 的数据库保存拖动布局的一个方法dojo 这里下载
Mar 07 PHP
php数组函数序列之array_intersect() 返回两个或多个数组的交集数组
Nov 10 PHP
win7下memCache的安装过程(具体操作步骤)
Jun 28 PHP
PHP empty函数报错解决办法
Mar 06 PHP
php调用nginx的mod_zip模块打包ZIP文件
Jun 11 PHP
php网站被挂木马后的修复方法总结
Nov 06 PHP
学习php设计模式 php实现工厂模式(factory)
Dec 07 PHP
PHP数组操作简单案例分析
Oct 15 PHP
PHP获取页面执行时间的方法(推荐)
Dec 10 PHP
php根据用户名和手机号查询是否存在手机号码
Feb 16 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
javascript,php获取函数参数对象的代码
2011/02/03 PHP
用来解析.htgroup文件的PHP类
2012/09/05 PHP
php使用GD库创建图片缩略图的方法
2015/06/10 PHP
详解HTTP Cookie状态管理机制
2016/01/14 PHP
Json_decode 解析json字符串为NULL的解决方法(必看)
2017/02/17 PHP
JavaScript—window对象使用示例
2013/12/09 Javascript
深入理解JavaScript系列(46):代码复用模式(推荐篇)详解
2015/03/04 Javascript
jquery对象和DOM对象的任意相互转换
2016/02/21 Javascript
浅谈JS原型对象和原型链
2016/03/02 Javascript
Ionic实现页面下拉刷新(ion-refresher)功能代码
2016/06/03 Javascript
js实现楼层效果的简单实例
2016/07/15 Javascript
AngularJS基础 ng-model 指令详解及示例代码
2016/08/02 Javascript
Javascript 闭包详解及实例代码
2016/11/30 Javascript
原生JS中slice()方法和splice()区别
2017/03/06 Javascript
JS实现小球的弹性碰撞效果
2017/11/11 Javascript
Angular4.0中引入laydate.js日期插件的方法教程
2017/12/25 Javascript
JS基于对象的链表实现与使用方法示例
2019/01/31 Javascript
VueQuillEditor富文本上传图片(非base64)
2020/06/03 Javascript
微信小程序向Java后台传输参数的方法实现
2020/12/10 Javascript
[01:02:38]DOTA2-DPC中国联赛定级赛 LBZS vs Phoenix BO3第二场 1月10日
2021/03/11 DOTA
Python数据可视化:幂律分布实例详解
2019/12/07 Python
食品厂厂长岗位职责
2014/01/30 职场文书
军人违纪检讨书
2014/02/04 职场文书
企业形象策划方案
2014/05/29 职场文书
干部作风整顿个人剖析材料
2014/10/06 职场文书
群众路线剖析材料怎么写
2014/10/09 职场文书
新教师个人工作总结
2015/02/06 职场文书
社区植树节活动总结
2015/02/06 职场文书
大学生个人学年总结
2015/02/15 职场文书
研究生给导师的自荐信
2015/03/06 职场文书
中标通知书
2015/04/17 职场文书
抢劫罪辩护词
2015/05/21 职场文书
小组组名及励志口号
2015/12/24 职场文书
MySQL8.0.24版本Release Note的一些改进点
2021/04/22 MySQL
Go语言 go程释放操作(退出/销毁)
2021/04/30 Golang
Windows 11要来了?微软文档揭示Win11太阳谷 / Win10有两个不同版本
2021/11/21 数码科技