PHP

Buy me a beer or a book?

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

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!


  • Mike

    Posted at 2012-04-27 06:27:33

    online logo design
    logo design canada

    Reply to comment

  • Bomblawter

    Posted at 2012-01-17 21:35:04

    I have been assigned to do a report for our school newspaper on this subject, and your post has been beneficial. Can you please add more reference to this topic, thanks.
    buy backlinks

    Reply to comment

    • Firstrowstart

      Posted at 2012-01-21 21:13:11

      I am in the middle of working on a school report on this topic and your post has helped me with the information I needed to complete it. Thanks.
      honda insurance quotes

      Reply to comment

  • Happprze

    Posted at 2012-01-16 23:13:55

    Part of my job requires me to stay on top of this subject. The article you have written has been beneficial in my research. Thanks a lot
    Public Speaking

    Reply to comment

  • Sorrwman

    Posted at 2012-01-15 21:12:29

    I am currently working on an assignment and I have been exploring your blog for a few hours. Thank you for your post it proved helpful for me.
    Buy Herbal Incense

    Reply to comment

  • TheTimenwes

    Posted at 2012-01-12 18:51:33

    This post has helped me to have another perspective. I am researching this topic for a paper I am writing. Your article provided me great insight of my topic.
    Press Release Distribution

    Reply to comment

  • Wentplace

    Posted at 2012-01-11 18:14:56

    I enjoyed reading your article and found it to be informative and to the point. Thank you for not rambling on and on just to fill the page.
    Lawyer Marketing

    Reply to comment

  • world of opinion

    Posted at 2012-01-04 19:52:29

    asdfasdf

    Reply to comment

  • Elevenside00

    Posted at 2012-01-04 19:50:46

    This topic has always been one of my favorite subjects to read about. I have found your post to be very rousing and full of good information. I will check your other articles shortly.
    [url=http://world-of-opinion.com/]world of opinion[/url]

    Reply to comment

  • Elevenside00

    Posted at 2012-01-04 19:48:27

    This topic has always fascinated me. Thank you for writing an article that has great content and is well written. Well I am inspired by your writing style.
    world of opinion

    Reply to comment

Post your comments...

Free Downloads