目錄
摘自织梦CMS中的图片处理类,摘自织梦cms图片
首頁 php教程 php手册 摘自织梦CMS中的图片处理类,摘自织梦cms图片

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

Jun 13, 2016 am 08:56 AM
cms 圖片處理

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

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

<&#63;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') &#63; 'imagecreatefromjpeg' : '';
    $this->imagefunc = function_exists('imagejpeg') &#63; 'imagejpeg' : '';
    break;
   case 'image/gif':
    $this->imagecreatefromfunc = function_exists('imagecreatefromgif') &#63; 'imagecreatefromgif' : '';
    $this->imagefunc = function_exists('imagegif') &#63; 'imagegif' : '';
    break;
   case 'image/png':
    $this->imagecreatefromfunc = function_exists('imagecreatefrompng') &#63; 'imagecreatefrompng' : '';
    $this->imagefunc = function_exists('imagepng') &#63; 'imagepng' : '';
    break;
  }//为空则匹配类型的函数不存在
  $this->attach['size'] = empty($this->attach['size']) &#63; @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 &#63; 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 &#63; '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 &#63; ($this->thumbstatus == 1 &#63; $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 &#63; 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 &#63; DEDEDATA.'/mark/mark.png' : DEDEDATA.'/mark/mark.gif';
    $watermarkinfo = @getimagesize($watermark_file);
    $watermark_logo = $this->watermarktype == 1 &#63; @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 &#63; $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程序设计有所帮助。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1666
14
CakePHP 教程
1425
52
Laravel 教程
1328
25
PHP教程
1273
29
C# 教程
1253
24
Laravel開發建議:如何優化圖片處理與快取 Laravel開發建議:如何優化圖片處理與快取 Nov 22, 2023 am 09:17 AM

Laravel開發建議:如何最佳化圖片處理與快取引言在現代web開發中,圖片處理與快取是一個常見且重要的問題。優化圖片處理和快取策略不僅可以提高網站的效能和使用者體驗,還能減少頻寬消耗和伺服器負載。本文將探討如何在Laravel開發中優化圖片處理與快取的方法與建議。 1.選擇合適的圖片格式選擇合適的圖片格式是優化圖片處理的首要步驟。常見的圖片格式有JPEG、PNG

如何利用Laravel實現圖片處理功能 如何利用Laravel實現圖片處理功能 Nov 04, 2023 pm 12:46 PM

如何利用Laravel實現圖片處理功能,需要具體程式碼範例現如今,隨著網路的發展,圖片處理已成為了網站開發中不可或缺的一部分。 Laravel是一個流行的PHP框架,為我們提供了許多方便的工具來處理圖片。本文將介紹如何利用Laravel實現圖片處理功能,並給出具體的程式碼範例。安裝LaravelInterventionImageInterven

如何使用Hyperf框架進行圖片處理 如何使用Hyperf框架進行圖片處理 Oct 24, 2023 pm 12:04 PM

如何使用Hyperf框架進行圖片處理引言:隨著行動互聯網的快速發展,圖片處理在現代Web開發中變得愈發重要。 Hyperf是一款基於Swoole的高效能框架,它提供了豐富的元件和功能,包括圖片處理。本文將介紹如何使用Hyperf框架進行圖片處理,並提供具體的程式碼範例。一、安裝Hyperf框架:在開始之前,我們先確保已經安裝了Hyperf框架。可以透過Compo

PHP框架與CMS整合:技術融合的無限可能 PHP框架與CMS整合:技術融合的無限可能 Jun 01, 2024 pm 05:16 PM

PHP框架與CMS整合的好處有:1.提高開發效率;2.增強安全性;3.內容管理簡化;4.彈性。實戰案例中,將Laravel框架與WordPressCMS集成,創建了具有自訂功能的部落格網站,集成步驟包括創建Laravel應用程式、安裝WordPress、配置WordPress、創建控制器、定義路由、獲取WordPress數據、在Laravel視圖中顯示數據。

Java開發技巧揭秘:實作圖片處理與浮水印功能 Java開發技巧揭秘:實作圖片處理與浮水印功能 Nov 20, 2023 pm 12:56 PM

Java作為一種世界上最受歡迎的程式語言之一,在開發領域有著廣泛的應用。其中,圖片處理與浮水印功能更是常見的需求之一。本文將揭秘Java開發中實現圖片處理與浮水印功能的技巧,幫助讀者更好地應對這項挑戰。為了實現圖片處理和浮水印功能,我們首先需要了解Java中處理圖片的基本概念和API。 Java提供了豐富的影像處理庫,其中最常用的是Java2DAPI和Java

深入理解Java開發中的圖片處理技巧 深入理解Java開發中的圖片處理技巧 Nov 20, 2023 pm 01:29 PM

隨著網路的快速發展,圖片在我們日常生活和工作中扮演著越來越重要的角色。隨著技術的進步,人們對於圖片品質和數量的要求也越來越高,而Java作為一種非常流行的程式語言,在圖片處理方面也逐漸受到了人們的關注。本文將從Java開發者角度出發,深入探討Java開發中的圖片處理技巧。一、圖片處理的基礎知識在Java開發中進行圖片處理,首先需要了解一些基礎知識。圖片是由

PHPcms與其他系統整合的實用技巧 PHPcms與其他系統整合的實用技巧 Mar 15, 2024 am 08:18 AM

PHPcms與其他系統整合的實用技巧隨著網路技術的不斷發展,網站開發領域也變得愈加多樣化和複雜化。在實際的專案中,我們常常會面對不同系統之間需要整合的情況,這就需要我們具備一定的技巧和經驗來解決這些問題。本文將針對PHPcms系統與其他系統整合的情況,介紹一些實用的技巧和具體的程式碼範例,幫助開發者更好地應對挑戰。一、整合基本原理在進行系統整合時,首先需

PHP框架與CMS:整合背後隱藏的機制 PHP框架與CMS:整合背後隱藏的機制 May 31, 2024 pm 08:05 PM

PHP框架與CMS整合的機制包括:鉤子和事件,允許CMS掛鉤框架的生命週期事件。架橋和適配器,提供標準化方法呼叫CMS功能。自我包含的程式碼,使CMS能夠獨立於框架運作。實戰案例:透過建立自訂路由、控制器和導入資料庫轉儲,可以將WordPress部落格整合到Laravel框架中。

See all articles