greg-md/php-imagix

使用Intervention Image实时将图片保存为不同格式。

dev-master 2019-07-24 08:31 UTC

This package is auto-updated.

Last update: 2024-09-19 22:54:13 UTC


README

StyleCI Build Status Total Downloads Latest Stable Version Latest Unstable Version License

使用Intervention Image实时将图片保存为不同格式。

当你的应用UI更改时,你不再关心从源生成新图片。你应该做的只是添加新格式或更改现有格式,应用程序将自动处理。

目录

要求

  • PHP版本 ^5.6 || ^7.0

工作原理

首先,你必须初始化Imagix。

可选地,你可以为它创建一个URL装饰器。这有助于将图像URL重写为/从HTTP服务器(Apache/Nginx)。

class ImagixDecorator implements ImageDecoratorStrategy
{
    // Will use /imagix public path for generated images.
    private $uri = '/imagix';
    
    public function output($url)
    {
        return $this->nginxUri . $url;
    }

    public function input($url)
    {
        return \Greg\Support\Str::shift($url, $this->uri);
    }
}

$sourcePath = __DIR__ . '/img';

$destinationPath = __DIR__ . '/imagix';

$decorator = new ImagixDecorator();

$intervention = new Intervention\Image\ImageManager();

$imagix = new \Greg\Imagix\Imagix($intervention, $sourcePath, $destinationPath, $decorator);

接下来,创建图像格式。

$imagix->format('square', function (\Intervention\Image\Image $image) {
    $image->resize(600, 600, function (\Intervention\Image\Constraint $constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    });
});

现在,是时候在你的应用程序中使用它了。

它将以以下格式生成一个URL:<image_path>/<image_name>@<format>@<last_modified>.<image_extension>

// result: /imagix/pictures/picture@square@129648839.jpg
$imageUrl = $imagix->url('/pictures/picture.jpg', 'square');

echo '<img src="' . $imageUrl . '" width="600" height="600" alt="Picture" />';

要查看结果,你必须配置你的http服务器

Nginx

# Imagix
location ~* ^/imagix/.+ {
    # If images doesn't exists, send to PHP to create it.
    if (!-f $document_root$uri) {
        rewrite .+ /imagix.php last;
    }

    expires max;
    add_header Pragma public;
    add_header Cache-Control "public";
    add_header Vary "Accept-Encoding";
}

imagix.php中,你将在/imagix路径中调度尚未生成的新文件。

$imagix->send($_SERVER['REQUEST_URI']);

方法

魔术方法:

__construct

初始化管理器。

__construct(ImageManager $manager, string $sourcePath, string $destinationPath, ImageDecoratorStrategy $decorator = null)

$manager - Intervention图像管理器;
$sourcePath - 源路径;
$destinationPath - 目标路径;
$decorator - 装饰器。

示例

class ImagixDecorator implements ImageDecoratorStrategy
{
    // Will use /imagix public path for generated images.
    private $uri = '/imagix';
    
    public function output($url)
    {
        return $this->nginxUri . $url;
    }

    public function input($url)
    {
        return \Greg\Support\Str::shift($url, $this->uri);
    }
}

$sourcePath = __DIR__ . '/img';

$destinationPath = __DIR__ . '/imagix';

$decorator = new ImagixDecorator();

$intervention = new Intervention\Image\ImageManager();

$imagix = new \Greg\Imagix\ImagixManager($intervention, $sourcePath, $destinationPath, $decorator);

支持的方法:

  • format - 注册图像格式;
  • url - 获取格式化图像URL;
  • source - 获取格式化图像URL的源URL;
  • effective - 获取格式化图像URL的有效URL;
  • compile - 编译格式化图像URL;
  • send - 发送格式化图像URL的图像;
  • unlink - 删除图像的格式化版本;
  • remove - 删除格式化图像。

format

注册图像格式。

format(string $name, callable(\Intervention\Image\Image $image): void $callable): $this

$name - 格式名称;
$callable - 一个用于格式化图像的可调用函数;
    $image - 图像。

示例

$imagix->format('square', function (\Intervention\Image\Image $image) {
    $image->resize(600, 600, function (\Intervention\Image\Constraint $constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    });
});

url

获取格式化图像URL。

url(string $source, string $format): string

$source - 原始图像URL;
$format - 格式名称。

示例

$imagix->url('/pictures/picture.jpg', 'square'); // result: /pictures/picture@square@129648839.jpg

source

获取格式化图像URL的源URL。

source(string $destination): string

$destination - 格式化图像URL。

示例

$imagix->source('/pictures/picture@square@129648839.jpg'); // result: /pictures/picture.jpg

effective

获取格式化图像URL的有效URL。

每次图像更改时,其有效URL也会更改。因此,如果你有一个旧的URL,你将得到一个新的。

当你在某处存储格式化URL时,这很有用。

默认情况下,send方法将返回一个301重定向到新URL。

effective(string $destination): string

$destination - 格式化图像URL。

示例

$imagix->effective('/pictures/picture@square@129642346.jpg'); // result: /pictures/picture@square@129648839.jpg

compile

编译格式化图像URL。将返回图像的实际路径。

compile(string $destination): string

$destination - 格式化图像URL。

示例

$imagix->compile('/pictures/picture@square@129648839.jpg'); // result: /path/to/pictures/picture@square@129648839.jpg

send

发送格式化图像的URL的图像。

send(string $destination): $this

$destination - 格式化图像URL。

示例

$imagix->send('/pictures/picture@square@129648839.jpg');

解除链接

删除图像的格式化版本。

unlink(string $source, string $format = null, int $lifetime = 0): $this

$source - 格式化图像URL;
$format - 格式名称;
$lifetime - 如果设置,则仅删除已过期的文件。

示例

$imagix->unlink('/pictures/picture.jpg');

删除

删除格式化图像。

remove(string $format = null, int $lifetime = 0): $this

$format - 格式名称;
$lifetime - 如果设置,则仅删除已过期的文件。

示例

$imagix->remove(); // Will remove all formatted images.

$imagix->remove('square'); // Will remove only square images.

许可

MIT © 格里戈里·杜卡

长引用

I fear not the man who has practiced 10,000 programming languages once, but I fear the man who has practiced one programming language 10,000 times. #horrorsquad