src/thinkextend/image/driver/Imagick.php
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALE)
    {
        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;
                }

                //计算缩放比例
                $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;
                $posx = ($width - $w * $scale) / 2;
                $posy = ($height - $h * $scale) / 2;

                //创建一张新图像
                $newimg = new Imagick();
                $newimg->newImage($width, $height, 'white', $this->info['type']);

                if ('gif' == $this->info['type']) {
                    $imgs = $this->im->coalesceImages();
                    $img  = new Imagick();
                    $this->im->destroy(); //销毁原图

                    //循环填充每一帧
                    do {
                        //填充图像
                        $image = $this->_fill($newimg, $posx, $posy, $neww, $newh, $imgs);

                        $img->addImage($image);
                        $img->setImageDelay($imgs->getImageDelay());
                        $img->setImagePage($width, $height, 0, 0);

                        $image->destroy(); //销毁零时图片

                    } while ($imgs->nextImage());

                    //压缩图片
                    $this->im->destroy();
                    $this->im = $img->deconstructImages();
                    $imgs->destroy(); //销毁零时图片
                    $img->destroy(); //销毁零时图片

                } else {
                    //填充图像
                    $img = $this->_fill($newimg, $posx, $posy, $neww, $newh);
                    //销毁原图
                    $this->im->destroy();
                    $this->im = $img;
                }

                //设置新图像属性
                $this->info['width']  = $width;
                $this->info['height'] = $height;
                return;

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

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

        /* 裁剪图像 */
        $this->crop($w, $h, $x, $y, $width, $height);
    }
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALE)
    {
        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;
                }

                //计算缩放比例
                $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;
                $posx = ($width - $w * $scale) / 2;
                $posy = ($height - $h * $scale) / 2;

                //创建一张新图像
                $newimg = new Imagick();
                $newimg->newImage($width, $height, 'white', $this->info['type']);

                if ('gif' == $this->info['type']) {
                    $imgs = $this->im->coalesceImages();
                    $img  = new Imagick();
                    $this->im->destroy(); //销毁原图

                    //循环填充每一帧
                    do {
                        //填充图像
                        $image = $this->_fill($newimg, $posx, $posy, $neww, $newh, $imgs);

                        $img->addImage($image);
                        $img->setImageDelay($imgs->getImageDelay());
                        $img->setImagePage($width, $height, 0, 0);

                        $image->destroy(); //销毁零时图片

                    } while ($imgs->nextImage());

                    //压缩图片
                    $this->im->destroy();
                    $this->im = $img->deconstructImages();
                    $imgs->destroy(); //销毁零时图片
                    $img->destroy(); //销毁零时图片

                } else {
                    //填充图像
                    $img = $this->_fill($newimg, $posx, $posy, $neww, $newh);
                    //销毁原图
                    $this->im->destroy();
                    $this->im = $img;
                }

                //设置新图像属性
                $this->info['width']  = $width;
                $this->info['height'] = $height;
                return;

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

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

        /* 裁剪图像 */
        $this->crop($w, $h, $x, $y, $width, $height);
    }
public function thumb ( $ width , $ height , $ type = THINKIMAGE_THUMB_SCALE ) { 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 ; } //计算缩放比例 $ 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 ; $ posx = ( $ width - $ w * $ scale ) / 2 ; $ posy = ( $ height - $ h * $ scale ) / 2 ; //创建一张新图像 $ newimg = new Imagick ( ) ; $ newimg -> newImage ( $ width , $ height , 'white' , $ this -> info [ 'type' ] ) ; if ( 'gif' == $ this -> info [ 'type' ] ) { $ imgs = $ this -> im -> coalesceImages ( ) ; $ img = new Imagick ( ) ; $ this -> im -> destroy ( ) ; //销毁原图 //循环填充每一帧 do { //填充图像 $ image = $ this -> _fill ( $ newimg , $ posx , $ posy , $ neww , $ newh , $ imgs ) ; $ img -> addImage ( $ image ) ; $ img -> setImageDelay ( $ imgs -> getImageDelay ( ) ) ; $ img -> setImagePage ( $ width , $ height , 0 , 0 ) ; $ image -> destroy ( ) ; //销毁零时图片 } while ( $ imgs -> nextImage ( ) ) ; //压缩图片 $ this -> im -> destroy ( ) ; $ this -> im = $ img -> deconstructImages ( ) ; $ imgs -> destroy ( ) ; //销毁零时图片 $ img -> destroy ( ) ; //销毁零时图片 } else { //填充图像 $ img = $ this -> _fill ( $ newimg , $ posx , $ posy , $ neww , $ newh ) ; //销毁原图 $ this -> im -> destroy ( ) ; $ this -> im = $ img ; } //设置新图像属性 $ this -> info [ 'width' ] = $ width ; $ this -> info [ 'height' ] = $ height ; return ; /* 固定 */ case THINKIMAGE_THUMB_FIXED : $ x = $ y = 0 ; break ; default : throw new \ Exception ( '不支持的缩略图裁剪类型'); } /* 裁剪图像 */ $ this -> crop ( $ w , $ h , $ x , $ y , $ width , $ height ) ; }
生成缩略图

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