realitaetsverlust/imagewrapper

PHP 中图像的 OOP 包装器

1.0.1 2021-04-17 13:52 UTC

This package is auto-updated.

Last update: 2024-09-17 21:49:10 UTC


README

PHP 中图像的一个简单包装器

ImageWrapper 是我的一些 PHP 简单 OOP 包装器之一。我的目标是创建一个轻量级、依赖性很小且无需过多解释即可使用的库。我认为我确实成功了。Image wrapper 无依赖项,不是非常复杂,并且非常容易集成到现有或新的 Web 应用程序中。

请注意,此软件仍在开发中。它目前的状态绝对适用于生产环境,但可能会有偶尔的 bug,尤其是在较旧的 GDLb 版本中。如果您发现了一个 bug,请留下一个问题,说明您使用的确切 PHP 版本和 GDLib 版本。

安装

您可以通过 composer 轻松安装 ImageWrapper

composer require realitaetsverlust/imagewrapper

ImageWrapper 可以为您做什么

  • 轻松加载、编辑、转换和输出任何类型的图像
  • 为所有文件类型提供易于使用的 API
  • 无花哨的功能:每个方法映射到一个,有时是两个 PHP 函数。非常直接

例如:您想加载一个名为 test.png 的图像,将其转换为 JPEG 文件并输出

use Realitaetsverlust\Wrapper\ImageTypes\Jpeg;
use Realitaetsverlust\Wrapper\ImageTypes\Png;

$image = new Png('test.png');
$image->convert(new Jpeg());
$image->output('test.jpeg');

太好了,这就是将图像从 PNG 转换为 JPEG 所需的所有内容。如果您喜欢更花哨的方式,您还可以使用 ImageWrapper 提供的方法链

use Realitaetsverlust\Wrapper\ImageTypes\Jpeg;
use Realitaetsverlust\Wrapper\ImageTypes\Png;

$image = new Png('test.png');
$image->convert(new Jpeg())->output('test.jpeg');

几乎每个方法最终都会返回自身,有一些例外。因此,您可以根据需要多次链式调用方法!

此外,如果您不知道您有什么类型的图像,或者您只是不想麻烦,没关系,ImageWrapper 会支持您。有一个 ImageFactory::create() 会自动检查文件类型使用 exif_imagetype() 并返回相应的对象

use Realitaetsverlust\Wrapper\ImageFactory;

$image = ImageFactory::create('test.png'); // Will return a Png Object
$image = ImageFactory::create('test.jpg'); // Will return a Jpeg Object
$image = ImageFactory::create('image_with_no_extension'); // Will return the correct object, whatever it is

ImageWrapper 支持 PHP 所支持的所有图像类型,包括

  • .png
  • .jpeg
  • .gif
  • .webp
  • .bmp
  • .wbmp
  • .xbm
  • .xpm(仅读,不可写)

库元素

ImageBase

ImageBase 是所有图像的基础类。它是一个抽象类,包含所有方法的定义。每个方法映射到一个 PHP 函数,有时两个如果两个方法非常相似。下面是 ImageBase 中方法的完整文档

颜色

Color() 类是 RGB 表示,在整个库中使用。它的主要优点是可以转换 RGB 值以及基于十六进制的字符串(#ff00ff)和 CMYK。

每次函数需要颜色表示时,您应该传递一个 Color 对象。这可以像下面这样

$image->colorize(new \Realitaetsverlust\Wrapper\Color(255, 0, 0));

这将使图像变为红色。然而,正如上面提到的,您也可以添加其他表示颜色的值

$image->colorize(new \Realitaetsverlust\Wrapper\Color("#FF0000")); // Hex
$image->colorize(new \Realitaetsverlust\Wrapper\Color(0, 100, 100, 0)); // CMYK

通常,您不必担心颜色的分配,图片类会自己处理这些。但是,allocateColor() 方法允许您自己分配颜色。

方法文档

public function output(string $destination = null, ?): bool 

输出图像。如果设置了 $destination,则图像将保存到指定路径。如果没有设置,则直接发送为输出。

此方法不在 ImageBase 中,但在子类中。PHP 的输出函数不是非常流畅,这使得编写适用于所有这些函数的函数非常困难。

public function convert(ImageBase $imageType): ImageBase

将图像从一种图像类型转换为另一种类型。请注意,此函数返回一个新的对象,您必须使用它。因此,以下内容将不起作用。

$image = new Png('test.png');
$image->convert(new Jpeg());
$image->output('test.jpeg');

但是,方法链将显然起作用。

public function sendHeaders() : void

此方法以浏览器预期的格式发送标头,如Content-Type: image/png。它们由image_type_to_mime_type()确定。通常,您不需要自己调用此函数,如果方法output()没有获取任何$destination参数,则会自动调用。但是,可能存在您想自己调用函数的情况,例如,在将图像发送给接收者之前设置一些参数。

public function getFileType(): string

返回图像的当前文件类型。这不是由扩展名决定的,而是由exif_imagetype()决定的。

public function getImageSize(): array 

返回图像的文件大小。

public function getMinQuality(): int 

返回输出函数中PHP接受的最低质量。

public function getMaxQuality(): int 

返回输出函数中PHP接受的最高质量。

public function alphaBlending(bool $blendmode): ImageBase

设置图像的混合模式

public function enableAntialias(bool $enabled): ImageBase

激活用于线条和线多边形的快速绘图抗锯齿方法。

public function drawArc(int $cx, int $cy, int $width, int $height, int $start, int $end, Color $color, int $style = null): ImageBase

绘制弧线。如果传递了$style,则弧线将被填充

public function drawChar(bool $drawVertical, int $font, int $x, int $y, string $c, int $color): ImageBase

将字符水平或垂直绘制到图像上。

public function getColoarAt(int $x, int $y): int|false 

获取特定位置的颜色值

public function cutPartial(int $x, int $y, int $width, int $height): ImageBase

从当前图像中剪切一部分并将其复制到新图像中

public function crop(int $x, int $y, int $width, int $height): ImageBase

裁剪图像到给定尺寸

public function drawDashedLine(int $srcX, int $srcY, int $destX, int $destY, Color $color): ImageBase

在图像上绘制线条

public function drawEllipse(int $x, int $y, int $width, int $height, Color $color, bool $fill = false): ImageBase

绘制椭圆。如果传递了$fill,则椭圆将以传递的颜色填充。

public function fill(int $x, int $y, Color $color): ImageBase

在图像上执行洪水填充

public function drawPolygon(array $points, int $numberOfPoints, Color $color, bool $fill = false): ImageBase

绘制多边形。如果传递了$fill,则多边形将以传递的颜色填充。

public function drawRectangle(int $topLeft, int $topRight, int $bottomLeft, int $bottomRight, Color $color, bool $fill = false)

绘制矩形。如果传递了$fill,则矩形将以给定颜色填充。

public function invertColors(): ImageBase

反转图像的颜色(白色到黑色,黑色到白色等)

public function grayscaleImage(): ImageBase

将图像转换为灰度图像。保留Alpha组件。

public function setBrightness(int $brightness): ImageBase

设置给定图像的亮度

public function setContrast(int $contrast): ImageBase

设置给定图像的对比度

public function colorize(Color $color): ImageBase

为图像上色

public function edgedetect(): ImageBase

使用边缘检测突出图像中的边缘。

public function emboss(): ImageBase

图像浮雕

public function blur(bool $useGauss = false): ImageBase

模糊图像。如果传递了$useGauss,则使用高斯模糊而不是选择性模糊

public function sketchImage(): ImageBase

将图像转换为草图外观

public function smooth(int $smoothness): ImageBase

平滑图像

public function pixelate(int $blockSize, bool $advancedPixelationMode = false): ImageBase

像素化图像

public function scatter(int $effectSubstractionLevel, int $effectAdditionLevel, array $onlyApplyTo = []): ImageBase

对图像应用散射效果。

public function gammaCorrection(float $inputGamma, float $outputGamma): ImageBase

伽玛校正方法

public function flip(int $mode): ImageBase

使用给定模式翻转图像。模式是IMG_FLIP_HORIZONTAL、IMG_FLIP_VERTICAL和IMG_FLIP_BOTH

public function setInterlace(bool $interlaceMode): ImageBase

启用/禁用交错

public function isTrueColor(): bool

确定图像是否为真彩色图像

public function allocateColor(Color $color): false|int

分配颜色并将其添加到已分配颜色的列表中。