tooleks / php-avg-color-picker
PHP 平均颜色选择库
1.1.2
2017-07-07 11:41 UTC
Requires
- php: ^7.0
- ext-gd: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^5.5
This package is not auto-updated.
Last update: 2024-09-14 20:40:47 UTC
README
该包提供了从给定图像中选择平均颜色的库。目前它支持 image/png
、image/jpeg
、image/gif
图像 MIME 类型。
示例
输入 - 图片路径
输出 - 图片的平均颜色
要求
"php": "^7.0", "ext-mbstring": "*", "ext-gd": "*"
安装
包安装
执行以下命令以获取该包的最新版本
composer require tooleks/php-avg-color-picker
使用示例
<?php use Tooleks\Php\AvgColorPicker\Gd\AvgColorPicker; $imageAvgRgbColor = (new AvgColorPicker)->getImageAvgRgbByPath($imagePath); // or $imageAvgRgbColor = (new AvgColorPicker)->getImageAvgRgbByResource($gdImageResource); // or $imageAvgRgbColor = (new AvgColorPicker)->getImageAvgRgb($imagePath); // or $imageAvgRgbColor = (new AvgColorPicker)->getImageAvgRgb($gdImageResource); // The `$imageAvgRgbColor` variable contains the average color of the given image in RGB format (array)[255, 255, 255].
<?php use Tooleks\Php\AvgColorPicker\Gd\AvgColorPicker; $imageAvgHexColor = (new AvgColorPicker)->getImageAvgHexByPath($imagePath); // or $imageAvgHexColor = (new AvgColorPicker)->getImageAvgHexByResource($gdImageResource); // or $imageAvgHexColor = (new AvgColorPicker)->getImageAvgHex($imagePath); // or $imageAvgHexColor = (new AvgColorPicker)->getImageAvgHex($gdImageResource); // The `$imageAvgHexColor` variable contains the average color of the given image in HEX format (string)"#fffff".
您可以使用计算出的值在图片加载前显示容器中的平均图像颜色。
<div style="background-color: <?= $imageAvgHexColor ?>; width: <?= $imageWidth ?>; height: <?= $imageHeight ?>;"> <img src="/url/to/the/image.(jpg|jpeg|png|gif)" alt=""> </div>
测试
执行以下命令以运行测试
./vendor/bin/phpunit