Skip to content

Instantly share code, notes, and snippets.

@tjlytle
Created November 17, 2009 19:19
Show Gist options
  • Save tjlytle/237185 to your computer and use it in GitHub Desktop.
Save tjlytle/237185 to your computer and use it in GitHub Desktop.
Class to find 'average' image color.
<?php
/**
* imageColor
*
* Shows three methods to find the 'average' image color.
*
* Each function expects a gd image object.
*
* imageColor::averageResize($image) resizing to 1px, and checking the color.
* imageColor::averageBorder($image) find the average color of all border pixels.
* imageColor::averageImage($image) find the average color of all pixels.
*
*/
class imageColor
{
function scanLine($image, $height, $width, $axis, $line)
{
$i = 0;
if("x" == $axis){
$limit = $width;
$y = $line;
$x =& $i;
if(-1 == $line){
$y = 0;
$y2 = $width -1;
$x2 =& $i;
}
} else {
$limit = $height;
$x = $line;
$y =& $i;
if(-1 == $line){
$x = 0;
$x2 = $width -1;
$y2 =& $i;
}
}
$colors = array();
if(-1 == $line){
for($i = 0; $i < $limit; $i++){
self::addPixel($colors, $image, $x, $y);
self::addPixel($colors, $image, $x2, $y2);
}
} else {
for($i = 0; $i < $limit; $i++){
self::addPixel($colors, $image, $x, $y);
}
}
return $colors;
}
function addPixel(&$colors, $image, $x, $y)
{
$rgb = imagecolorat($image, $x, $y);
$color = imagecolorsforindex($image, $rgb);
$colors['red'][] = $color['red'];
$colors['green'][] = $color['green'];
$colors['blue'][] = $color['blue'];
}
function totalColors($color, $colors)
{
$color['red'] += array_sum($colors['red']);
$color['green'] += array_sum($colors['green']);
$color['blue'] += array_sum($colors['blue']);
return $colors;
}
function averageTotal($color, $count)
{
$color['red'] = intval($color['red']/$count);
$color['green'] = intval($color['green']/$count);
$color['blue'] = intval($color['blue']/$count);
return $color;
}
function averageResize($image)
{
$width = imagesx($image);
$height = imagesy($image);
$pixel = imagecreatetruecolor(1, 1);
imagecopyresampled($pixel, $image, 0, 0, 0, 0, 1, 1, $width, $height);
$rgb = imagecolorat($pixel, 0, 0);
$color = imagecolorsforindex($pixel, $rgb);
return $color;
}
function averageBorder($image)
{
$width = imagesx($image);
$height = imagesy($image);
$colors = self::scanLine($image, $height, $width, 'x', -1);
self::totalColors(&$color, $colors);
$colors = self::scanLine($image, $height, $width, 'y', -1);
self::totalColors(&$color, $colors);
$borderSize = ($height=$width)*2;
self::averageTotal(&$color, $borderSize);
return $color;
}
function averageImage($image)
{
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
for($line = 0; $line < $height; $line++){
$colors = self::scanLine($image, $height, $width, 'x', $line);
self::totalColors(&$color, $colors);
}
$count = $width*$height;
self::averageTotal(&$color, $count);
return $color;
}
}
?>
@ve3
Copy link

ve3 commented Sep 21, 2022

Updated for work with PHP 8.1

<?php
/**
 * imageColor 
 * 
 * @link https://gist.github.com/tjlytle/237185 Original source code.
 * 
 * Shows three methods to find the 'average' image color.
 * 
 * Each function expects a gd image object.
 * 
 * imageColor::averageResize($image) resizing to 1px, and checking the color.
 * imageColor::averageBorder($image) find the average color of all border pixels.
 * imageColor::averageImage($image)  find the average color of all pixels.
 * 
 */
class ImageColor
{
	protected static function scanLine($image, $height, $width, $axis, $line)
	{
		$i = 0;
		
		if("x" == $axis){
			$limit = $width;
			$y = $line;
			$x =& $i;
			
			if(-1 == $line){
				$y = 0;
				$y2 = $width -1;
				$x2 =& $i;	
			}
		} else {
			$limit = $height;
			$x = $line;
			$y =& $i;
			
			if(-1 == $line){
				$x = 0;
				$x2 = $width -1;
				$y2 =& $i;	
			}
		}
		
		$colors = array();
		
		if(-1 == $line){
			for($i = 0; $i < $limit; $i++){
				static::addPixel($colors, $image, $x, $y);
				static::addPixel($colors, $image, $x2, $y2);
			}
		} else {
			for($i = 0; $i < $limit; $i++){
				static::addPixel($colors, $image, $x, $y);
			}
		}
		
		return $colors;
	}
	
	protected static function addPixel(&$colors, $image, $x, $y)
	{
		$rgb = imagecolorat($image, $x, $y);
		$color = imagecolorsforindex($image, $rgb);
		$colors['red'][] = $color['red'];
		$colors['green'][] = $color['green'];
		$colors['blue'][] = $color['blue'];
	}
	
	protected static function totalColors(&$color, $colors)
	{
		$color['red'] += array_sum($colors['red']);
		$color['green'] += array_sum($colors['green']);
		$color['blue'] += array_sum($colors['blue']);

		return $colors;
	}
	
	protected static function averageTotal(&$color, $count)
	{
		$color['red'] = intval($color['red']/$count);
		$color['green'] = intval($color['green']/$count);
		$color['blue'] = intval($color['blue']/$count);
		
		return $color;
	}
	
	public static function averageResize($image)
	{
		$width = imagesx($image);
		$height = imagesy($image);
		
		$pixel = imagecreatetruecolor(1, 1);
		imagecopyresampled($pixel, $image, 0, 0, 0, 0, 1, 1, $width, $height);
		$rgb = imagecolorat($pixel, 0, 0);
		$color = imagecolorsforindex($pixel, $rgb);
		
		return $color;
	}
	
	public static function averageBorder($image)
	{
		$width = imagesx($image);
		$height = imagesy($image);

		$pixel = imagecreatetruecolor(1, 1);
		$rgb = imagecolorat($pixel, 0, 0);
		$color = imagecolorsforindex($pixel, $rgb);
		
		$colors = static::scanLine($image, $height, $width, 'x', 0);
		static::totalColors($color, $colors);
		
		$colors = static::scanLine($image, $height, $width, 'y', -1);
		static::totalColors($color, $colors);

		$borderSize = ($height+$width)*2;
		static::averageTotal($color, $borderSize);
		
		return $color;
	}
	
	public static function averageImage($image)
	{
		$width = imagesx($image);
		$height = imagesy($image);
		
		$colors = [];

		$pixel = imagecreatetruecolor(1, 1);
		$rgb = imagecolorat($pixel, 0, 0);
		$color = imagecolorsforindex($pixel, $rgb);
		
		for($line = 0; $line < $height; $line++){
			$colors = static::scanLine($image, $height, $width, 'x', $line);
			static::totalColors($color, $colors);
		}
		
		$count = $width*$height;
		static::averageTotal($color, $count);
		
		return $color;
	}
}

Fixed a lot of errors. I'm not sure is it work correctly compared to original code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment