Matching colours to image colours with PHP

Back in June/July 2008 I was around looking for a job as I was just finishing my degree, and I had an intreaging test given to me as part of a preparation to an interview in London (which I didn't goto in the end) but I thought I would share how to do it with the world in hope it may help someone else in the future. Albeit clunky, and not overly effective it will retrieve the most popular colours within a specified image, and then based on that I attemped to make an algorithm which then chose a colour relative to the colour code of the most popular colours used in the picture.

Here is the Code for index.php:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
 
require_once('functions.colorcontrol.php');
 
//for testing purposes, fetchs one random image
$num = rand(1,4);
// sets the value to be sent to the pallete retrieval function.
$image = $num .'.jpeg';
 
//fetchs the array returned form the function
$hex = fetch_hexcodes($image);
 
//populates the new returned colour codes into their specific var
$font = $hex[4];
$bg = $hex[0];
$border = $hex[3];
$title = $hex[2];
 
echo "font color: $font \n";
echo "bg color: $bg \n";
echo "border color: $border \n";
echo "title color: $title \n";
 
?>

 

This is the function code which goes with this file:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

 
function fetch_hexcodes($image)
{
//fetchs the array of all colours in image
$tmp = fetch_palette($image);
//sorts the colours by the most commonly used
arsort($tmp);
 
//vars necessary for looping
$c = 1;
$img = imagecreatefromjpeg($image);
 
//runs through and fetchs the RGB indexes for the most
//common 5 colours

foreach ($tmp as $key => $val)
{
if ($c < 6)
{
$topColours[$c] = imagecolorsforindex($img, $key);
$c++;
}
}
 
//vars necessary for looping
$c = 1;
$c2 = 0;
//this loop constructs the rgb codes into
//hexidecimal values so they

//are ready to be used inside the html code
while ($topColours[$c])
{
$hexlist[$c2] = '#'. dechex($topColours[$c]['red']) .
dechex($topColours[$c]['green']) . dechex($topColours[$c]['blue']);

$c2++;
$c++;
}
 
//returns the array of colours
return $hexlist;
}
 
function fetch_palette($img)
{
//fetchs image size for later comparison
$imgDetails = getimagesize($img);
 
//vars necessary for looping
$x = 1;
$y = 1;
$img = imagecreatefromjpeg($img);
 
//This double loop runs through every single
//pixel on the submitted image

while ($y <= intval($imgDetails[1]))
{
while ($x <= intval($imgDetails[0]))
{
//assign the colour value for the pixel
//being processed

$colour = imagecolorat($img, $x, $y);
//checks if the colour is in the array
//or not and

//populates the array accordingly.
if ($colours[$colour])
{
$colours[$colour]++;
}
else
{
$colours[$colour] = 1;
}
 
$x++;
}
//x-axis is necessary to run through it
//once for each y-axis

//so it is reset here ready for the next y-axis loop
$x = 1;
$y++;
}
//returns the list of raw colour values ready for processing.
return $colours;
}
 
?>

 

I hope this helps some of you out there, and who knows it might even get you that job if its still going, hehe!

I have also attached a zip file for this snippet for peoples reference, good luck!


PHP

Buy me a beer or a book?

amazon PayPal - The safer, easier way to donate online!

Free Downloads