net-tools / gd-indexed-color-converter
Composer 库 gd-indexed-color-converter
This package is auto-updated.
Last update: 2024-09-24 21:07:08 UTC
README
GDIndexedColorConverter 是一个简单的库,可以将图像转换为 索引颜色模式。在索引颜色模式下,图像可以仅使用几种特定的颜色显示。
为了实现图像抖动效果,GDIndexedColorConverter 使用 [Floyd–Steinberg 抖动] (http://zh.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering) 算法将每个像素的错误扩散应用于其相邻像素。
要求
由于 GDIndexedColorConverter 使用了 GD 扩展 的一些功能,您需要在 PHP 配置文件中启用 GD 扩展 (php.ini
)。
用法
GDIndexedColorConverter 提供一个名为 convertToIndexedColor
的函数,用于将图像转换为索引颜色模式,它接受三个参数(如下所示),并返回一个新图像资源,该资源以索引颜色模式。
-
im
(imageresource) 由 GD 库的函数创建的图像资源。 -
palette
(array) 包含索引颜色模式图像将使用的所有特定颜色的调色板。此参数是一个数组,存储所有颜色,每个颜色是一个索引数组,由红色、绿色和蓝色通道值组成。 -
dither
(float) Floyd–Steinberg 抖动算法对图像的影响程度。此参数是可选的,其默认值为 0.75,其值必须在 0 和 1 之间。
代码示例
// create an image $image = imagecreatefromjpeg('example.jpg'); // create a gd indexed color converter $converter = new GDIndexedColorConverter(); // the color palette $palette = array( array(0, 0, 0), array(255, 255, 255), array(255, 0, 0), array(0, 255, 0), array(0, 0, 255) ); // convert the image to indexed color mode $new_image = $converter->convertToIndexedColor($image, $palette, 0.8); // save the new image imagepng($new_image, 'example_indexed_color.png', 0);
抖动
在索引颜色图像上应用不同的抖动值,可以得到不同的图像效果。在 example
文件夹中,有一个简单的示例,该示例创建三个具有不同抖动值(0.2、0.4、0.8)和五种颜色(白色、黑色、红色、绿色和蓝色)的索引图像。
示例图像 shell.jpg
由 @sage_solar 创建。该图像在 Creative Commons License 下。
许可证
GDIndexedColorConverter 在 [MIT 许可证] (https://raw.githubusercontent.com/ccpalettes/gd-indexed-color-converter/master/LICENSE) 下授权。