摘自織夢(mèng)CMS中的圖片處理類
更新時(shí)間:2015年08月08日 15:29:59 作者:Minho
這篇文章主要介紹了摘自織夢(mèng)CMS中的圖片處理類,通過面向?qū)ο蟮姆绞捷^為詳細(xì)的實(shí)現(xiàn)了php針對(duì)圖片的縮略圖生成及水印添加等操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了摘自織夢(mèng)CMS中的圖片處理類。分享給大家供大家參考。具體如下:
<?php if(!defined('DEDEINC')) exit('dedecms'); /** * 圖像處理類 * * @version $Id: image.class.php 1 18:10 2010年7月5日Z(yǔ) 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; // 析構(gòu)函數(shù),兼容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); } // 析構(gòu)函數(shù) 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; }//為空則匹配類型的函數(shù)不存在 $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 是否預(yù)覽 * @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 是否預(yù)覽 * @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 是否預(yù)覽 * @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進(jìn)行水印 * * @access public * @param int $preview 是否預(yù)覽 * @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
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- DEDECMS首頁(yè)調(diào)用圖片集里的多張圖片
- 解決phpcms更換javascript的幻燈片代碼調(diào)用圖片問題
- dedecms 批量提取第一張圖片最為縮略圖的代碼(文章+軟件)
- 實(shí)現(xiàn)dedecms圖集單擊圖片翻頁(yè)的功能
- 實(shí)現(xiàn)dedecms圖集單擊圖片翻頁(yè)的功能
- 常用的php圖片處理類(水印、等比縮放、固定高寬)分享
- php實(shí)現(xiàn)的通用圖片處理類
- php多功能圖片處理類分享(php圖片縮放類)
- PHP圖片處理類 phpThumb參數(shù)用法介紹
- 讓php處理圖片變得簡(jiǎn)單 基于gb庫(kù)的圖片處理類附實(shí)例代碼下載
相關(guān)文章
Yii列表定義與使用分頁(yè)方法小結(jié)(3種方法)
這篇文章主要介紹了Yii列表定義與使用分頁(yè)方法,總結(jié)分析了3種方法供大家參考,涉及Yii針對(duì)數(shù)據(jù)庫(kù)的查詢、分頁(yè)計(jì)算及相關(guān)類與方法的使用技巧,需要的朋友可以參考下2016-07-07PHP實(shí)現(xiàn)的簡(jiǎn)單mock json腳本分享
這篇文章主要介紹了PHP實(shí)現(xiàn)的簡(jiǎn)單mock json腳本分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-02-02中高級(jí)PHP程序員應(yīng)該掌握哪些技術(shù)?
中高級(jí)PHP程序員應(yīng)該掌握哪些技術(shù)你知道嗎?這篇文章就為大家詳細(xì)介紹了PHP程序員應(yīng)具備的幾類重要技術(shù),感興趣的小伙伴們可以參考一下2016-09-09php基于curl擴(kuò)展制作跨平臺(tái)的restfule 接口
這篇文章主要介紹了php基于curl擴(kuò)展制作跨平臺(tái)的restfule 接口的相關(guān)資料以及詳細(xì)的代碼,有需要的小伙伴可以參考下。2015-05-05