侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

框架(架构)  /  管理员 发布于 7年前   115

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

__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程序设计有所帮助。

您可能感兴趣的文章:

  • DEDECMS首页调用图片集里的多张图片
  • 解决phpcms更换javascript的幻灯片代码调用图片问题
  • dedecms 批量提取第一张图片最为缩略图的代码(文章+软件)
  • 实现dedecms图集单击图片翻页的功能
  • 实现dedecms图集单击图片翻页的功能
  • 常用的php图片处理类(水印、等比缩放、固定高宽)分享
  • php实现的通用图片处理类
  • php多功能图片处理类分享(php图片缩放类)
  • PHP图片处理类 phpThumb参数用法介绍
  • 让php处理图片变得简单 基于gb库的图片处理类附实例代码下载


  • 上一条:
    摘自织梦CMS的HTTP文件下载类
    下一条:
    smarty中常用方法实例总结
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客