Skip to content

A simple and efficient PHP function that allows you to quickly and easily determine the average color of an image.

Notifications You must be signed in to change notification settings

xyba1337/PHP-Average-Color-Extractor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Image Average Color Extractor

A simple, lightweight and fast PHP function that uses the GD library to extract the average color of an image.

How to use

  1. Include the function in your PHP script:
require_once 'imgextractor.php';
  1. Call the function by passing the path to the image file as the argument:
$avgColor = getAvgColor('path/to/image.<any image extension>');
  1. The function will return the average color of the image as a hex value, for example: #ff0000

Basic example

<?php
require_once('imgextractor.php');
$img_url ="https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819__340.jpg";
?>

<body>
    <img src=<?php echo $img_url; ?>>
    <style>
        body {
            background-color: <?php echo getAvgColor($img_url) ?>;
        }
    </style>
</body>

Note

Keep in mind that this function will return the average color of an image, not the main color. Also it's a simple implementation, it may not be the best solution for all cases.

Contribution

Feel free to open an issue or a PR if you have any suggestion or modification to the code.

License

This project is licensed under the MIT License

About

A simple and efficient PHP function that allows you to quickly and easily determine the average color of an image.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages