php实现常见图片格式的水印和缩略图制作(面向对象)

 更新时间:2016年6月24日 10:11  点击:1408

本文实例为大家分享了php水印和缩略图制作代码,使用面向对象的方法来实现常见图片格式jpg,png,gif的水印和缩略图的制作,供大家参考,具体内容如下

<?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';
  
 //thumb的制作
 //默认缩略图功能开启
 private $thumb_on = 1;
 //生成缩略图的方式
 public $thumb_type = 1;
 //生成缩略图的宽度
 public $thumb_width;
 //生成缩略图的高度
 public $thumb_height;
 //生成缩略图的后缀名
 public $thumb_fix = '_dq';
 
 //缩略图函数处理
 public function thumb( $img,$outfile='',$t_type='',$t_w='',$t_h='' ){
  //验证图片是否符合要求
  if(!$this->check($img) || !$this->thumb_on) return FALSE;
   
  //定义缩略图的初始值
  $t_type = $t_type ? $t_type : $this->thumb_type;
  $t_w = $t_w ? $t_w : $this->thumb_width;
  $t_h = $t_h ? $t_h : $this->thumb_height;
   
  //获取到原图的信息
  $img_info = getimagesize($img);
  $img_w = $img_info[0];
  $img_h = $img_info[1];
  //取得图像类型的文件后缀
  $img_type = image_type_to_extension($img_info[2]);
  //获取到相关尺寸
  $thumb_size = $this->thumb_size($img_w,$img_h,$t_w,$t_h,$t_type);
  //确定原始图像类型
  //利用自定义函数来实现图片类型的确定
  $func = "imagecreatefrom".substr($img_type, 1);
  $res_img = $func($img);
   
  //缩略图资源   编辑图片资源moon
  if( $img_type == '.gif' || $img_type == '.png' ){
   $res_thumb = imagecreate($thumb_size[0], $thumb_size[1]);
   $color = imagecolorallocate($res_thumb, 255, 0, 0);
  }else{
   $res_thumb = imagecreatetruecolor($thumb_size[0], $thumb_size[1]);
  }
   
  //制作缩略图
  if(function_exists( "imagecopyresampled" ) ){
   imagecopyresampled($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
  }else{
   imagecopyresized($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
  }
  //处理透明色
  if( $img_type =='.gif' || $img_type == '.png' ){
   imagecolortransparent($res_thumb,$color);
  }
   
  //配置输出文件名
  $outfile = $outfile ? $outfile : $outfile.substr($img,0,strripos($img,'.')).$this->thumb_fix.$img_type;
   
  //文件的保存输出
  $func = "image".substr($img_type, 1);
  $func($res_thumb,$outfile);
  if(isset($res_thumb)) imagedestroy ($res_thumb);
  if(isset($res_img)) imagedestroy ($res_img);
  return $outfile;
 } 
 
 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:
   imagepng($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);
  } 
   
  //获取缩略图的相关比例
  //获取到图片的处理类型
  private function thumb_size( $img_w,$img_h,$t_w,$t_h,$t_type){
   //定义缩略图尺寸
   $w = $t_w;
   $h = $t_h;
    
   //定义图片的原始尺寸
   $cut_w = $img_w;
   $cut_h = $img_h;
    
   //当要目标图像小于缩略图的尺寸时;
   if( $img_w <= $t_w && $img_h < $t_h ){
    $w = $img_w;
    $h = $img_h;
   }else{
    if( !empty($t_type) && $t_type>0 ){
     switch ( $t_type ){
      //当宽度固定时
      case 1:
       $h = $t_w/$img_w*$img_h;
       break;
      //高度固定时
      case 2:
       $w = $t_h/$img_h*$img_w;
       break;
      //宽度固定,高度裁切
      case 3:
       $cut_h = $img_w/$t_w*$t_h;
       break;
      //高度固定,宽度裁切
      case 4:
       $cut_w = $img_h/$t_h*$t_w;
       break;
      //等比例缩放
      default :
       if( ($img_w/$t_w) > ($img_h/$t_h) ){
        $h = $t_w/$img_w*$t_h;
       }elseif( ($img_w/$t_w) < ($img_h/$t_h) ){
        $w = $t_h/$img_h*$t_w;
       }else{
        $w = $t_w;
        $h = $t_h;
       }
     }
    }
     
     
   }
   $arr[0] = $t_w;
   $arr[1] = $t_h;
   $arr[2] = $cut_w;
   $arr[3] = $cut_h;
   return $arr;
 }
}

以上就是本文的全部内容,希望对大家学习PHP程序设计有所帮助。

[!--infotagslink--]

相关文章

  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • index.php怎么打开?如何打开index.php?

    index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
  • PHP中func_get_args(),func_get_arg(),func_num_args()的区别

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04
  • PHP编程 SSO详细介绍及简单实例

    这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
  • PHP实现创建以太坊钱包转账等功能

    这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
  • php微信公众账号开发之五个坑(二)

    这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
  • ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单

    首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31
  • PHP如何通过date() 函数格式化显示时间

    这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
  • PHP+jQuery+Ajax实现多图片上传效果

    今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15
  • golang与php实现计算两个经纬度之间距离的方法

    这篇文章主要介绍了golang与php实现计算两个经纬度之间距离的方法,结合实例形式对比分析了Go语言与php进行经纬度计算的相关数学运算技巧,需要的朋友可以参考下...2016-07-29
  • PHP正则表达式过滤html标签属性(DEMO)

    这篇文章主要介绍了PHP正则表达式过滤html标签属性的相关内容,实用性非常,感兴趣的朋友参考下吧...2016-05-06
  • 谈谈PHP中相对路径的问题与绝对路径的使用

    经常看到有人踩在了PHP路径的坑上面了,感觉有必要来说说PHP中相对路径的一些坑,以及PHP中绝对路径的使用,下面一起来看看。 ...2016-08-24
  • php构造方法中析构方法在继承中的表现

    这篇文章主要为大家详细介绍了php构造方法中析构方法在继承中的表现,感兴趣的小伙伴们可以参考一下...2016-04-15
  • thinkPHP中多维数组的遍历方法

    这篇文章主要介绍了thinkPHP中多维数组的遍历方法,以简单实例形式分析了thinkPHP中foreach语句的使用技巧,需要的朋友可以参考下...2016-01-12
  • PHP如何使用cURL实现Get和Post请求

    这篇文章主要介绍了PHP如何使用cURL实现Get和Post请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-11
  • php有序列表或数组中删除指定的值的实现代码

    这篇文章主要介绍了php有序列表或数组中删除指定的值的实现代码,删除给定的值之后,得到一个新的有序列表,长度-1,下面是具体的实现方法...2021-08-22
  • php图片添加文字水印实现代码

    这篇文章主要为大家详细介绍了php图片添加文字水印实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-03-17
  • PHP简单实现生成txt文件到指定目录的方法

    这篇文章主要介绍了PHP简单实现生成txt文件到指定目录的方法,简单对比分析了PHP中fwrite及file_put_contents等函数的使用方法,需要的朋友可以参考下...2016-04-28
  • php判断邮箱地址是否存在的方法

    这篇文章主要介绍了php判断邮箱地址是否存在的方法,php判断邮箱地址是否存在的方法有两种,感兴趣的朋友可以参考一下...2016-02-18