OSDN Git Service

v2.2.0
[nucleus-jp/nucleus-plugins.git] / trunk / NP_TrimImage / NP_TrimImage.php
index d850aa7..5f6cd91 100644 (file)
@@ -1,4 +1,45 @@
 <?php
+// vim: tabstop=2:shiftwidth=2
+
+/**
+  * NP_TrimImage ($Revision: 1.9 $)
+  * by nakahara21 ( http://nakahara21.com/ )
+  * by hsur ( http://blog.cles.jp/np_cles/ )
+  * $Id: NP_TrimImage.php,v 1.9 2007-03-06 20:55:44 hsur Exp $
+  *
+  * Based on NP_TrimImage 1.0 by nakahara21
+  * http://nakahara21.com/?itemid=512
+*/
+
+/*
+  * Copyright (C) 2004-2006 nakahara21 All rights reserved.
+  * Copyright (C) 2006-2007 cles All rights reserved.
+  *
+  * This program is free software; you can redistribute it and/or
+  * modify it under the terms of the GNU General Public License
+  * as published by the Free Software Foundation; either version 2
+  * of the License, or (at your option) any later version.
+  * 
+  * This program is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  * GNU General Public License for more details.
+  * 
+  * You should have received a copy of the GNU General Public License
+  * along with this program; if not, write to the Free Software
+  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+  * 
+  * In addition, as a special exception, mamio and cles gives
+  * permission to link the code of this program with those files in the PEAR
+  * library that are licensed under the PHP License (or with modified versions
+  * of those files that use the same license as those files), and distribute
+  * linked combinations including the two. You must obey the GNU General Public
+  * License in all respects for all of the code used other than those files in
+  * the PEAR library that are licensed under the PHP License. If you modify
+  * this file, you may extend this exception to your version of the file,
+  * but you are not obligated to do so. If you do not wish to do so, delete
+  * this exception statement from your version.
+*/
 
 //history
 //     0.2:    $archive, $blogid and $catid suppot ($exmode=all ready)
 //     0.7:    supports templatevar
 //                     supports <%popup()%> 
 //     0.8:    supports gif
-//     0.9             doTemplateVar calls DB data for other PreItem Plugin
+//     0.9:    doTemplateVar calls DB data for other PreItem Plugin
 //     0.9:    change '&' to '&amp;'
-
-class NP_TrimImage extends NucleusPlugin
-{
-
-       function getName ()
-       {
+//     1.1:    NP_Paint support.
+//             Security Fix.
+//     2.0:    use phpThumb() (http://phpthumb.sourceforge.net)
+//     2.1:    update regex
+//             add alt/title attribute
+//             bug fix
+//  2.2:       support <img /> tag. 
+//                     doTemplateVar() bug fix.
+//                     Add ENT_QUOTES to htmlspecialchars()
+//                     Add ExtractImageMode
+
+define('NP_TRIMIMAGE_FORCE_PASSTHRU', true); //passthru(standard)
+//define('NP_TRIMIMAGE_FORCE_PASSTHRU', false); //redirect(advanced)
+
+define('NP_TRIMIMAGE_CACHE_MAXAGE', 86400 * 30); // 30days
+define('NP_TRIMIMAGE_PREFER_IMAGEMAGICK', false);
+
+require_once(dirname(__FILE__).'/sharedlibs/sharedlibs.php');
+require_once('phpthumb/phpthumb.functions.php');
+require_once('phpthumb/phpthumb.class.php');
+
+class NP_TrimImage extends NucleusPlugin {
+       function getName() {
                return 'TrimImage';
        }
 
-       function getAuthor ()
-       {
-               return 'nakahara21';
+       function getAuthor() {
+               return 'nakahara21 + hsur';
        }
 
-       function getURL () {
-               return 'http://nakahara21.com/';
+       function getURL() {
+               return 'http://blog.cles.jp/np_cles/category/31/subcatid/15';
        }
-       
-       function getVersion () {
-               return '1.0';
+
+       function getVersion() {
+               return '2.2.0';
        }
-       
-       function supportsFeature($what)
-       {
+
+       function supportsFeature($what) {
                switch ($what) {
-                       case 'SqlTablePrefix':
+                       case 'SqlTablePrefix' :
                                return 1;
-                       default:
+                       default :
                                return 0;
                }
        }
 
-       function getDescription ()
-       {
-               return 'Extract image in items, and embed these images.';
+       function getDescription() {
+               return 'Trim image in items, and embed these images.';
        }
-
-       function instaii()
-       {
-               $ver_min = (getNucleusVersion() < $this->getMinNucleusVersion());
-               $pat_min = ((getNucleusVersion() == $this->getMinNucleusVersion()) &&
-                       (getNucleusPatchLevel() < $this->getMinNucleusPatchLevel()));
-               if ($ver_min || $pat_min) {
-                       $this->_attention();
-               }
+               \r       function getEventList() {
+               return array ('PostAddItem', 'PostUpdateItem', 'PostDeleteItem',);
        }
-
-       function _attention()
-       {
-               global $admin;
-               $admin->pagehead();
-               echo '<h2>ATTENTION</h2>';
-               echo 'Your Nucleus version is old<br />';
-               echo 'Please version-up Nucleus CORE<br />';
-               echo 'newest version is 3.23 !!<br />';
-               echo '<a href="index.php" onclick="history.back()">'._BACK.'</a>';
-               $admin->pagefoot();
-               return;
+       
+       function event_PostAddItem(& $data) {
+               $this->_clearCache();
+       }
+       function event_PostUpdateItem(& $data) {
+               $this->_clearCache();
+       }
+       function event_PostDeleteItem(& $data) {
+               $this->_clearCache();
+       }
+       function _clearCache() {
+               /*
+               $phpThumb = new phpThumb();
+               foreach ($this->phpThumbParams as $paramKey => $paramValue) {
+                       $phpThumb->setParameter($paramKey, $paramValue);
+               }
+               $phpThumb->setParameter('config_cache_maxage', 1);
+               $phpThumb->CleanUpCacheDirectory();
+               var_dump($phpThumb);
+               */
        }
 
-       function init()
-       {
-               $this->fileex = array('.gif', '.jpg', '.png');
-               $this->random = 1;
+       function init() {
+               global $DIR_MEDIA;
+               $this->fileex = array ('.gif', '.jpg', '.png');
+               $cacheDir = $DIR_MEDIA.'phpthumb/';
+               $cacheDir = (is_dir($cacheDir) && @ is_writable($cacheDir)) ? $cacheDir : null;
+               
+               $this->phpThumbParams = array(
+                       'config_document_root' => $DIR_MEDIA,
+                       'config_cache_directory' => $cacheDir,
+                       'config_cache_disable_warning' => true,
+                       'config_cache_directory_depth' => 0,
+                       'config_cache_maxage' => NP_TRIMIMAGE_CACHE_MAXAGE,
+                       'config_cache_maxsize' => 10 * 1024 * 1024, // 10MB
+                       'config_cache_maxfiles' => 1000,
+                       'config_cache_source_filemtime_ignore_local' => true,
+                       'config_cache_cache_default_only_suffix' => '',
+                       'config_cache_prefix' => 'phpThumb_cache',
+                       'config_cache_force_passthru' => NP_TRIMIMAGE_FORCE_PASSTHRU,
+                       'config_max_source_pixels' => 3871488, //4Mpx
+                       'config_output_format' => 'jpg',
+                       'config_disable_debug' => true,
+                       'config_prefer_imagemagick' => NP_TRIMIMAGE_PREFER_IMAGEMAGICK,
+               );
        }
        
-       function doSkinVar($skinType, $amount = 10, $wsize = 80, $hsize = 80, $point = 0, $random = 0, $exmode = '')
-       {
+       function doSkinVar($skinType, $amount = 10, $wsize = 80, $hsize = 80, $point = 0, $random = 0, $exmode = '', $titlemode = '', $includeImg = 'true') {
                global $CONF, $manager, $blog;
                if ($blog) {
-                       $b =& $blog;
+                       $b = & $blog;
                } else {
-                       $b =& $manager->getBlog($CONF['DefaultBlog']);
+                       $b = & $manager->getBlog($CONF['DefaultBlog']);
                }
-/*             ($blog)?
-                       $b =& $blog :
-                       $b =& $manager->getBlog($CONF['DefaultBlog']);*/
                
-               if ($amount=='') $amount = 10;
-               if ($hsize=='') $hsize = 80;
-               if ($wsize=='') $wsize = 80;
-               if ($point != 'lefttop') $point = 0;
-               $this->exquery = '';
+               if (!is_numeric($amount))
+                       $amount = 10;
+               if (!is_numeric($hsize))
+                       $hsize = 80;
+               if (!is_numeric($wsize))
+                       $wsize = 80;
+               $point = ($point == 'lefttop') ? true : false;
+               $includeImg = ( $includeImg == 'true' ) ? true : false;
                
+               $this->exquery = '';
 
-               switch($skinType){
-                       case 'archive':
+               switch ($skinType) {
+                       case 'archive' :
                                global $archive;
                                $year = $month = $day = '';
-                               sscanf($archive, '%4c-%2c-%2c', $year, $month, $day);
-                               if (empty($day)) {
+                               sscanf($archive, '%d-%d-%d', $year, $month, $day);
+                               if (empty ($day)) {
                                        $timestamp_start = mktime(0, 0, 0, $month, 1, $year);
-                                       $timestamp_end = mktime(0, 0, 0, $month + 1, 1, $year);  // also works when $month==12
+                                       $timestamp_end = mktime(0, 0, 0, $month +1, 1, $year); // also works when $month==12
                                } else {
                                        $timestamp_start = mktime(0, 0, 0, $month, $day, $year);
-                                       $timestamp_end = mktime(0, 0, 0, $month, $day + 1, $year);  
+                                       $timestamp_end = mktime(0, 0, 0, $month, $day +1, $year);
                                }
                                $this->exquery .= ' and itime >= ' . mysqldate($timestamp_start)
-                                                               ' and itime < ' . mysqldate($timestamp_end);
+                                                                       .' and itime < ' . mysqldate($timestamp_end);
 
-//                     break;
-                       default:
+                               //break;
+                       default :
                                if ($exmode == '') {
-                                       $this->exquery .= ' and iblog = ' . intval($b->getID());
+                                       $this->exquery .= ' and iblog = '.intval($b->getID());
                                        global $catid;
-                                       if ($catid) $this->exquery .= ' and icat = ' . intval($catid);
-                               } elseif ($exmode == 'all') {
+                                       if ($catid)
+                                               $this->exquery .= ' and icat = '.intval($catid);
+                               }
+                               elseif ($exmode == 'all') {
+                                       // nothing
                                } else {
-                                       $spid_array = $spbid = $spcid = array();
+                                       $spid_array = $spbid = $spcid = array ();
                                        $spid_array = explode('/', $exmode);
                                        foreach ($spid_array as $spid) {
                                                if (substr($spid, 0, 1) == 'b')
@@ -131,272 +209,312 @@ class NP_TrimImage extends NucleusPlugin
                                        }
                                        $spbid = implode(',', $spbid);
                                        $spcid = implode(',', $spcid);
-                                       if ($spbid && $spcid) {
-                                               $this->exquery .= ' and ( iblog IN (' . $spbid . ') or icat IN (' . $spcid . ') )';
-                                       } elseif($spbid) {
-                                               $this->exquery .= ' and iblog IN (' . $spbid . ') ';
-                                       } elseif ($spcid) {
-                                               $this->exquery .= ' and icat IN (' . $spcid . ') ';
+                                       if ($spbid) {
+                                               $this->exquery .= ' and iblog IN ('.$spbid.') ';
+                                       }
+                                       if ($spcid) {
+                                               $this->exquery .= ' and icat IN ('.$spcid.') ';
                                        }
                                }
                }
 
-
-               $filelist = array();
-               $this->imglists = array();
-               $this->imgfilename = array();
-               if (!($filelist = $this->listup())) {
-                       echo 'No images here.';
+               $filelist = array ();
+               $this->imglists = array ();
+               $this->imgfilename = array ();
+               $random = $random ? true : false;
+               if (!($filelist = $this->listup($amount, $random, $includeImg))) {
+                       //echo 'No images here.';
                        return;
                }
-//             print_r($filelist);
-               $amount = min($amount,count($filelist));
-               if ($random) {
-                       srand((float)microtime()*1000000);
-                       shuffle($filelist);
-               }
 
+               $amount = min($amount, count($filelist));
                echo '<div>';
-               for ($i=0;$i<$amount;$i++) {
-                       $itemlink = $this->createGlobalItemLink($filelist[$i][1], '');  // why not createItemLink ?
-//                     $itemlink = $this->createItemLink($filelist[$i][1]);
-                       echo '<a href="' . $itemlink . '">';
+               for ($i = 0; $i < $amount; $i ++) {
+                       $itemlink = $this->createGlobalItemLink($filelist[$i][1], ''); // why not createItemLink ?
+                       //                      $itemlink = $this->createItemLink($filelist[$i][1]);
+                       echo '<a href="'.$itemlink.'">';
+
+                       $src = '';
+                       if (!$this->phpThumbParams['config_cache_force_passthru']) {
+                               $src = $this->createImage($filelist[$i][0], $wsize, $hsize, $point, true);
+                       }
+                       if (!$src) {
+                               $src = htmlspecialchars($CONF['ActionURL'], ENT_QUOTES)
+                                               .'?action=plugin&amp;name=TrimImage&amp;type=draw'.'&amp;p='
+                                               .$filelist[$i][0].'&amp;wsize='.$wsize.'&amp;hsize='.$hsize
+                                               . ($point ? '&amp;pnt=lefttop' : '');
+                       }
                        
-                       $exq = '';
-                       if ($point) $exq = '&amp;pnt=lefttop';
-                               
-                       echo '<img src="' . $CONF['ActionURL'] . '?action=plugin&amp;name=TrimImage&amp;type=draw&amp;p=' . $filelist[$i][0][0] . '&amp;wsize=' . $wsize . '&amp;hsize=' . $hsize . $exq . '" />';
+                       if($titlemode == 'item')
+                               $title = ($filelist[$i][4]) ? $filelist[$i][4] : $filelist[$i][2];
+                       else
+                               $title = ($filelist[$i][2]) ? $filelist[$i][2] : $filelist[$i][4];
+
+                       echo '<img src="'.$src.'" '                     
+                               . ( $wsize ? 'width="'.$wsize.'"'  : '' )
+                               . ( $hsize ? 'height="'.$hsize.'"' : '' )
+                               . 'alt="'.htmlspecialchars($title, ENT_QUOTES)
+                               . '" title="'.htmlspecialchars($title, ENT_QUOTES).'"/>';
                        echo "</a>\n";
                }
                echo "</div>\n";
        }
 
-       function listup()
-       {
+       function listup($amount = 10, $random = false, $includeImg = true) {
                global $CONF, $manager, $blog;
                if ($blog) {
-                       $b =& $blog;
+                       $b = & $blog;
                } else {
-                       $b =& $manager->getBlog($CONF['DefaultBlog']);
+                       $b = & $manager->getBlog($CONF['DefaultBlog']);
                }
-/*             ($blog)?
-                       $b =& $blog :
-                       $b =& $manager->getBlog($CONF['DefaultBlog']);*/
 
                $query = 'SELECT inumber as itemid, ititle as title, ibody as body, iauthor, itime, imore as more,';
                $query .= ' icat as catid, iclosed as closed';
-               $query .= ' FROM ' . sql_table('item');
+               $query .= ' FROM '.sql_table('item');
                $query .= ' WHERE idraft = 0';
-               $query .= ' and itime <= ' . mysqldate($b->getCorrectTime());   // don't show future items!
+               $query .= ' and itime <= '.mysqldate($b->getCorrectTime()); // don't show future items!
                $query .= $this->exquery;
-               $query .= ' ORDER BY itime DESC'; 
-       
+               $query .= ' ORDER BY itime DESC LIMIT '.intval($amount * 10);
+
                $res = sql_query($query);
-               
-               if (!mysql_num_rows($res)) return FALSE;
-               
+
+               if (!mysql_num_rows($res))
+                       return FALSE;
+
                while ($it = mysql_fetch_object($res)) {
-                       $txt = $it->body . $it->more;
-                       preg_match_all("/\<\%image\((.*)\)\%\>/Us", $txt, $imgpnt, PREG_PATTERN_ORDER);
-                       @array_walk($imgpnt[1], array(&$this, "exarray"), array($it->itemid, $it->iauthor));
-                       preg_match_all("/\<\%popup\((.*)\)\%\>/Us", $txt, $imgpntp, PREG_PATTERN_ORDER);
-                       @array_walk($imgpntp[1], array(&$this, "exarray"), array($it->itemid, $it->iauthor));
-               }
-               return $this->imglists;
-       }
+                       $txt = $it->body.$it->more;
+                       if (preg_match_all("/<%(image|popup|paint)\((.*?)\)%>/s", $txt, $imgpnt))
+                               @ array_walk($imgpnt[2], array (& $this, "exarray"), array ($it->itemid, $it->iauthor, $it->title));
 
-       function exarray($imginfo,$key,$iaid)
-       {
-               $imginfo = explode("|", $imginfo);
-               if (!in_array(strtolower(strrchr($imginfo[0], "." )), $this->fileex)) return;
-               if (in_array($imginfo[0], $this->imgfilename)) return;
-               $this->imgfilename[] = $imginfo[0];
-               if (!strstr($imginfo[0], '/')) {
-                       $imginfo[0] = $iaid[1] . '/' . $imginfo[0];
+                       if ($includeImg && preg_match_all("/<img (.*?)>/s", $txt, $imgtmp)){
+                               foreach($imgtmp[1] as $tmp){
+                                       $this->exarray($tmp, null, array($it->itemid, $it->iauthor, $it->title), true);
+                               }
+                       }
+
+                       if (count($this->imglists) >= $amount)
+                               break;
                }
-               $this->imglists[] = array($imginfo, $iaid[0]);
+               mysql_free_result($res);
+
+               if ($random)
+                       shuffle($this->imglists);
+               $this->imglists = array_slice($this->imglists, 0, $amount);
+               return $this->imglists;
        }
 
-       function baseimageCreate($p,$imgtype)
-       {
-               switch ($imgtype) {
-                       case 1:
-                               return ImageCreateFromGif($p);
-                       case 2:
-                               return ImageCreateFromJpeg($p);
-                       case 3:
-                               return ImageCreateFromPng($p);
-                       default:
+       function exarray($imginfo, $key, $params, $isImg = false) {
+               global $CONF;
+               if ($isImg){
+                       if( preg_match_all('/(src|width|height|alt)=\"(.*?)\"/i', $imginfo, $matches) ) {
+                               $index = 0;
+                               $param = array();
+                               foreach( $matches[1] as $type ){
+                                       $param[$type] = $matches[2][$index];                                            
+                                       $index++;
+                               }
+                               if( $param['src'] && ( strpos($param['src'], $CONF['MediaURL']) === 0 ) ){
+                                       $imginfo = substr( $param['src'], strlen($CONF['MediaURL']) )
+                                       . '|' . $param['width']
+                                       . '|' . $param['height']
+                                       . '|' . $param['alt'];
+                               }
+                       } else {
                                return;
+                       }
+               }
+               
+               list ($url, $w, $h, $alt, $ext) = explode("|", $imginfo, 5);
+               if (!in_array(strtolower(strrchr($url, ".")), $this->fileex))
+                       return;
+               if (in_array($url, $this->imgfilename))
+                       return;
+               $this->imgfilename[] = $url;
+               if (!strstr($url, '/')) {
+                       $url = $params[1].'/'.$url;
                }
+               $this->imglists[] = array ($url, $params[0], $alt, $ext, $params[2]);
        }
 
-       function doTemplateVar(&$item, $wsize=80, $hsize=80, $point=0, $maxAmount=0){
-       {
+       function doTemplateVar(& $item, $wsize = 80, $hsize = 80, $point = 0, $maxAmount = 0, $titlemode = '', $includeImg = 'true') {
                global $CONF;
-               if ($hsize=='') $hsize = 80;
-               if ($wsize=='') $wsize = 80;
-               if ($point != 'lefttop') $point = 0;
+               if (!is_numeric($hsize))
+                       $hsize = 80;
+               if (!is_numeric($wsize))
+                       $wsize = 80;
+               $point = ($point == 'lefttop') ? true : false;
+               $includeImg = ( $includeImg == 'true' ) ? true : false;
                
-               $filelist = array();
-               $this->imglists = array();
-               $this->imgfilename = array();
-//                     $txt = $item->body.$item->more;
-                       $txt = '';
-                       $q = 'SELECT ibody as body, imore as more FROM '.sql_table('item').' WHERE inumber='.intval($item->itemid);
-                       $r = sql_query($q);
-                       while ($d = mysql_fetch_object($r)) {
-                               $txt .= $d->body.$d->more;
-                       }
+               $filelist = array ();
+               $this->imglists = array ();
+               $this->imgfilename = array ();
+               //$txt = $item->body.$item->more;
+               $txt = '';
+               $q = 'SELECT ibody as body, imore as more, ititle as title FROM '.sql_table('item').' WHERE inumber='.intval($item->itemid);
+               $r = sql_query($q);
+               while ($d = mysql_fetch_object($r)) {
+                       $txt .= $d->body.$d->more;
+               }
 
-                       preg_match_all("/\<\%image\((.*)\)\%\>/Us", $txt, $imgipnt, PREG_PATTERN_ORDER);
-                       @array_walk($imgipnt[1], array(&$this, "exarray"), array($item->itemid, $item->authorid));
-                       preg_match_all("/\<\%popup\((.*)\)\%\>/Us",$txt,$imgipntp, PREG_PATTERN_ORDER);
-                       @array_walk($imgipntp[1], array(&$this, "exarray"), array($item->itemid, $item->authorid));
+               if (preg_match_all("/<%(image|popup|paint)\((.*?)\)%>/s", $txt, $imgpnt))
+                       @ array_walk($imgpnt[2], array (& $this, "exarray"), array ($item->itemid, $item->authorid, $item->title));
                        
-                       $filelist = $this->imglists;
-//                     print_r($filelist);
-                       if(!$maxAmount)
-                               $amount = count($filelist);
-                       else
-                               $amount = min($maxAmount, count($filelist));
+               if ($includeImg && preg_match_all("/<img (.*?)>/s", $txt, $imgtmp)){
+                       foreach($imgtmp[1] as $tmp){
+                               $this->exarray($tmp, null, array ($item->itemid, $item->authorid, $item->title), true);
+                       }
+               }
 
-//             echo '<div style="text-align:center;padding:3px;">';
+               $filelist = $this->imglists;
+               if (!$maxAmount)
+                       $amount = count($filelist);
+               else
+                       $amount = min($maxAmount, count($filelist));
 
                if (!$amount) {
-//                     echo '<img src="" width="'.$wsize.'" height="'.$hsize.'" />';
-                       $img_tag = '<img src="' . $CONF['ActionURL'] . '?action=plugin&amp;name=TrimImage';
-                       $img_tag .= '&amp;type=draw&amp;p=non&amp;wsize=' . $wsize . '&amp;hsize=' . $hsize . $exq;
-                       $img_tag .= '" width="' . $wsize . '" height="' . $hsize . '" />';
+                       $img_tag = '<img src="'.htmlspecialchars($CONF['ActionURL'], ENT_QUOTES).'?action=plugin&amp;name=TrimImage';
+                       $img_tag .= '&amp;type=draw&amp;p=non&amp;wsize='.$wsize.'&amp;hsize='.$hsize.$exq;
+                       $img_tag .= '" width="'.$wsize.'" height="'.$hsize.'" />';
                        echo $img_tag;
+               } else {
+                       for ($i = 0; $i < $amount; $i ++) {
+                               $src = '';
+                               if (!$this->phpThumbParams['config_cache_force_passthru']) {
+                                       $src = $this->createImage($filelist[$i][0], $wsize, $hsize, $point, true);
+                               }
+                               if (!$src) {
+                                       $src = htmlspecialchars($CONF['ActionURL'], ENT_QUOTES).'?action=plugin&amp;name=TrimImage&amp;type=draw'.'&amp;p='.$filelist[$i][0].'&amp;wsize='.$wsize.'&amp;hsize='.$hsize. ($point ? '&amp;pnt=lefttop' : '');
+                               }
+                               
+                               if($titlemode == 'item')
+                                       $title = ($filelist[$i][4]) ? $filelist[$i][4] : $filelist[$i][2];
+                               else
+                                       $title = ($filelist[$i][2]) ? $filelist[$i][2] : $filelist[$i][4];
+                               
+                               echo '<img src="'.$src.'" '                     
+                                       . ( $wsize ? 'width="'.$wsize.'"'  : '' )
+                                       . ( $hsize ? 'height="'.$hsize.'"' : '' )
+                                       . 'alt="'.htmlspecialchars($title, ENT_QUOTES)
+                                       . '" title="'.htmlspecialchars($title, ENT_QUOTES).'"/>';
+                               }
                }
+       }
 
+       function doAction($type) {
+               $w = is_numeric(requestVar('wsize')) ? requestVar('wsize') : 80;
+               $h = is_numeric(requestVar('hsize')) ? requestVar('hsize') : 80;
+               $isLefttop = (requestVar('pnt') == 'lefttop') ? true : false;
 
-               for ($i=0;$i<$amount;$i++) {
-//                     $itemlink = $this->createGlobalItemLink($filelist[$i][1], '');
-//                     echo '<a href="'.$itemlink.'">';
-                       
-                       $exq = '';
-                       if ($point) $exq = '&amp;pnt=lefttop';
-                               
-                       $img_tag = '<img src="' . $CONF['ActionURL'] . '?action=plugin&amp;name=TrimImage&amp;type=draw';
-                       $img_tag .= '&amp;p=' . $filelist[$i][0][0] . '&amp;wsize=' . $wsize . '&amp;hsize=' . $hsize . $exq;
-                       $img_tag .= '" width="' . $wsize . '" height="' . $hsize . '" />';
-                       echo $img_tag;
-//                     echo '<img src="'.$CONF['ActionURL'].'?action=plugin&name=TrimImage&type=draw&p='.$filelist[$i][0][0].'&wsize='.$wsize.'&hsize='.$hsize.$exq.'" width="'.$wsize.'" height="'.$hsize.'" />';
-//                     echo '<br />'.shorten(strip_tags($item->title),16,'...');
-//                     echo "</a>\n";
+               switch ($type) {
+                       case 'draw' :
+                               $this->createImage(requestVar('p'), $w, $h, $isLefttop);
+                               break;
+                       default :
+                               return 'No such action';
+                               break;
                }
-//             echo "</div>\n";
        }
 
+       function createImage($p, $w, $h, $isLefttop, $cacheCheckOnly = false) {
+               $phpThumb = new phpThumb();
+               foreach ($this->phpThumbParams as $paramKey => $paramValue) {
+                       $phpThumb->setParameter($paramKey, $paramValue);
+               }
 
-       function doAction($type)
-       {
-               global $CONF;
-               global $DIR_MEDIA;
-               $return = serverVar('HTTP_REFERER');
-               switch ($type) {
-                       case draw:
-                               if (!requestVar('p')) return;
-                               $p = $DIR_MEDIA . requestVar('p');      //path
-               
-                               
-                               if (requestVar('p') == 'non') {
-                                       $im = ImageCreate(requestVar('wsize'), requestVar('hsize')) or die ("Cannnot Initialize new GD image stream");
-                                       $bgcolor = ImageColorAllocate($im, 0, 255, 255); //color index:0
-//                                     $strcolor = ImageColorAllocate($im,153,153,153); //color index:1
-                                       imagecolortransparent($im, $bgcolor);
-//                                     imageString($im, 1, 4, 0,'No images',$strcolor);
-                                       header ("Content-type: image/png");
-                                       ImagePng($im);
-                                       imagedestroy($im);
-                                       berak;
-                               }
+               if($h) $phpThumb->setParameter('h', intval($h));
+               if($w) $phpThumb->setParameter('w', intval($w));
 
-                               list($imgwidth, $imgheight, $imgtype) = GetImageSize($p);
-               
-                               $tsize['w'] = requestVar('wsize');
-                               $tsize['h'] = requestVar('hsize');
-                               $point = requestVar('pnt');
-                               
-                               if ($imgwidth / $imgheight < $tsize['w'] / $tsize['h']) { // height longer
-                                       $trimX = 0;
-                                       $trimW = $imgwidth;
-                                       $trimH = intval($tsize['h'] / $tsize['w'] * $imgwidth);
-                                       $trimY = intval(($imgheight - $trimH) / 2);
-                               } else { // width longer
-                                       $trimY = 0;
-                                       $trimH = $imgheight;
-                                       $trimW = intval($tsize['w'] / $tsize['h'] * $imgheight);
-                                       $trimX = intval(($imgwidth - $trimW) / 2);
+               if ($p == 'non') {
+                       $phpThumb->setParameter('new', 'FFFFFF');
+               } else {
+                       $phpThumb->setParameter('src', '/'.$p);
+                       if( $w && $h  )
+                               $phpThumb->setParameter('zc', $isLefttop ? 2 : 1);
+                       else
+                               $phpThumb->setParameter('aoe', 1);
+               }
+
+               // getCache     
+               $phpThumb->cache_filename = null;
+               $phpThumb->CalculateThumbnailDimensions();
+               $phpThumb->SetCacheFilename();
+               if (file_exists($phpThumb->cache_filename)) {
+                       $nModified = filemtime($phpThumb->cache_filename);
+                       if (time() - $nModified < NP_TRIMIMAGE_CACHE_MAXAGE) {
+                               global $CONF;
+                               preg_match('/^'.preg_quote($this->phpThumbParams['config_document_root'], '/').'(.*)$/', $phpThumb->cache_filename, $matches);
+                               $fileUrl = $CONF['MediaURL'].$matches[1];
+                               if ($cacheCheckOnly)
+                                       return $fileUrl;
+
+                               header('Last-Modified: '.gmdate('D, d M Y H:i:s', $nModified).' GMT');
+                               if (@ serverVar('HTTP_IF_MODIFIED_SINCE') 
+                                       && ($nModified == strtotime(serverVar('HTTP_IF_MODIFIED_SINCE'))) 
+                                       && @ serverVar('SERVER_PROTOCOL')
+                               ) {
+                                       header(serverVar('SERVER_PROTOCOL').' 304 Not Modified');
+                                       return true;
                                }
-                               
-                               if ($point == 'lefttop') {
-                                       $trimX = $trimY = 0;
+                               if ($getimagesize = @ GetImageSize($phpThumb->cache_filename)) {
+                                       header('Content-Type: '.phpthumb_functions :: ImageTypeToMIMEtype($getimagesize[2]));
                                }
-                               
-                               $im_r = $this->baseimageCreate($p,$imgtype);
-                               $im = ImageCreateTrueColor($tsize['w'],$tsize['h']);
-                               ImageCopyResampled( $im, $im_r, 0, 0, $trimX, $trimY, $tsize['w'], $tsize['h'], $trimW, $trimH);
-                               switch ($imgtype) {
-                                       case 1:
-                                       header ("Content-type: image/gif");
-                                       Imagegif($im);
-                                       imagedestroy($im);
-                                       break;
-                                       case 2:
-                                       header ("Content-type: image/jpeg");
-                                       ImageJpeg($im);
-                                       imagedestroy($im);
-                                       break;
-                                       case 3:
-                                       header ("Content-type: image/png");
-                                       ImagePng($im);
-                                       imagedestroy($im);
-                                       break;
-                                       default:
-                                       return;
+                               elseif (eregi('\.ico$', $phpThumb->cache_filename)) {
+                                       header('Content-Type: image/x-icon');
                                }
-                       break;
-
-                       default:
-                               Header('Location: ' . $return);
-                               break;
-//_=======
+                               if ($this->phpThumbParams['config_cache_force_passthru']) {
+                                       @ readfile($phpThumb->cache_filename);
+                               } else {
+                                       header('Location: '.$fileUrl);
+                               }
+                               return true;
+                       }
+               }
+               if ($cacheCheckOnly) {
+                       unset ($phpThumb);
+                       return false;
                }
+
+               // generate
+               $phpThumb->GenerateThumbnail();
+
+               // putCache
+               if (!rand(0, 20))
+                       $phpThumb->CleanUpCacheDirectory();
+               $phpThumb->RenderToFile($phpThumb->cache_filename);
+               @ chmod($phpThumb->cache_filename, 0666);
+
+               // to browser
+               $phpThumb->OutputThumbnail();
+               unset ($phpThumb);
+               return true;
        }
 
-       function canEdit()
-       {
+       function canEdit() {
                global $member, $manager;
-               if (!$member->isLoggedIn()) return 0;
+               if (!$member->isLoggedIn())
+                       return 0;
                return $member->isAdmin();
        }
 
-
-       function createGlobalItemLink($itemid, $extra = '')
-       {
+       function createGlobalItemLink($itemid, $extra = '') {
                global $CONF, $manager;
                $itemid = intval($itemid);
                if ($CONF['URLMode'] == 'pathinfo') {
-                       $link = $CONF['ItemURL'] . '/item/' . $itemid;
+                       $link = $CONF['ItemURL'].'/item/'.$itemid;
                } else {
                        $blogid = getBlogIDFromItemID($itemid);
-                       $b_tmp =& $manager->getBlog($blogid);
-                       $blogurl = $b_tmp->getURL() ;
-                       if(!$blogurl){
+                       $b_tmp = & $manager->getBlog($blogid);
+                       $blogurl = $b_tmp->getURL();
+                       if (!$blogurl) {
                                $blogurl = $CONF['IndexURL'];
                        }
-                       if(substr($blogurl, -4) != '.php'){
-                               if(substr($blogurl, -1) != '/')
+                       if (substr($blogurl, -4) != '.php') {
+                               if (substr($blogurl, -1) != '/')
                                        $blogurl .= '/';
                                $blogurl .= 'index.php';
                        }
-                       $link = $blogurl . '?itemid=' . $itemid;
+                       $link = $blogurl.'?itemid='.$itemid;
                }
                return addLinkParams($link, $extra);
        }
-
 }
-?>
\ No newline at end of file