PHP下使用强大的imagick轻巧生成组合缩略图

php中文网
发布: 2016-06-13 12:28:40
原创
1167人浏览过

PHP下使用强大的imagick轻松生成组合缩略图

project: blogtarget: use-imagick-to-composite-images-thumbnail.mddate: 2016-02-19status: publishtags:    - php    - imagick    - thumbnailcategories:    - php
登录后复制

这里说的imagickimagemagick 在php下的扩展。使用pecl安装起来那叫一个轻松简单 —— 一条命令就搞定:

sudo pecl install imagick
登录后复制

(扩展装好后还是要在php.ini中加上extension=imagick.so,然后记得重启apachephp-fpm服务。)

最近有个需求是要把多张图片组合起来生成缩略图,刚好用用这个强大的imagick扩展。
这个需求是要这样生成缩略图:

  1. 如果有1张图片,就直接生成这张图片的缩略图;
  2. 如果有2张图片,则一张在左边一张在右边,各一半;
  3. 如果有3张图片,则两张左边平均分配,一张独占右边;
  4. 如果有4张图片,则像田字格一样平均分配空间;
  5. 更多张图片,则只取前4张,按田字格方式生成缩略图。

这规则还真不少,不过还不算太过复杂,很快搞出来了:

namespace \clarence\thumbnail;class Thumbnail extends \Imagick{    /**     * @param array $images     * @param int $width     * @param int $height     * @return static     * @throws ThumbnailException     */    public static function createFromImages($images, $width, $height){        if (empty($images)){            throw new ThumbnailException("No images!");        }        $thumbnail = new static();        $thumbnail->newImage($width, $height, 'white', 'jpg');        $thumbnail->compositeImages($images);        return $thumbnail;    }    public function compositeImages($images){        $imagesKeys = array_keys($images);        $compositeConfig = $this->calcCompositeImagesPosAndSize($images);        foreach ($compositeConfig as $index => $cfg){            $imgKey = $imagesKeys[$index];            $img = new \Imagick($images[$imgKey]);            $img = $this->makeCompositeThumbnail($img, $cfg);            $this->compositeImage($img, self::COMPOSITE_OVER, $cfg['to']['x'], $cfg['to']['y']);        }    }    protected function makeCompositeThumbnail(\Imagick $img, $cfg){        $img->cropThumbnailImage($cfg['size']['width'], $cfg['size']['height']);        return $img;    }    protected function calcCompositeImagesPosAndSize($images){        $width = $this->getImageWidth();        $height = $this->getImageHeight();        switch(count($images)){            case 0:                throw new ThumbnailException("No images!");            case 1:                // | 0 |                return [                    0 => [                        'to' => [ 'x' => 0, 'y' => 0 ],                        'size' => [                            'width' => $width,                            'height' => $height,                        ]                    ]                ];            case 2:                // | 0 | 1 |                return [                    0 => [                        'to' => [ 'x' => 0, 'y' => 0 ],                        'size' => [                            'width' => $width / 2,                            'height' => $height,                        ]                    ],                    1 => [                        'to' => [ 'x' => $width / 2, 'y' => 0],                        'size' => [                            'width' => $width / 2,                            'height' => $height,                        ]                    ]                ];            case 3:                // | 0 | 1 |                // | 2 |   |                return [                    0 => [                        'to' => [ 'x' => 0, 'y' => 0 ],                        'size' => [                            'width' => $width / 2,                            'height' => $height / 2,                        ]                    ],                    1 => [                        'to' => [ 'x' => $width / 2, 'y' => 0],                        'size' => [                            'width' => $width / 2,                            'height' => $height,                        ]                    ],                    2 => [                        'to' => [ 'x' => 0, 'y' => $height / 2 ],                        'size' => [                            'width' => $width / 2,                            'height' => $height / 2,                        ]                    ],                ];            default:                // >= 4:                // | 0 | 1 |                // | 2 | 3 |                return [                    0 => [                        'to' => [ 'x' => 0, 'y' => 0 ],                        'size' => [                            'width' => $width / 2,                            'height' => $height / 2,                        ]                    ],                    1 => [                        'to' => [ 'x' => $width / 2, 'y' => 0],                        'size' => [                            'width' => $width / 2,                            'height' => $height / 2,                        ]                    ],                    2 => [                        'to' => [ 'x' => 0, 'y' => $height / 2 ],                        'size' => [                            'width' => $width / 2,                            'height' => $height / 2,                        ]                    ],                    3 => [                        'to' => [ 'x' => $width / 2, 'y' => $height / 2],                        'size' => [                            'width' => $width / 2,                            'height' => $height / 2,                        ]                    ],                ];        }    }}
登录后复制

用个试试:

立即学习PHP免费学习笔记(深入)”;

Avactis购物车
Avactis购物车

Avactis是一个强大的PHP在线购物系统拥有多个版本包括开源版本。它具备一个在线购物系统所需要的所有功能从产品到会员管理,订单和营销。可以无限分类和为产品指定任务数量的图片(支持自动生成缩略图)。使用自定义字段功能,让你可以更好地定义一个产品。该系统提供以非常灵活的方式来创建任意类型的促销活动如设置折扣代码,基于价格的折扣或基于数量的折扣等。

Avactis购物车 0
查看详情 Avactis购物车
$thumbnail = \clarence\thumbnail\Thumbnail::createFromImages($srcImages, 240, 320);$thumbnail->writeImage($outputDir."/example.jpg");
登录后复制

效果立马出来了:

赞一个~

(详细代码见 http://github.com/clarence-pan/thumbnail)

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号