Пример #1
0
 function thumbnail($upfiledir, $src, $tName, $tw = '', $th = '', $scale = true, $tDir = "thumb")
 {
     global $iCMS;
     $R = array();
     $tw = empty($tw) ? $iCMS->config['thumbwidth'] : (int) $tw;
     $th = empty($th) ? $iCMS->config['thumbhight'] : (int) $th;
     if ($tw && $th) {
         list($width, $height, $type) = @getimagesize($src);
         if ($width < 1 && $height < 1) {
             $R['width'] = $tw;
             $R['height'] = $th;
             $R['src'] = $src;
             return $R;
         }
         if ($width > $tw || $height > $th) {
             $R['src'] = $upfiledir . $tDir . "/" . $tName . '_' . $tw . 'x' . $th . '.' . substr(strrchr($src, "."), 1);
             if (in_array('Gmagick', get_declared_classes())) {
                 $image = new Gmagick();
                 $image->readImage($src);
                 $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $image->getImageWidth(), "h" => $image->getImageHeight()));
                 $image->resizeImage($im['w'], $im['h'], null, 1);
                 $image->cropImage($tw, $th, 0, 0);
                 //$image->thumbnailImage($gm_w,$gm_h);
                 FS::mkdir($upfiledir . $tDir);
                 $image->writeImage($R['src']);
                 $image->destroy();
             } else {
                 $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $width, "h" => $height), $scale);
                 $R['width'] = $im['w'];
                 $R['height'] = $im['h'];
                 $res = self::imagecreate($type, $src);
                 if ($res) {
                     $thumb = imagecreatetruecolor($im['w'], $im['h']);
                     imagecopyresampled($thumb, $res, 0, 0, 0, 0, $im['w'], $im['h'], $width, $height);
                     //PHP_VERSION != '4.3.2' && self::UnsharpMask($thumb);
                     FS::mkdir($upfiledir . $tDir);
                     self::image($thumb, $type, $R['src']);
                 } else {
                     $R['src'] = $src;
                 }
             }
         } else {
             $R['width'] = $width;
             $R['height'] = $height;
             $R['src'] = $src;
         }
         return $R;
     }
 }
Пример #2
0
 /**
  * Apply transformations on image
  *
  * @param string $source Source image
  * @param array  $params Transformations and parameters
  * @param string $store  Temporary store on disk
  *
  * @return string
  */
 public function transform($source, $params, $store = null)
 {
     try {
         $image = new \Gmagick();
         $image->readImage($source);
         if (isset($params['negate'])) {
             $this->canNegate();
         }
         if (isset($params['rotate'])) {
             $image->rotateImage('#000', $params['rotate']['angle']);
         }
         if (isset($params['crop'])) {
             $image->cropImage($params['crop']['width'], $params['crop']['height'], $params['crop']['x'], $params['crop']['y']);
         }
         if (isset($params['contrast'])) {
             $this->canContrast();
         }
         if (isset($params['brightness'])) {
             $value = (int) $params['brightness'];
             $brightness = null;
             if ($value <= 0) {
                 $brightness = $value + 100;
             } else {
                 $brightness = $value * 3 + 100;
             }
             $image->modulateImage($brightness, 100, 100);
         }
         $ret = null;
         if ($store !== null) {
             $ret = $image->writeImage($store);
         } else {
             $ret = $image->getImageBlob();
         }
         $image->destroy();
         return $ret;
     } catch (\GmagickException $e) {
         $image->destroy();
         throw new \RuntimeException($e->getMessage());
     }
 }
Пример #3
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);
     }
 }
Пример #4
0
            }
            return $res;
        }
    }
}
if (fsexists($src)) {
    $image = new Gmagick();
    $image->readImage($src);
    $scale = array("tw" => $tw, "th" => $th, "w" => $image->getImageWidth(), "h" => $image->getImageHeight());
    if ($tw > 0 && $th > 0) {
        $im = scale($scale);
        $image->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));
        $image->cropImage($tw, $th, $x, $y);
    } else {
        empty($scale['th']) && ($scale['th'] = 9999999);
        $im = bitscale($scale);
        $image->resizeImage($im['w'], $im['h'], null, 1);
    }
    $expires = 31536000;
    header("Cache-Control: maxage=" . $expires);
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME']) . ' GMT');
    header('Expires: ' . gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME'] + $expires) . ' GMT');
    $srcData = $image->current();
} else {
    //1x1.gif
    $srcData = 'R0lGODlhAQABAIAAAAAAAP///yH5BAEHAAEALAAAAAABAAEAAAICTAEAOw==';
    //nopic.gif
    $srcData = 'R0lGODlhyADIAKIAAMzMzP///+bm5vb29tXV1d3d3e7u7gAAACH5BAAHAP8ALAAAAADIAMgAAAP/
Пример #5
0
 public static function thumbnail($src, $tw = "0", $th = "0", $scale = true)
 {
     if (!self::$config['thumb']['enable']) {
         return;
     }
     $rs = array();
     $tw = empty($tw) ? self::$config['thumb']['width'] : (int) $tw;
     $th = empty($th) ? self::$config['thumb']['height'] : (int) $th;
     if ($tw && $th) {
         list($width, $height, $type) = getimagesize($src);
         if ($width < 1 && $height < 1) {
             $rs['width'] = $tw;
             $rs['height'] = $th;
             $rs['src'] = $src;
             return $rs;
         }
         if ($width > $tw || $height > $th) {
             $rs['src'] = $src . '_' . $tw . 'x' . $th . '.jpg';
             if (in_array('Gmagick', get_declared_classes())) {
                 $image = new Gmagick();
                 $image->readImage($src);
                 $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $image->getImageWidth(), "h" => $image->getImageHeight()));
                 $image->resizeImage($im['w'], $im['h'], null, 1);
                 $image->cropImage($tw, $th, 0, 0);
                 //$image->thumbnailImage($gm_w,$gm_h);
                 $image->writeImage($rs['src']);
                 $image->destroy();
             } else {
                 $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $width, "h" => $height), $scale);
                 $ret = self::imagecreate($type, $src);
                 $rs['width'] = $im['w'];
                 $rs['height'] = $im['h'];
                 if ($ret) {
                     $thumb = imagecreatetruecolor($im['w'], $im['h']);
                     imagecopyresampled($thumb, $ret, 0, 0, 0, 0, $im['w'], $im['h'], $width, $height);
                     self::image($thumb, $type, $rs['src']);
                 } else {
                     $rs['src'] = $src;
                 }
             }
         } else {
             $rs['src'] = $src;
             $rs['width'] = $width;
             $rs['height'] = $height;
         }
         return $rs;
     }
 }