Login   Register  
PHP Classes
elePHPant
Icontem

Class Modifications

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      Color extract  >  All threads  >  Class Modifications  >  (Un) Subscribe thread alerts  
Subject:Class Modifications
Summary:Additional functionality added.
Messages:1
Author:John Hamson
Date:2008-06-14 20:23:39
 

  1. Class Modifications   Reply  
Picture of John Hamson
John Hamson
2008-06-14 20:23:39
I added a function to get percentage of image transparency vs solid colors based on this class. The original function passes out the alpha value w/ the hex so the 2 need to be split if you just need the hex code.
<?php
/**
* This class can be used to get the most common colors in an image. It needs one parameter: $image, which is the filename of the image you want to process.
* Originally based on BSD licensed code from http://www.phpclasses.org/browse/package/3370.html
* Modified by John Hamson - techiemon@gmail.com to support a function to measure what percent of the image is transparent or solid.
*/
class ColorQuery
{
/**
* The filename of the image (it can be a JPG, GIF or PNG image)
*
* @var string
*/
public $image;

/**
* Returns an array of HEX_alpha of each pixel in the image
*
* @return array
*/
public function get_color_data()
{
if (isset($this->image))
{
$size = GetImageSize($this->image);

if ($size[2]==1)
$image_orig=imagecreatefromgif($this->image);
if ($size[2]==2)
$image_orig=imagecreatefromjpeg($this->image);
if ($size[2]==3)
$image_orig=imagecreatefrompng($this->image);

$im = $image_orig;
$imgWidth = imagesx($im);
$imgHeight = imagesy($im);
$hexarray = array();
$hexarray2 = array();

for ($y=0; $y < $imgHeight; $y++)
{
for ($x=0; $x < $imgWidth; $x++)
{
$index = imagecolorat($im,$x,$y);
$Colors = imagecolorsforindex($im,$index);
$Colors['red']=intval((($Colors['red'])+15)/32)*32;
//ROUND THE COLORS, TO REDUCE THE NUMBER OF COLORS, SO THE WON'T BE ANY NEARLY DUPLICATE COLORS!
$Colors['green']=intval((($Colors['green'])+15)/32)*32;
$Colors['blue']=intval((($Colors['blue'])+15)/32)*32;

if ($Colors['red']>=256)
$Colors['red']=240;
if ($Colors['green']>=256)
$Colors['green']=240;
if ($Colors['blue']>=256)
$Colors['blue']=240;

$hex = substr("0".dechex($Colors['red']),-2).substr("0".dechex($Colors['green']),-2).substr("0".dechex($Colors['blue']),-2);
$alpha = $Colors['alpha'];

$hexarray[]=$hex.'_'.$alpha;
}
}

$hexarray=array_count_values($hexarray);
natsort($hexarray);
$hexarray=array_reverse($hexarray,true);

$keys=array_keys($hexarray);

$this->color_data = $hexarray;

}
else die("You must enter a filename! (\$image parameter)");
}

public function get_density_data() {
$this->get_color_data();

foreach ($this->color_data as $v=>$k) {
$color = split('_',$v);
if ($v=='000000_127') {
$alpha_count = $k;
} else {
$color_count = $color_count + $k;
}

$total_count = ($alpha_count+$color_count);
$alpha_percent = round(($alpha_count/$total_count),2)*100;
$color_percent = round(($color_count/$total_count),2)*100;

$this->solid = $color_percent;
$this->alpha = $alpha_percent;
}
}



}
?>