Determine the dominant color of an image in php

Posted: January 12, 2013 in PHP
Tags:
$i = imagecreatefromjpeg("image.jpg");

for ($x=0;$x<imagesx($i);$x++) {
    for ($y=0;$y<imagesy($i);$y++) {
        $rgb = imagecolorat($i,$x,$y);
        $r   = ($rgb >> 16) & 0xFF;
        $g   = ($rgb >>  & 0xFF;
        $b   = $rgb & 0xFF;

        $rTotal += $r;
        $gTotal += $g;
        $bTotal += $b;
        $total++;
    }
}

$rAverage = round($rTotal/$total);
$gAverage = round($gTotal/$total);
$bAverage = round($bTotal/$total);
Advertisements
Comments
  1. Thanks for a marvelous posting! I really enjoyed reading it, you
    are a great author.I will remember to bookmark your blog and definitely will come back
    at some point. I want to encourage you to definitely continue your great writing, have a nice holiday weekend!

  2. It’s in point of fact a great and useful piece of info. I am glad that you simply shared this useful info with us. Please stay us up to date like this. Thanks for sharing.

  3. I go to see everyday some sites and sites to read articles, however this webpage offers feature based posts.

  4. Hello There. I found your blog using msn. This is an extremely well written article.

    I will make sure to bookmark it and return to read more of your
    useful info. Thanks for the post. I will certainly comeback.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s