greg-md/php-static-image

此包已被废弃,不再维护。作者建议使用greg-md/php-imagix包。

使用 Intervention Image 在实时中以不同格式保存图像。

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

This package is auto-updated.

Last update: 2023-04-19 19:20:59 UTC


README

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

使用 Intervention Image 在实时中以不同格式保存图像。

当您的应用程序UI更改时,您不再关心从源生成新图像。您唯一需要做的就是添加新格式或更改现有格式,应用程序将自动处理。

目录

要求

  • PHP版本 ^5.6 || ^7.0

工作原理

首先,您必须初始化 Imagix。

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

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

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

source(string $destination): string

$destination - 格式化图片URL。

示例

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

有效

获取格式化图片URL的有效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

编译

编译格式化图片URL。将返回图片的真实路径。

compile(string $destination): string

$destination - 格式化图片URL。

示例

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

发送

发送格式化图片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 © Grigorii Duca

超级引用

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