src/thinkextend/image/driver/Gd.php
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALING)
    {
        if (empty($this->im)) {
            throw new Exception('没有可以被缩略的图像资源');
        }

        //原图宽度和高度
        $w = $this->info['width'];
        $h = $this->info['height'];

        /* 计算缩略图生成的必要参数 */
        switch ($type) {
            /* 等比例缩放 */
            case THINKIMAGE_THUMB_SCALING:
                //原图尺寸小于缩略图尺寸则不进行缩略
                if ($w < $width && $h < $height) {
                    return false;
                }

                //计算缩放比例
                $scale = min($width / $w, $height / $h);

                //设置缩略图的坐标及宽度和高度
                $x      = $y      = 0;
                $width  = $w * $scale;
                $height = $h * $scale;
                break;

            /* 居中裁剪 */
            case THINKIMAGE_THUMB_CENTER:
                //计算缩放比例
                $scale = max($width / $w, $height / $h);

                //设置缩略图的坐标及宽度和高度
                $w = $width / $scale;
                $h = $height / $scale;
                $x = ($this->info['width'] - $w) / 2;
                $y = ($this->info['height'] - $h) / 2;
                break;

            /* 左上角裁剪 */
            case THINKIMAGE_THUMB_NORTHWEST:
                //计算缩放比例
                $scale = max($width / $w, $height / $h);

                //设置缩略图的坐标及宽度和高度
                $x = $y = 0;
                $w = $width / $scale;
                $h = $height / $scale;
                break;

            /* 右下角裁剪 */
            case THINKIMAGE_THUMB_SOUTHEAST:
                //计算缩放比例
                $scale = max($width / $w, $height / $h);

                //设置缩略图的坐标及宽度和高度
                $w = $width / $scale;
                $h = $height / $scale;
                $x = $this->info['width'] - $w;
                $y = $this->info['height'] - $h;
                break;

            /* 填充 */
            case THINKIMAGE_THUMB_FILLED:
                //计算缩放比例
                if ($w < $width && $h < $height) {
                    $scale = 1;
                } else {
                    $scale = min($width / $w, $height / $h);
                }

                //设置缩略图的坐标及宽度和高度
                $neww = $w * $scale;
                $newh = $h * $scale;
                $x    = $this->info['width'] - $w;
                $y    = $this->info['height'] - $h;
                $posx = ($width - $w * $scale) / 2;
                $posy = ($height - $h * $scale) / 2;

                do {
                    //创建新图像
                    $img = imagecreatetruecolor($width, $height);
                    // 调整默认颜色
                    $color = imagecolorallocate($img, 255, 255, 255);
                    imagefill($img, 0, 0, $color);

                    //裁剪
                    imagecopyresampled($img, $this->im, $posx, $posy, $x, $y, $neww, $newh, $w, $h);
                    imagedestroy($this->im); //销毁原图
                    $this->im = $img;
                } while (!empty($this->gif) && $this->gifNext());

                $this->info['width']  = $width;
                $this->info['height'] = $height;
                return $this;

            /* 固定 */
            case THINKIMAGE_THUMB_FIXED:
                $x = $y = 0;
                break;

            default:
                throw new Exception('不支持的缩略图裁剪类型');
        }

        /* 裁剪图像 */
        $this->crop($w, $h, $x, $y, $width, $height);
        return $this;
    }
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALING)
    {
        if (empty($this->im)) {
            throw new Exception('没有可以被缩略的图像资源');
        }

        //原图宽度和高度
        $w = $this->info['width'];
        $h = $this->info['height'];

        /* 计算缩略图生成的必要参数 */
        switch ($type) {
            /* 等比例缩放 */
            case THINKIMAGE_THUMB_SCALING:
                //原图尺寸小于缩略图尺寸则不进行缩略
                if ($w < $width && $h < $height) {
                    return false;
                }

                //计算缩放比例
                $scale = min($width / $w, $height / $h);

                //设置缩略图的坐标及宽度和高度
                $x      = $y      = 0;
                $width  = $w * $scale;
                $height = $h * $scale;
                break;

            /* 居中裁剪 */
            case THINKIMAGE_THUMB_CENTER:
                //计算缩放比例
                $scale = max($width / $w, $height / $h);

                //设置缩略图的坐标及宽度和高度
                $w = $width / $scale;
                $h = $height / $scale;
                $x = ($this->info['width'] - $w) / 2;
                $y = ($this->info['height'] - $h) / 2;
                break;

            /* 左上角裁剪 */
            case THINKIMAGE_THUMB_NORTHWEST:
                //计算缩放比例
                $scale = max($width / $w, $height / $h);

                //设置缩略图的坐标及宽度和高度
                $x = $y = 0;
                $w = $width / $scale;
                $h = $height / $scale;
                break;

            /* 右下角裁剪 */
            case THINKIMAGE_THUMB_SOUTHEAST:
                //计算缩放比例
                $scale = max($width / $w, $height / $h);

                //设置缩略图的坐标及宽度和高度
                $w = $width / $scale;
                $h = $height / $scale;
                $x = $this->info['width'] - $w;
                $y = $this->info['height'] - $h;
                break;

            /* 填充 */
            case THINKIMAGE_THUMB_FILLED:
                //计算缩放比例
                if ($w < $width && $h < $height) {
                    $scale = 1;
                } else {
                    $scale = min($width / $w, $height / $h);
                }

                //设置缩略图的坐标及宽度和高度
                $neww = $w * $scale;
                $newh = $h * $scale;
                $x    = $this->info['width'] - $w;
                $y    = $this->info['height'] - $h;
                $posx = ($width - $w * $scale) / 2;
                $posy = ($height - $h * $scale) / 2;

                do {
                    //创建新图像
                    $img = imagecreatetruecolor($width, $height);
                    // 调整默认颜色
                    $color = imagecolorallocate($img, 255, 255, 255);
                    imagefill($img, 0, 0, $color);

                    //裁剪
                    imagecopyresampled($img, $this->im, $posx, $posy, $x, $y, $neww, $newh, $w, $h);
                    imagedestroy($this->im); //销毁原图
                    $this->im = $img;
                } while (!empty($this->gif) && $this->gifNext());

                $this->info['width']  = $width;
                $this->info['height'] = $height;
                return $this;

            /* 固定 */
            case THINKIMAGE_THUMB_FIXED:
                $x = $y = 0;
                break;

            default:
                throw new Exception('不支持的缩略图裁剪类型');
        }

        /* 裁剪图像 */
        $this->crop($w, $h, $x, $y, $width, $height);
        return $this;
    }
public function thumb ( $ width , $ height , $ type = THINKIMAGE_THUMB_SCALING ) { if ( empty ( $ this -> im ) ) { throw new Exception ( '没有可以被缩略的图像资源'); } //原图宽度和高度 $ w = $ this -> info [ 'width' ] ; $ h = $ this -> info [ 'height' ] ; /* 计算缩略图生成的必要参数 */ switch ( $ type ) { /* 等比例缩放 */ case THINKIMAGE_THUMB_SCALING : //原图尺寸小于缩略图尺寸则不进行缩略 if ( $ w < $ width && $ h < $ height ) { return false ; } //计算缩放比例 $ scale = min ( $ width / $ w , $ height / $ h ) ; //设置缩略图的坐标及宽度和高度 $ x = $ y = 0 ; $ width = $ w * $ scale ; $ height = $ h * $ scale ; break ; /* 居中裁剪 */ case THINKIMAGE_THUMB_CENTER : //计算缩放比例 $ scale = max ( $ width / $ w , $ height / $ h ) ; //设置缩略图的坐标及宽度和高度 $ w = $ width / $ scale ; $ h = $ height / $ scale ; $ x = ( $ this -> info [ 'width' ] - $ w ) / 2 ; $ y = ( $ this -> info [ 'height' ] - $ h ) / 2 ; break ; /* 左上角裁剪 */ case THINKIMAGE_THUMB_NORTHWEST : //计算缩放比例 $ scale = max ( $ width / $ w , $ height / $ h ) ; //设置缩略图的坐标及宽度和高度 $ x = $ y = 0 ; $ w = $ width / $ scale ; $ h = $ height / $ scale ; break ; /* 右下角裁剪 */ case THINKIMAGE_THUMB_SOUTHEAST : //计算缩放比例 $ scale = max ( $ width / $ w , $ height / $ h ) ; //设置缩略图的坐标及宽度和高度 $ w = $ width / $ scale ; $ h = $ height / $ scale ; $ x = $ this -> info [ 'width' ] - $ w ; $ y = $ this -> info [ 'height' ] - $ h ; break ; /* 填充 */ case THINKIMAGE_THUMB_FILLED : //计算缩放比例 if ( $ w < $ width && $ h < $ height ) { $ scale = 1 ; } else { $ scale = min ( $ width / $ w , $ height / $ h ) ; } //设置缩略图的坐标及宽度和高度 $ neww = $ w * $ scale ; $ newh = $ h * $ scale ; $ x = $ this -> info [ 'width' ] - $ w ; $ y = $ this -> info [ 'height' ] - $ h ; $ posx = ( $ width - $ w * $ scale ) / 2 ; $ posy = ( $ height - $ h * $ scale ) / 2 ; do { //创建新图像 $ img = imagecreatetruecolor ( $ width , $ height ) ; // 调整默认颜色 $ color = imagecolorallocate ( $ img , 255 , 255 , 255 ) ; imagefill ( $ img , 0 , 0 , $ color ) ; //裁剪 imagecopyresampled ( $ img , $ this -> im , $ posx , $ posy , $ x , $ y , $ neww , $ newh , $ w , $ h ) ; imagedestroy ( $ this -> im ) ; //销毁原图 $ this -> im = $ img ; } while ( ! empty ( $ this -> gif ) && $ this -> gifNext ( ) ) ; $ this -> info [ 'width' ] = $ width ; $ this -> info [ 'height' ] = $ height ; return $ this ; /* 固定 */ case THINKIMAGE_THUMB_FIXED : $ x = $ y = 0 ; break ; default : throw new Exception ( '不支持的缩略图裁剪类型'); } /* 裁剪图像 */ $ this -> crop ( $ w , $ h , $ x , $ y , $ width , $ height ) ; return $ this ; }
生成缩略图

@param  integer $width  缩略图最大宽度
@param  integer $height 缩略图最大高度
@param int      $type   缩略图裁剪类型

@return $this
@throws Exception
生成缩略图