talesoft/phim

PHP 图像和颜色处理库

1.0.6 2020-10-20 10:56 UTC

This package is auto-updated.

Last update: 2024-09-20 18:48:34 UTC


README

PHP 图像处理

Phim 是什么?

Phim 是一个 PHP 图像处理库。它包含一个强大的颜色处理库和针对多个适配器(包括 GD 和 Imagick)的图像处理抽象。

此库仍在开发中。如果您有兴趣提供帮助,请发送电子邮件至 torben@talesoft.codes

安装

通过 Composer 安装。

$ composer require talesoft/phim

已完成什么,还需要做什么?

  • 颜色处理
    • RGB
    • RGBA
    • HSL
    • HSLA
    • HSV
    • HSVA
    • CIE XYZ
    • CIE L*a*b*
    • CIEDE2000 颜色比较
  • 几何
    • 基本几何形状
    • 每个形状的几何数学工具
  • 图像处理
    • 抽象图像表示
      • 线条/直线路径
      • 多边形/形状
      • 曲线/圆/椭圆/弧线
    • 创建图像
      • 图像和形状工厂
      • 转换
      • 颜色过滤器(作用于 ColorInterface
      • 点过滤器(作用于 PointInterface
    • 渲染适配器
      • GD
        • 基本渲染
        • 完全兼容的渲染
      • Imagick
        • 基本渲染
        • 完全兼容的渲染
      • Phim(库内部,自有渲染)
        • 基本渲染
        • 完全兼容的渲染
    • 图像文件解析
      • 解析 PNG
      • 解析 JPG
      • 解析 GIF
      • 解析 BMP
      • 解析 WebP
      • 解析动画 GIF
      • 解析 ICO
      • 解析 SVG
      • 解析 PDF
      • 解析 PSD
      • 解析 AI(可能?)
      • 解析 EPS
      • 解析 GhostScript

我可以在生产中使用它吗?

您已经可以在生产中使用颜色处理功能,它经过测试且稳定。其他一切都非常实验性,目前不应在生产环境中使用。

API

颜色解析

Phim 可以处理许多不同的颜色格式,并在非常详细级别上修改它们。

Phim 提供的任何颜色都将为类型 Phim\ColorInterface。它们通常有一个定义颜色空间的高级类。

Phim 被设计成您无需关心颜色的颜色空间。如果您需要修改颜色,请使用其中一个 to{Space}-函数。

一切从 Color::get() 函数开始,它将接受混合参数并提供一个有效的 ColorInterface 类型颜色。

您可以自由传递颜色名称、整数值、十六进制字符串和函数字面量。

use Phim\Color;

//By name
Color::get('red'); //RgbColor(255,0,0)
Color::get(Color::MAROON); //RgbColor(128, 0, 0)


//By hex string
Color::get('#00ff00'); //RgbColor(0, 255, 0)
Color::get('#00f'); //RgbColor(0, 0, 255)
Color::get('#000a'); //RgbaColor(0, 0, 0, 0.1)


//By integer
Color::get(0x00ff00); //RgbColor(0, 255, 0)
Color::get(0x00ff00ff); //RgbaColor(0, 255, 0, 1.0)


//By function literals
Color::get('rgb(14, 24, 100)'); //RgbColor(14, 24, 100)
Color::get('hsl(120, .5, 1.0)'); //HslColor(120, 0.5, 1.0)
Color::get('lab(50, 40, 22)'); //LabColor(50, 40, 22)


//Percent-values are allowed and will scale automatically
Color::get('rgba(50%, 100, 20%, 50%)'); //RgbaColor(127.5, 100, 51, 0.5)
Color::get('hsl(50%, 50%, 1.0)'); //HslColor(180, 0.5, 1.0)


//You can also just create them manually (better performance)

use Phim\Color\RgbColor;
use Phim\Color\HslaColor;

$myRgbColor = new RgbColor(0, 100, 0);
$myHslaColor = new HslaColor(120, .5, .1, 1.0);

颜色转换

Phim 中的每个颜色都可以立即转换为任何其他颜色空间。

$color->toHsl()->getLightness();

$color->toRgb()->getGreen();

$color->toLab()->getL();

以下是一个支持的颜色空间及其相应值获取器和设置器的列表

  • RgbColor (toRgb())
    • getRed() | setRed($red)
    • getGreen() | setGreen($green)
    • getBlue() | setBlue($blue)
    • toAlpha() -> RgbaColor
  • RgbaColor (toRgba())
    • getRed() | setRed($red)
    • getGreen() | setGreen($green)
    • getBlue() | setBlue($blue)
    • getAlpha() | setAlpha($alpha)
    • toOpaque() -> RgbColor
  • HslColor (toHsl())
    • getHue() | setHue($hue)
    • getSaturation() | setSaturation($saturation)
    • getLightness() | setLightness($lightness)
    • toAlpha() -> HslaColor
  • HslaColor (getHsla())
    • getHue() | setHue($hue)
    • getSaturation() | setSaturation($saturation)
    • getLightness() | setLightness($lightness)
    • getAlpha() | setAlpha($alpha)
    • toOpaque() -> HslColor
  • HsvColor (toHsv())
    • getHue() | setHue($hue)
    • getSaturation() | setSaturation($saturation)
    • getValue() | setValue($value)
    • toAlpha() -> HsvaColor
  • HsvaColor (toHsva())
    • getHue() | setHue($hue)
    • getSaturation() | setSaturation($saturation)
    • getLightness() | setLightness($lightness)
    • getAlpha() | setAlpha($alpha)
    • toOpaque() -> HsvColor
  • XyzColor (toXyz())
    • getX() | setX($x)
    • getY() | setY($y)
    • getZ() | setZ($z)
    • toAlpha() -> RgbaColor
  • CIE L*a*b* (toLab())
    • getL() | setL($l)
    • getA() | setA($a)
    • getB() | setB($b)
    • toAlpha() -> RgbaColor

在这个系统中,您可以通过一个非常表达性的API轻松地操作颜色。让我们快速看看。

$lightTurquoise = Color::get('blue')
    ->toRgb()
        ->setGreen(50) //Mix some green in
    ->toHsl()
        ->setLightness(.6) //Make it lighter
    ->toAlpha()
        ->setAlpha(.4) //Add 40% transparency
    ->toRgba(); //Make sure it's an RGBA color in the end
    
echo $lightTurquoise; //Will print `rgba(50,90,255,0.4)`

现在我们将详细探讨这些可能性。

颜色修改

颜色修改是通过将其转换为特定的颜色空间并修改其值来完成的。例如,HSL颜色空间非常适合减少颜色的亮度或增加颜色的饱和度。

Phim\Color类将缩短大多数操作。在您使用Color类静态方法时,不需要将颜色转换为任何空间,它将自动将其转换为操作所需的任何空间。

请注意,这些操作几乎总会改变您颜色的返回类型。根据您的应用程序要求,在所需的格式之间来回转换,双重转换具有非常低的内存配置文件。

use Phim\Color;

//Lightening and darkening (Added, not multiplied)
Color::lighten($color, .4);
Color::darken($color, .2);

//Get the complementary color (180° rotated hue)
Color::complement($color);

//Convert to grayscale
Color::grayscale($color);

//Fade in or out (Added, not multiplied)
Color::fade($color, .1);
Color::fade($color, -.1);

//Mix two colors
Color::mix($color, $mixColor);

//Inverse a color
Color::inverse($color);

//Saturate/Desaturate
Color::saturate($color, .4);
Color::desaturate($color, .2);

颜色信息

Color静态类还包含一些有用的工具,可以帮助您接收传递给它们的颜色信息。

//Get the hue range the color resides in (basically, "what base color is this color"?)
//Supported hue ranges are: RED, YELLOW, GREEN, CYAN, BLUE and MAGENTA.
Color::getColorHueRange(Color::get('rgba(80, 0, 0)')); //Color::HUE_RANGE_RED

//Gets the hexadecimal representation of a color.
//Notice that it will turn an Alpha-color into a 4-channel hexadecimal string, because it can also parse them.
//If you want to use the hex value in CSS or HTML, use `toRgb()` on the color before to strip the alpha channel
Color::toHexString($color);

//Gets the integer representation of a color (including support for alpha channel)
Color::toInt($color);

//Get the name of a color. Many colors don't have names, it will return a hex-representation in this case
Color::toName($color);

颜色比较

通过实现CIE XYZ、CIE L*a*b*和CIEDE2000标准进行颜色比较,您可以轻松地通过视觉比较颜色。

$red = Color::get('red'); //rgb(255, 0, 0)

Color::getDifference($red, Color::get('blue')); //52.878674140461

Color::getDifference($red, Color::get('maroon')); //25.857031489394

Color::getDifference($red, Color::get('rgb(250, 0, 0)')); //1.0466353226581

要使用容差比较颜色,您可以使用equals方法。

$red = Color::get('red');

foreach ($otherColors as $otherColor) {

    //Using a tolerance of 2, so the maximum difference between the colors is 2
    if (Color::equals($red, $otherColor, 2))
        echo "Well, this is almost the same color!";
}

如果还不够,让我们继续!Phim还有更多酷炫的功能。

颜色调色板

基本上是一个小型辅助类,用于将颜色放在一起,就像一个普通的数组。传入的颜色将被自动转换。

可以通过将第二个参数传递给Palette来限制最大大小。

use Phim\Color\Palette;

$palette = new Palette([
    'red',
    'green',
    'blue',
    'yellow',
    'white',
    'black'
]);

$palette[] = 'rgb(120, 35, 56)';

$palette[0]; //RgbColor(255, 0, 0)
$palette[6]; //RgbColor(120, 35, 56)

您可以使用Palette类的静态方法来操作调色板。

合并调色板

use Phim\Color\Palette;

$palette = Palette::merge($somePalette, $someOtherPalette);

过滤调色板

use Phim\Color\Palette;
use Phim\ColorInterface;

//Filter all colors that have a lightness below .4
$palette = Palette::filter(function(ColorInterface $color) {
    
    return $color->getHsl()->getLightness() >= .4;
});

预定义过滤器

基于色调范围过滤所有颜色。您将只获得给定色调范围内的颜色。

use Phim\Color\Palette;

//Would only yield colors that have a red-ish hue
$palette = Palette::filterByHueRange($palette, Color::HUE_RANGE_RED);

过滤出看起来相似的颜色的颜色(使用CIEDE2000进行颜色比较)

use Phim\Color\Palette;

//Colors would need to have a difference of at least 4 to all other colors
//in the palette in order to be put in the result collection.
$palette = Palette::filterSimilarColors($palette, 4);

颜色方案

方案是预定义的、生成的调色板。它们从基础颜色中生成一系列相关颜色。有关更多信息,您可以阅读这篇文章。这也是我从中提取以下图片的地方。请注意,任何方案始终也是一个像上面那样的调色板。

请注意,任何方案总是像上面那样是一个调色板。

Phim可以为您生成以下方案

具有固定大小的方案

互补方案

Complementary Scheme

use Phim\Color\Scheme\ComplementaryScheme;

$colors = new ComplementaryScheme($baseColor); //2 colors

类似色方案

Analogous Scheme

use Phim\Color\Scheme\AnalogousScheme;

$colors = new AnalogousScheme($baseColor); //3 colors

三色方案

Triadic Scheme

use Phim\Color\Scheme\TriadicScheme;

$colors = new TriadicScheme($baseColor); //3 colors

分割互补方案

Split-Complementary Scheme

use Phim\Color\Scheme\SplitComplementaryScheme;

$colors = new SplitComplementaryScheme($baseColor); //3 colors

四色方案

Tetradic Scheme

use Phim\Color\Scheme\TetradicScheme;

$colors = new TetradicScheme($baseColor); //4 colors

正方形方案

Square Scheme

use Phim\Color\Scheme\SquareScheme;

$colors = new SquareScheme($baseColor); //4 colors

命名单色方案

生成颜色的3种色调和3种色调,您可以通过方法访问它们。第二个参数是step,它定义了每个颜色之间的加深/变浅量。

use Phim\Color\Scheme\NamedMonochromaticScheme;

$colors = new NamedMonochromaticScheme($baseColor, .3); //7 colors

$colors->getDarkestShade();
$colors->getDarkerShader();
$colors->getDarkShade();
$colors->getBaseColor();
$colors->getLightTint();
$colors->getLighterTint();
$colors->getLightestTint();

生成的方案(动态大小)

这些方案接受amountstepamount指定要生成的总颜色数。指定5将产生包含5种颜色(包括基础颜色,主要是)的调色板。

色调旋转方案

添加色调圆圈中的相邻颜色。

下面的代码将生成5种颜色,基础颜色和4种进一步的颜色,每种颜色在色调上旋转+5°。

use Phim\Color\Scheme\HueRotationScheme;

$colors = new HueRotationScheme($baseColor, 5, 5); //5 colors, +5° hue per color

色调方案

生成颜色的较浅色调。

use Phim\Color\Scheme\TintScheme;

$colors = new TintScheme($baseColor, 6, .3); //6 colors, +.3 lightness per color

阴影方案

生成颜色的较暗阴影。

use Phim\Color\Scheme\ShadeScheme;

$colors = new ShadeScheme($baseColor, 6, .3); //6 colors, -.3 lightness per color

色调方案

生成颜色较少饱和度的色调。

use Phim\Color\Scheme\ToneScheme;

$colors = new ToneScheme($baseColor, 6, .3); //6 colors, -.3 saturation per color

如果您不确定方案将如何结束,您始终可以使用以下方式来打印整个调色板的可视表示。

Palette::toHtml($myPalette);

这将打印一个小表格,包含调色板中的所有颜色以及有关颜色的基本信息。

要自己制作方案,您有很多可能性,最简单的一种是扩展Phim\Color\SchemeBasePhim\Color\Scheme\ContinousSchemeBase

<?php

namespace Phim\Color\Scheme;

use Phim\Color;
use Phim\ColorInterface;

class MyCustomScheme extends Color\SchemeBase
{

    protected function generate(ColorInterface $baseColor)
    {

    	//Generates the passed base-color with 5 different lightness values
    	yield $baseColor->toHsl()->setLightness(.1);
    	yield $baseColor->toHsl()->setLightness(.2);
    	yield $baseColor->toHsl()->setLightness(.3);
    	yield $baseColor->toHsl()->setLightness(.4);
    	yield $baseColor->toHsl()->setLightness(.5);
    }
}
<?php

namespace Phim\Color\Scheme;

use Phim\Color;
use Phim\ColorInterface;

class MyCustomContinousScheme extends Color\Scheme\ContinousSchemeBase
{

    protected function generateStep(ColorInterface $baseColor, $i, $step)
    {

    	//Does the same as above, but both, the amount and step-value can be passed to the constructor
        return Color::lighten($baseColor, $i * $step);
    }
}

整合一切

请注意,您可以将方案的结果进行组合和筛选,因为它们也是调色板。结果始终是一个 Palette 实例(即使在 NameMonochromaticScheme 中也是如此,所以请注意,您将完全使用您的命名功能)

您可以将上面的所有东西组合成一些酷炫的颜色操作。

$colors = [Color::RED, Color::BLUE, Color::GREEN, Color::YELLOW];

$palette = new Palette();

foreach ($colors as $color) {

    //Add the tetradic scheme for each color
    $palette = Palette::merge($palette, new TetradicScheme($color));
}

//Only take blue-ish colors
$palette = Palette::filterByHueRange($palette, Color::HUE_RANGE_BLUE);

//Avoid similar colors
$palette = Palette::filterSimilarColors($palette, 8);

//Print a nice HTML representation with 4 columns of the palette for debugging
Palette::toHtml($palette, 4);

这将产生 bluenavy 颜色。在 filterSimilarColors 上减少所需范围会产生更多和不同的类似蓝色。

在 HTML 中使用颜色

当转换为字符串时,颜色大多会自动转换为它们最佳的 CSS 表示形式。

echo Color::get('red'); //'rgb(255, 0, 0)'
echo Color::get('hsl(120, .5, .2)'); //'hsl(120, 50%, 20%)'

鉴于一些浏览器不支持 hsl() CSS 颜色,最好在转换为字符串之前始终转换为 RGB 或 RGBA。

echo Color::get('red')->getRgba(); //rgba(255, 0, 0, 1)

这不是自动完成的,以给您使用 hsl() 写作风格的能力。我们还不知道 CSS 是否将在未来实现更多的颜色构造函数。

请注意,您传递给 Color::get 的大多数字面量都会返回一个 RgbaColor-实例,因为大多数系统(十六进制、整型、名称)都是基于 RGB 值设计的。

几何,图像处理等。

如您所见,如果我现在继续这样做,这个 README 将会非常大,所以我将在这里停止,并慢慢地制作更好的结构化文档。

您可以在 tests/how-to.php 中看到一个图像处理的快速示例。

示例,代码,贡献,支持

如果您有任何问题,代码中的问题或您想贡献,可以查阅 示例代码、编写一个 问题给我发一封电子邮件。您也可以通过拉取请求进行贡献,只需发送它们即可。

要查看所有可用的颜色名称列表,请 点击这里

如果您喜欢我的工作,它以某种方式帮助了您,简化了您的工作,或类似的事情,请考虑通过 请我喝杯咖啡 来支持我。

致谢