shadesoft/gd-image

该软件包已被废弃且不再维护。作者建议使用 sbolch/gd-image 软件包。

使用PHP的GD库的图像编辑软件包。

v4.0.1 2023-03-13 23:27 UTC

This package is auto-updated.

Last update: 2023-10-02 20:26:07 UTC


README

使用PHP的GD库的图像编辑软件包。

Latest Stable Version StyleCI Total Downloads License

安装

打开命令行,进入您的项目目录并执行以下命令以下载此软件包的最新稳定版本

$ composer require sbolch/gd-image

此命令需要您全局安装Composer,请参阅Composer文档中的安装章节

用法

图像转换器

<?php
  // ...
  use sbolch\GDImage\Converter;
  // ...
  class Demo {
    public function demo() {
      $img = 'path/to/image.png';

      $converter = new Converter();
      $converter
        ->image($img)
        ->toJpg()
        ->target('path/to/converted-image.jpg')
        ->save();
    }
  }

可用函数

  • image ( string | GdImage $image ) : self

    从文件路径或图像资源设置源图像

  • target ( string $path ) : self

    设置目标路径

  • quality ( int quality ) : self

    设置输出质量 - 接受的值是百分比

  • background ( int red, int green, int blue ) : self

    设置背景颜色而不是透明度 - 接受的值是介于0到255之间的整数

  • toAvif() | toBmp() | toGif() | toJpg() | toJpeg() | toPng() | toWebp() : self

    设置输出格式

  • save() : string

    保存生成的图像

  • output() : void

    将图像打印到PHP输出

图像尺寸调整器

<?php
  // ...
  use sbolch\GDImage\Sizer;
  // ...
  class Demo {
    public function demo() {
      $img = 'path/to/image.jpg';

      $sizer = new Sizer();
      $sizer
        ->image($img)
        ->thumbnail(400, 300)
        ->save();
    }
  }

可用函数

在转换器中查看可用函数,所有这些函数在这里也可用

  • image ( string | GdImage $image ) : self

    从文件路径或图像资源设置源图像

  • instance(): GdImage

    返回当前图像资源

  • widen ( int $width ) : self

    设置图像的宽度,同时保持其比例

  • heighten ( int $height ) : self

    设置图像的高度,同时保持其比例

  • maximize ( int $width, int $height ) : self

    通过最大尺寸的维度最大化图像大小,同时保持其比例

  • crop ( int $width, int $height [, int $x [, int $y]] ) : self

    从给定位置裁剪图像到指定尺寸

  • thumbnail ( int $width, int $height ) : self

    通过裁剪图像的较短维度(居中裁剪)来创建缩略图

缓存图像尺寸调整器

与图像尺寸调整器相同,但它使用缓存。

<?php
  // ...
  use sbolch\GDImage\CachedSizer;
  // ...
  class Demo {
    public function demo() {
      $img = 'path/to/image.jpg';

      $sizer = new CachedSizer();
      $sizer
        ->image($img)
        ->thumbnail(400, 300)
        ->save();
    }
  }

Twig集成

如果您使用Twig,可以包含以下扩展

  • sbolch\GDImage\Twig\ConverterExtension([string $docroot])
  • sbolch\GDImage\Twig\SizerExtension([string $docroot [, string $cacheDir]])

您可以使用可选的$docroot参数覆盖默认PHP文档根目录,这两个扩展都支持。

您可以使用SizerExtension中的缓存机制,通过使用可选的$cacheDir参数和您选择的缓存文件夹。

然后您可以使用以下过滤器(问号表示可选参数)

{# Converting image and returning its new path via sbolch\GDImage\Twig\ConverterExtension #}
{{ 'path/to/image'|convert_to_avif(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_bmp(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_gif(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_jpg(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_jpeg(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_png(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_webp(?targetPath, ?quality) }}

{# Resizing image and returning its new path via sbolch\GDImage\Twig\SizerExtension #}
{{ 'path/to/image'|widen(width, ?outputFormat, ?targetPath) }}
{{ 'path/to/image'|heighten(height, ?outputFormat, ?targetPath) }}
{{ 'path/to/image'|maximize(width, height, ?outputFormat, ?targetPath) }}
{{ 'path/to/image'|thumbnail(width, height, ?outputFormat, ?targetPath) }}

Phar模式(目前只有转换器可用)

您可以从发布页面下载phar文件,并按以下方式使用

php converter.phar -i /path/to/image -f jpg
  • -i (--input) : 输入文件
  • -o (--output) : 输出文件(可选 - 您必须使用-o或-f)
  • -f (--format) : 输出格式(可选 - 您必须使用-o或-f)
  • -q (--quality): 编码质量作为百分比(可选)