0
votes

This is my code for selecting an image, and then converting that image into several colours and then posting them to a directory on my server. I am having some issues with the "if($im && imagefilter($im, IMG_FILTER_COLORIZE, $colour->RGB))" part of the query. IF i put in an actual RGB code in the "$colour->RGB" part it works. However the RGB saved in the database is saved correctly, this is the result of the query.

White 255,255,255 White shading failed.

Green 0,255,0 Green shading failed.

Orange 255,165,0 Orange shading failed.

Red 255,0,0 Red shading failed.

Pink 255,51,153 Pink shading failed.

Blue 0,0,255 Blue shading failed.

Silver 192,192,192 Silver shading failed.

Yellow 255,255,0 Yellow shading failed.

<?php

include('PATH_TO/ez_sql_core.php');
include('PATH_TO/ez_sql_mysql.php');

$db = new ezSQL_mysql('xxxx','xxxx','xxxx','xxxx');

$colours = $db->get_results("SELECT Colour, RGB FROM colours");

foreach($colours as $colour)
{
    echo $colour->Colour;
    echo "<br>";
    echo $colour->RGB;  
    echo "<br>";

    $im =     imagecreatefrompng("IMAGE_URL");

    if($im && imagefilter($im, IMG_FILTER_COLORIZE, $colour->RGB))
    {
        echo "Image successfully shaded {$colour->Colour}.";

        imagepng($im, "images/Convert/IMAGENAME-{$colour->Colour}.png");
        imagedestroy($im);
    }
else
    {
        echo "{$colour->Colour} shading failed.";
    }
    echo "<br>";
}

Can anyone advise why $colour->RGB will show correctly on the echo yet not on the "if($im && imagefilter($im, IMG_FILTER_COLORIZE, $colour->RGB))"

2

2 Answers

0
votes

Per the docs the RGB colour values need to be provided as separate arguments:

list($red,$green,$blue) = explode(',', $colour->RGB);

imagefilter($im, IMG_FILTER_COLORIZE, $red, $green, $blue);
-1
votes

http://php.net/manual/en/function.imagefilter.php

From the manual COLORIZE filter needs individual R G B values. You are passing string.

Convert imagefilter($im, IMG_FILTER_COLORIZE, $colour->RGB)

To

$ rgb = explode ($color-> RGB); imagefilter($im, IMG_FILTER_COLORIZE, $rgb [0], $rgb [1], $rgb [2] );