Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.4 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorJB
    • CommentTimeJun 12th 2008 edited
     permalink
    HI

    I have been asked to provide a solution to this problem as part of the selection process for a job. I have limited experience of PHP, however when I told my potential employer this they were happy for me to spend some time researching the following problem:

    "We have a dynamic online magazine. Each page of the magazine contains an image and an article. Your problem is to dynamically colour the article text and the article background to match the colours that are present in the image, making considerations for readability and accessibility. Justify your choice of colour and explain any extensions to this that you feel would enhance the solution."

    What I've got so far http://www.setapart.co.uk/colours.php />
    I have been able to pull two colours from the image and apply them as background colours. What I want is the first colour to be used as the background colour and the second colour as the text colour. The image is also to be displayed beside the text.

    This is my code so far:
    <?php
    function colorPalette($imageFile, $numColors, $granularity = 5)
    {
    $granularity = max(1, abs((int)$granularity));
    $colors = array();
    $size = @getimagesize($imageFile);
    if($size === false)
    {
    user_error("Unable to get image size data");
    return false;
    }
    $img = @imagecreatefromjpeg($imageFile);
    if(!$img)
    {
    user_error("Unable to open image file");
    return false;
    }
    for($x = 0; $x < $size[0]; $x += $granularity)
    {
    for($y = 0; $y $size[1]; $y += $granularity)
    {
    $thisColor = imagecolorat($img, $x, $y);
    $rgb = imagecolorsforindex($img, $thisColor);
    $red = round(round(($rgb['red'] / 0x33)) * 0x33);
    $green = round(round(($rgb['green'] / 0x33)) * 0x33);
    $blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
    $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
    if(array_key_exists($thisRGB, $colors))
    {
    $colors[$thisRGB]++;
    }
    else
    {
    $colors[$thisRGB] = 1;
    }
    }
    }
    arsort($colors);
    return array_slice(array_keys($colors), 0, $numColors);
    }
    // sample usage:
    $palette = colorPalette('MOURNES1.JPG', 2, 4);
    echo "<table>\n";
    foreach($palette as $color)
    {
    echo "<tr>
    <td style='background-color:#$color;width:20em;color:#fff;'>Morbi feugiat mauris at velit. Proin rutrum lectus. Proin pulvinar turpis tempor nibh. Cras sit amet magna sed risus tempor vestibulum. Nunc vitae nulla. Vivamus fermentum. Praesent a sem. Cras eu neque ultricies tellus tristique vehicula. Praesent dignissim consequat metus. Integer dolor. Donec pellentesque, libero eu ullamcorper suscipit, lorem augue molestie arcu, vitae sodales quam nulla vel urna. Suspendisse accumsan sem nec leo. Proin dui ante, placerat id, consectetuer et, gravida in, velit. Duis non massa. Etiam mollis. Vestibulum id est. Sed sit amet tellus. Vestibulum varius dolor vitae velit.</td>
    <td><img src='$imageFile' alt='photograph' /></td>
    </tr>\n";
    }
    echo "</table>\n";

    Any help appreciated.

    Regards

    Jeremy
  1.  permalink
    This is some of the most advanced stuff I've seen around here in ages. I'd be interested to know if there'd be a way to calculate a proper contrast between the text and the background as well. Also, you really don't need the tables, but that's not a big deal considering the bigger picture.

    A refreshing problem to say the least.
Add your comments
    Username Password
  • Format comments as (Help)