midnight/image

一个图像编辑库

v0.1.1 2013-10-03 08:32 UTC

This package is auto-updated.

Last update: 2024-09-20 00:00:50 UTC


README

一个用于PHP的轻量级图像编辑库

安装

此库作为Composer包提供

./composer.phar require midnight/image

用法

打开一个图像

$image = \Midnight\Image\Image::open('path/to/image.jpg');

应用一个过滤器

$image->fit(array('width' => 150, 'height' => 200));

应用过滤器后获取新的图像

$image->getFile();

包含的过滤器

此库包含一些预定义的过滤器。可以通过调用图像对象的方法来访问这些过滤器。所有过滤器都接受一个选项数组作为它们的唯一参数。例如

$options = array('foo' => true, 'bar' => 42);
$filtered_image = $image->filterName($options);

fit

将图像按比例放大或缩小以适应由选项 widthheight 定义的矩形。它相当于CSS的 background-size: contain 属性或Adobe InDesign的“按比例适应”选项。

// $image has a size of 1500 by 2000 pixels
$thumbnail = $image->fit(array('width' => 200, 'height' => 200));
// $thumbnail is 150 x 200 pixels 

fill

使用 fill 过滤器填充由选项 widthheight 定义的矩形。它相当于CSS的 background-size: cover 属性或Adobe InDesign的“按比例填充”选项。它很可能会裁剪图像的一部分。

// $image has a size of 1500 by 2000 pixels
$thumbnail = $image->fill(array('width' => 200, 'height' => 200));
// $thumbnail is 200 x 200 pixels, with cropped areas at the top and bottom

brighten

使图像变亮。唯一的选项是 amount,它接受一个从 -255(实际上会使图像变暗)到 255 的整数。

$brightened = $image->brighten(array('amount' => 50));

desaturate

使图像去饱和。唯一的选项是 amount,它接受一个从 -100(实际上会饱和图像)到 100 的整数。

$desaturated = $image->desaturate(array('amount' => 25));

colorcast

将颜色滤镜应用于图像。可用选项是 color,它接受一个 Midnight\Color\Color(实际上是 ColorInterface)实例,以及 amount,它接受一个介于 0 和 1 之间的值。

// Apply a red color cast.
$red = new \Midnight\Color\Color(255, 0, 0);
$colorcasted = $image->colorcast(array('color' => $red, 'amount' => .2));

noise

向图像添加噪声。唯一可用的选项是 amount

$noisy = $image->noise(array('amount' => .1));

添加自定义过滤器

您可以轻松添加自定义过滤器

class MyCustomFilter extends \Midnight\Image\Filter\AbstractGdFilter
{
    public function filter($image)
    {
        parent::filter($image);

        $cache = $this->getCache();
        if ($cache->exists($this)) {
            return Image::open($cache->getPath($this));
        }

        $im = $this->getGdImage();

        // Do stuff to GD image resource $im

        $image = $this->save($im);

        return $image;
    }
}
$plugin_manager = \Midnight\Image\ImagePluginManager::getInstance();
$plugin_manager->setService('myCustomFilter', 'MyCustomFilter');

// Use the filter
$filtered = \Midnight\Image\Image::open('file.jpg')->myCustomFilter();