Tuesday, July 22, 2014

grab image color php

7/22/2014

This PHP class can extract the colors of a given image file. It can read an image in the GIF, JPEG or PNG format and extracts the list of colors that are used in the pixels of the image. The class takes in account a similarity factor to consider very similar colors as if they are the same. It returns an array with the colors used in the picture sorted by the frequency of the pixels that use each color.


Save following php code as a "colorextractor.class.php" without double quotes.

<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

class ColorExtractor {
    private function RGBtoHEX($rgb) {
        $rgb = $rgb . ",";
        preg_match("/(.*?)\,(.*?)\,(.*?)\,/", $rgb, $colors);
        $r   = $colors[1];
        $g   = $colors[2];
        $b   = $colors[3];
        $hex = "#";
        $hex .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
        $hex .= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
        $hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
        return $hex;
    }
    public function getColors($img, $num, $precision, $type) {
        $allArray  = array();
        $retArray  = array();
        $maxWidth  = 200;
        $maxHeight = 200;
        $cw        = 240;
        $num       = preg_replace('/[^0-9]/', '', $num);
        $precision = preg_replace('/[^0-9]/', '', $precision);
        if ($num == "") {
            $num = 5;
        }
        if ($precision == "") {
            $precision = 5;
        }
        if ($type != "rgb" && $type != "hex"){
            $type = "rgb";
        }
        $det = GetImageSize($img);
        if (!$det) {
            return "Enter a valid Image";
        }
        $imgWidth  = $det[0];
        $imgHeight = $det[1];
        if ($imgWidth > $maxWidth || $imgHeight > $maxHeight) {
            if ($imgWidth > $imgHeight) {
                $newWidth  = $maxWidth;
                $diff      = round($imgWidth / $maxWidth);
                $newHeight = round($imgHeight / $diff);
            } else {
                $newHeight = $maxHeight;
                $diff      = round($imgHeight / $maxHeight);
                $newWidth  = round($imgWidth / $diff);
            }
        }
        if ($det[2] == 1) {
            $origImage = imagecreatefromgif($img);
        } else if ($det[2] == 2) {
            $origImage = imagecreatefromjpeg($img);
        } else if ($det[2] == 3) {
            $origImage = imagecreatefrompng($img);
        }
        $newImage = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight);
        for ($x = 0; $x < $newWidth; $x++) {
            for ($y = 0; $y < $newHeight; $y++) {
                $colors = imagecolorsforindex($newImage, imagecolorat($newImage, $x, $y));
                $r      = $colors['red'];
                $g      = $colors['green'];
                $b      = $colors['blue'];
                $a      = $colors['alpha'];
                if (!($r > $cw && $g > $cw && $b > $cw)) {
                    if (!($r == 0 && $g == 0 && $b == 0)) {
                        $rgb = $r . "," . $g . "," . $b;
                        array_push($allArray, $rgb);
                    }
                }
            }
        }
        $count = array_count_values($allArray);
        arsort($count);
        $keys = array_keys($count);
        for ($s = 0; $s < $num; $s++) {
            $first = $keys[$s] . ",";
            preg_match("/(.*?)\,(.*?)\,(.*?)\,/", $first, $colors2);
            $r  = $colors2[1];
            $g  = $colors2[2];
            $b  = $colors2[3];
            $iS = $s + 1;
            for ($i = $iS; $i < count($keys); $i++) {
                $next = $keys[$i] . ",";
                preg_match("/(.*?)\,(.*?)\,(.*?)\,/", $next, $colors2);
                $r2 = $colors2[1];
                $g2 = $colors2[2];
                $b2 = $colors2[3];
                if (abs($r2 - $r) < $precision || abs($g2 - $g) < $precision || abs($g2 - $g) < $precision) {
                    unset($keys[$i]);
                }
            }
            $keys = array_values($keys);
        }
        if ($type == "hex") {
            for ($j = 0; $j < $num; $j++) {
                $color = $this->RGBtoHEX($keys[$j]);
                array_push($retArray, $color);
            }
        } else if ($type == "rgb"){
            for ($j = 0; $j < $num; $j++) {
                array_push($retArray, $keys[$j]);
            }
        }
        return $retArray;
    }
}

?>
Usage of class: <?php //Require the ColorExtractor Class require("colorextractor.class.php"); $img = "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRveQ2t7dRmKi1aq54eHUEto2OfDyxmfIZn--lVvye_Hxc2eJ59"; $ce = new ColorExtractor; /* Call the function getColors, this function gets the main colors from the image getColors(Link to Image, Number of colors to return, minumum difference between each color, Type of return "rgb" or "hex"); */ $colorArray = $ce->getColors($img, 5, 11, "rgb"); ?> <img src="<?php echo $img; ?>"> <br /><br /> <table width="200px" border="1"> <?php foreach ($colorArray as $color) {     echo '<tr>';     if (substr($color, 0, 1) == "#") {         echo '<td width="50%" align="center">' . $color . '</td><td width="50%" align="center"><div style="background-color:' . $color . '; width:50px; height:50px;"></div></td>';     } else {         echo '<td width="50%" align="center">' . $color . '</td><td width="50%" align="center"><div style="background-color:rgb(' . $color . '); width:50px; height:50px;"></div></td>';     }     echo '</tr>'; } ?>
</table>

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top