Exemplo n.º 1
0
 /**
  * 生成缩略图
  * @param  [type]  $path [原图路径]
  * @param  integer $tw   [缩略图宽度]
  * @param  integer $th   [缩略图高度]
  * @return [image]        [缩略图资源]
  */
 public static function make($path, $tw = 1, $th = 1)
 {
     strpos($path, '..') === false or exit('What are you doing?');
     $srcPath = iPHP_RES_PAHT . $path;
     $thumbPath = $srcPath . '_' . $tw . 'x' . $th . '.jpg';
     if (empty($path) || !self::exists($srcPath)) {
         return self::blank();
     }
     iPHP_RES_CACHE && (self::$srcData = self::cache($thumbPath, 'get'));
     if (empty(self::$srcData)) {
         $gmagick = new Gmagick();
         $gmagick->readImage($srcPath);
         $scale = array("tw" => $tw, "th" => $th, "w" => $gmagick->getImageWidth(), "h" => $gmagick->getImageHeight());
         if ($tw > 0 && $th > 0) {
             $im = self::scale($scale);
             $gmagick->resizeImage($im['w'], $im['h'], null, 1);
             $x = $y = 0;
             $im['w'] > $im['tw'] && ($x = ceil(($im['w'] - $im['tw']) / 3));
             $im['h'] > $im['th'] && ($y = ceil(($im['h'] - $im['th']) / 3));
             $gmagick->cropImage($tw, $th, $x, $y);
         } else {
             empty($scale['th']) && ($scale['th'] = 9999999);
             $im = self::bitScale($scale);
             $gmagick->resizeImage($im['w'], $im['h'], null, 1);
         }
         header('X-Thumb-Cache: MAKE-' . $_SERVER['REQUEST_TIME']);
         self::$srcData = $gmagick->current();
         iPHP_RES_CACHE && self::cache($thumbPath, self::$srcData);
     }
 }