tooleks / php-avg-color-picker
The PHP Average Color Picker Library
Installs: 105 296
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: ^7.0
- ext-gd: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^5.5
This package is not auto-updated.
Last update: 2024-12-21 22:13:18 UTC
README
The package provides the library for picking an average color from the given image. Currently it supports image/png
, image/jpeg
, image/gif
image MIME types.
Example
Input - The Image Path
Output - The Image Average Color
Requirements
"php": "^7.0", "ext-mbstring": "*", "ext-gd": "*"
Installation
Package Installation
Execute the following command to get the latest version of the package:
composer require tooleks/php-avg-color-picker
Usage Examples
<?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".
You can use the calculated value to show the average image color in its container before the image is loaded.
<div style="background-color: <?= $imageAvgHexColor ?>; width: <?= $imageWidth ?>; height: <?= $imageHeight ?>;"> <img src="/url/to/the/image.(jpg|jpeg|png|gif)" alt=""> </div>
Tests
Execute the following command to run tests:
./vendor/bin/phpunit