摘自织梦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 相关文章推荐
基于文本的搜索
Oct 09 PHP
一个php作的文本留言本的例子(四)
Oct 09 PHP
使用XDebug调试及单元测试覆盖率分析
Jan 27 PHP
基于PHP常用函数的用法详解
May 10 PHP
浅析PHP绘图技术
Jul 03 PHP
浅析php适配器模式(Adapter)
Nov 25 PHP
php生成curl命令行的方法
Dec 14 PHP
Joomla简单判断用户是否登录的方法
May 04 PHP
PHP使用curl制作简易百度搜索
Nov 03 PHP
Yii2框架制作RESTful风格的API快速入门教程
Nov 08 PHP
php脚本守护进程原理与实现方法详解
Jul 20 PHP
使用PHP连接数据库_实现用户数据的增删改查的整体操作示例
Sep 01 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
解析thinkphp的左右值无限分类
2013/06/20 PHP
详解PHP中foreach的用法和实例
2016/10/25 PHP
轻轻松松学习JavaScript
2007/02/25 Javascript
使用jquery动态加载javascript以减少服务器压力
2012/10/29 Javascript
JavaScript:Div层拖动效果实例代码
2013/08/06 Javascript
Javascript前端UI框架Kit使用指南之kitjs的对话框组件
2014/11/28 Javascript
JavaScript检查数字是否为整数或浮点数的方法
2015/06/09 Javascript
jQuery插件开发精品教程(让你的jQuery更上一个台阶)
2015/11/07 Javascript
JS实现的跨浏览器解析XML文件实例
2016/06/21 Javascript
过期软件破解办法实例详解
2017/01/04 Javascript
JS实现点击Radio动态更新table数据
2017/07/18 Javascript
bootstrap Table插件使用demo
2017/08/07 Javascript
微信小程序如何获取用户手机号
2018/01/26 Javascript
微信小程序实现倒计时补零功能
2018/07/09 Javascript
VUE实现可随意拖动的弹窗组件
2018/09/25 Javascript
bootstrap table合并行数据并居中对齐效果
2018/10/17 Javascript
vue组件三大核心概念图文详解
2019/05/30 Javascript
vue轮播组件实现$children和$parent 附带好用的gif录制工具
2019/09/26 Javascript
原生JS实现九宫格抽奖
2020/09/13 Javascript
Vue3 实现双盒子定位Overlay的示例
2020/12/22 Vue.js
Python入门之三角函数sin()函数实例详解
2017/11/08 Python
Python语言实现百度语音识别API的使用实例
2017/12/13 Python
python实现画圆功能
2018/01/25 Python
Python实战之制作天气查询软件
2019/05/14 Python
Django 导出项目依赖库到 requirements.txt过程解析
2019/08/23 Python
Python解压 rar、zip、tar文件的方法
2019/11/19 Python
python模式 工厂模式原理及实例详解
2020/02/11 Python
python数据库编程 Mysql实现通讯录
2020/03/27 Python
python如何快速生成时间戳
2020/07/21 Python
德国婴儿推车和儿童安全座椅商店:BABYSHOP
2016/09/01 全球购物
编写一个类体现构造,公有,私有方法,静态,私有变量
2013/08/10 面试题
医科大学毕业生自荐信
2014/02/03 职场文书
酒店员工职业生涯规划
2014/02/25 职场文书
批评与自我批评总结
2014/10/17 职场文书
python字符串拼接.join()和拆分.split()详解
2021/11/23 Python
《宝可梦》动画制作25周年到来 官方发布特别纪念视频
2022/04/01 日漫