sbolch/gd-image

使用PHP的GD库进行图像编辑的包。

资助包维护!
sbolch
Ko Fi
请我喝咖啡

安装: 6

依赖: 0

建议者: 0

安全: 0

星标: 3

关注者: 2

分支: 0

公开问题: 0

类型:

v4.0.2 2023-10-02 20:15 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文档根目录,这两个扩展都可以使用此参数。

您可以通过使用可选的 $cacheDir 参数以及您所需的缓存文件夹来在SizerExtension中使用缓存机制。

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

{# 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): 作为百分比的编码质量(可选)