摘自织梦CMS中的图片处理类


Posted in PHP onAugust 08, 2015

本文实例讲述了摘自织梦CMS中的图片处理类。分享给大家供大家参考。具体如下:

<?php if(!defined('DEDEINC')) exit('dedecms');
/**
 * 图像处理类
 *
 * @version  $Id: image.class.php 1 18:10 2010年7月5日Z tianya $
 * @package  DedeCMS.Libraries
 * @copyright  Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license  http://help.dedecms.com/usersguide/license.html
 * @link   http://www.dedecms.com
 */
class image
{
 var $attachinfo;
 var $targetfile; //图片路径
 var $imagecreatefromfunc;
 var $imagefunc;
 var $attach;
 var $animatedgif;
 var $watermarkquality;
 var $watermarktext;
 var $thumbstatus;
 var $watermarkstatus;
 // 析构函数,兼容PHP4
 function image($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array())
 {
  $this->__construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach);
 }
 // 析构函数
 function __construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array())
 {
  $this->thumbstatus = $cfg_thumb;
  $this->watermarktext = $cfg_watermarktext;
  $this->watermarkstatus = $photo_waterpos;
  $this->watermarkquality = $photo_marktrans;
  $this->watermarkminwidth = $photo_wwidth;
  $this->watermarkminheight = $photo_wheight;
  $this->watermarktype = $cfg_watermarktype;
  $this->watermarktrans = $photo_diaphaneity;
  $this->animatedgif = 0;
  $this->targetfile = $targetfile;
  $this->attachinfo = @getimagesize($targetfile);
  $this->attach = $attach;
  switch($this->attachinfo['mime'])
  {
   case 'image/jpeg':
    $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
    $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
    break;
   case 'image/gif':
    $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
    $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
    break;
   case 'image/png':
    $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
    $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
    break;
  }//为空则匹配类型的函数不存在
  $this->attach['size'] = empty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size'];
  if($this->attachinfo['mime'] == 'image/gif')
  {
   $fp = fopen($targetfile, 'rb');
   $targetfilecontent = fread($fp, $this->attach['size']);
   fclose($fp);
   $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === false ? 0 : 1;
  }
 }
 /**
  * 生成缩略图
  *
  * @access public
  * @param  int $thumbwidth 图片宽度
  * @param  int $thumbheight 图片高度
  * @param  int $preview 是否预览
  * @return void
  */
 function thumb($thumbwidth, $thumbheight, $preview = 0)
 {
  $this->thumb_gd($thumbwidth, $thumbheight, $preview);
 
  if($this->thumbstatus == 2 && $this->watermarkstatus)
  {
   $this->image($this->targetfile, $this->attach);
   $this->attach['size'] = filesize($this->targetfile);
  }
 }
 /**
  * 图片水印
  *
  * @access public
  * @param  int $preview 是否预览
  * @return void
  */
 function watermark($preview = 0)
 {
  if($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight)
  {
   return ;
  }
  $this->watermark_gd($preview);
 }
 /**
  * 使用gd生成缩略图
  *
  * @access public
  * @param  int $thumbwidth 图片宽度
  * @param  int $thumbheight 图片高度
  * @param  int $preview 是否预览
  * @return void
  */
 function thumb_gd($thumbwidth, $thumbheight, $preview = 0)
 {
  if($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg'))
  {
   $imagecreatefromfunc = $this->imagecreatefromfunc;
   $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;
   list($imagewidth, $imageheight) = $this->attachinfo;
   if(!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight))
   {
    $attach_photo = $imagecreatefromfunc($this->targetfile);
    $x_ratio = $thumbwidth / $imagewidth;
    $y_ratio = $thumbheight / $imageheight;
    if(($x_ratio * $imageheight) < $thumbheight)
    {
     $thumb['height'] = ceil($x_ratio * $imageheight);
     $thumb['width'] = $thumbwidth;
    }
    else
    {
     $thumb['width'] = ceil($y_ratio * $imagewidth);
     $thumb['height'] = $thumbheight;
    }
    $targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg';
    $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
    imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth, $imageheight);
    if($this->attachinfo['mime'] == 'image/jpeg')
    {
     $imagefunc($thumb_photo, $targetfile, 100);
    }
    else
    {
     $imagefunc($thumb_photo, $targetfile);
    }
    $this->attach['thumb'] = $this->thumbstatus == 1 ? 1 : 0;
   }
  }
 }
 /**
  * 使用gd进行水印
  *
  * @access public
  * @param  int $preview 是否预览
  * @return void
  */
 function watermark_gd($preview = 0)
 {
  if($this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge'))
  {
   $imagecreatefunc = $this->imagecreatefromfunc;
   $imagefunc = $this->imagefunc;
   list($imagewidth, $imageheight) = $this->attachinfo;
   if($this->watermarktype < 2)
   {
    $watermark_file = $this->watermarktype == 1 ? DEDEDATA.'/mark/mark.png' : DEDEDATA.'/mark/mark.gif';
    $watermarkinfo = @getimagesize($watermark_file);
    $watermark_logo = $this->watermarktype == 1 ? @imagecreatefrompng($watermark_file) : @imagecreatefromgif($watermark_file);
    if(!$watermark_logo)
    {
     return ;
    }
    list($logowidth, $logoheight) = $watermarkinfo;
   }
   else
   {
    $box = @imagettfbbox($this->watermarktext['size'], $this->watermarktext['angle'], $this->watermarktext['fontpath'],$this->watermarktext['text']);
    $logowidth = max($box[2], $box[4]) - min($box[0], $box[6]);
    $logoheight = max($box[1], $box[3]) - min($box[5], $box[7]);
    $ax = min($box[0], $box[6]) * -1;
    $ay = min($box[5], $box[7]) * -1;
   }
   $wmwidth = $imagewidth - $logowidth;
   $wmheight = $imageheight - $logoheight;
   if(($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif)
   {
    switch($this->watermarkstatus)
    {
     case 1:
 
      $x = +5;
      $y = +5;
      break;
     case 2:
      $x = ($imagewidth - $logowidth) / 2;
      $y = +5;
      break;
     case 3:
      $x = $imagewidth - $logowidth - 5;
      $y = +5;
      break;
     case 4:
      $x = +5;
      $y = ($imageheight - $logoheight) / 2;
      break;
     case 5:
      $x = ($imagewidth - $logowidth) / 2;
      $y = ($imageheight - $logoheight) / 2;
      break;
     case 6:
      $x = $imagewidth - $logowidth - 5;
      $y = ($imageheight - $logoheight) / 2;
      break;
     case 7:
      $x = +5;
      $y = $imageheight - $logoheight - 5;
      break;
     case 8:
      $x = ($imagewidth - $logowidth) / 2;
      $y = $imageheight - $logoheight - 5;
      break;
     case 9:
      $x = $imagewidth - $logowidth - 5;
      $y = $imageheight - $logoheight -5;
      break;
    }
    $dst_photo = @imagecreatetruecolor($imagewidth, $imageheight);
    $target_photo = $imagecreatefunc($this->targetfile);
    imagecopy($dst_photo, $target_photo, 0, 0, 0, 0, $imagewidth, $imageheight);
    if($this->watermarktype == 1)
    {
     imagecopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight);
    }
    elseif($this->watermarktype == 2)
    {
     if(($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor'])
     {
      $shadowcolorrgb = explode(',', $this->watermarktext['shadowcolor']);
      $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
      imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'],
      $x + $ax + $this->watermarktext['shadowx'], $y + $ay + $this->watermarktext['shadowy'], $shadowcolor,
      $this->watermarktext['fontpath'], $this->watermarktext['text']);
     }
     $colorrgb = explode(',', $this->watermarktext['color']);
     $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
     imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'],
     $x + $ax, $y + $ay, $color, $this->watermarktext['fontpath'], $this->watermarktext['text']);
    }
    else
    {
     imagealphablending($watermark_logo, true);
     imagecopymerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight, $this->watermarktrans);
    }
    $targetfile = !$preview ? $this->targetfile : './watermark_tmp.jpg';
    if($this->attachinfo['mime'] == 'image/jpeg')
    {
     $imagefunc($dst_photo, $targetfile, $this->watermarkquality);
    }
    else
    {
     $imagefunc($dst_photo, $targetfile);
    }
    $this->attach['size'] = filesize($this->targetfile);
   }
  }
 }
}//End Class

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
自动生成文章摘要的代码[PHP 版本]
Mar 20 PHP
在php MYSQL中插入当前时间
Apr 06 PHP
PHP实现变色验证码实例
Jan 06 PHP
用 Composer构建自己的 PHP 框架之构建路由
Oct 30 PHP
PHP附件下载中文名称乱码的解决方法
Dec 17 PHP
php实现的一段简单概率相关代码
May 30 PHP
学习PHP Cookie处理函数
Aug 09 PHP
PHPCMS2008广告模板SQL注入漏洞修复
Oct 11 PHP
php 字符串中是否包含指定字符串的多种方法
Apr 12 PHP
Swoole实现异步投递task任务案例详解
Apr 02 PHP
PHP中有关长整数的一些操作教程
Sep 11 PHP
laravel-admin 管理平台获取当前登陆用户信息的例子
Oct 08 PHP
PHP模拟asp.net的StringBuilder类实现方法
Aug 08 #PHP
php自动识别文字编码并转换为目标编码的方法
Aug 08 #PHP
PHP模拟asp中response类实现方法
Aug 08 #PHP
PHP实现根据图片色界在不同位置加水印的方法
Aug 08 #PHP
PHP中使用curl伪造IP的简单方法
Aug 07 #PHP
smarty中常用方法实例总结
Aug 07 #PHP
php递归函数三种实现方法及如何实现数字累加
Aug 07 #PHP
You might like
php大小写转换函数(strtolower、strtoupper)用法介绍
2017/11/17 PHP
PHP实现简易计算器功能
2020/08/28 PHP
PHP fopen函数用法实例讲解
2019/02/15 PHP
javascript 使td内容不换行不撑开
2012/11/29 Javascript
js拦截alert对话框另类应用
2013/01/16 Javascript
从数据结构的角度分析 for each in 比 for in 快的多
2013/07/07 Javascript
js螺旋动画效果的具体实例
2013/11/15 Javascript
超简单JS二级、多级联动的简单实例
2014/02/18 Javascript
Internet Explorer 11 浏览器介绍:别叫我IE
2014/09/28 Javascript
Nodejs的express使用教程
2015/11/23 NodeJs
JavaScript学习笔记之数组求和方法
2016/03/23 Javascript
AngularJs基本特性解析(一)
2016/07/21 Javascript
JS数组去掉重复数据只保留一条的实现代码
2016/08/11 Javascript
js常用的继承--组合式继承
2017/03/06 Javascript
ES5学习教程之Array对象
2017/04/01 Javascript
JavaScript正则表达式和级联效果
2017/09/14 Javascript
在 Vue-CLI 中引入 simple-mock实现简易的 API Mock 接口数据模拟
2018/11/28 Javascript
layui实现下拉复选功能的例子(包括数据的回显与上传)
2019/09/24 Javascript
vue 强制组件重新渲染(重置)的两种方案
2019/10/29 Javascript
Selenium执行Javascript脚本参数及返回值过程详解
2020/04/01 Javascript
Python Deque 模块使用详解
2014/07/04 Python
Windows下python3.7安装教程
2018/07/31 Python
Python两个字典键同值相加的几种方法
2019/03/05 Python
pow在python中的含义及用法
2019/07/11 Python
Django中Q查询及Q()对象 F查询及F()对象用法
2020/07/09 Python
python推导式的使用方法实例
2021/02/28 Python
canvas之万花筒效果的简单实现(推荐)
2016/08/16 HTML / CSS
HTML5 canvas实现雪花飘落特效
2016/03/08 HTML / CSS
详解html5页面 rem 布局适配方法
2018/01/12 HTML / CSS
HTML5 视频播放(video),JavaScript控制视频的实例代码
2018/10/08 HTML / CSS
Fanatics官网:运动服装、球衣、运动装备
2020/10/12 全球购物
客户接待方案
2014/02/26 职场文书
建筑节能汇报材料
2014/08/22 职场文书
销售顾问工作计划书
2014/09/15 职场文书
同事去世追悼词
2015/06/23 职场文书
vue2实现provide inject传递响应式
2021/05/21 Vue.js