Link to home
Start Free TrialLog in
Avatar of walkman69
walkman69

asked on

Is there a way i can change HUI on a picture thru PHP?

I know there are some functions in PHP for setting alpha blending and such,
found here: http://se2.php.net/manual/en/function.imagealphablending.php

However I can't find a way to set HUI on a picture thru PHP?

What i'm really looking for is a way to somehow apply a selected colour
to a monochrome image.. Making it blue-ish, or red-ish.. och whatever.. -ish there is..

Think monochrome..

Don't point me in a direction of a complete library, I only need this one function.

Thanks in advance!

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Can you give us a definition or reference about HUI?  Thanks, ~Ray
Hue would make more sense.
http://en.wikipedia.org/wiki/Hue

Please post a test image and I will write a script showing how to do this using the PHP GD library
http://www.php.net/manual/en/ref.image.php

Thanks and regards, ~Ray
Or just have a look at this:
http://www.laprbass.com/RAY_temp_walkman69.php?r=255

Looks like some good information on the PHP man pages here (although I have not tested it)
http://www.php.net/manual/en/function.imagefilter.php#91796
<?php // RAY_temp_walkman69.php
error_reporting(E_ALL);

// GET OUR ARGUMENTS (NEED SANITY CHECKS HERE)
$r = isset($_GET["r"]) ? (int)$_GET["r"] : 0;
$g = isset($_GET["g"]) ? (int)$_GET["g"] : 0;
$b = isset($_GET["b"]) ? (int)$_GET["b"] : 0;
$a = isset($_GET["a"]) ? (int)$_GET["a"] : 0;

// PLACES TO PUT OUR IMAGES
$f = 'new.png';
$s = 'gsc.png';

// A DIRECTORY TO HOLD OUR WORK
$d = 'RAY_junk/';

// LOCATION OF A COLOR IMAGE
$url = $d . 'color.png';

// LOAD A COLOR IMAGE http://php.net/manual/en/function.imagecreatefrompng.php
$img = ImageCreateFromPNG($url);

// WRITE THE GRAY IMAGE
ImageFilter($img, IMG_FILTER_GRAYSCALE);
ImagePNG($img, $d . $s);

// RE-LOAD A COLOR IMAGE http://php.net/manual/en/function.imagecreatefrompng.php
$img = ImageCreateFromPNG($url);

// WRITE THE COLORIZED IMAGE
ImageFilter($img, IMG_FILTER_GRAYSCALE);
ImageFilter($img, IMG_FILTER_COLORIZE, $r, $g, $b, $a);
ImagePNG($img, $d . $f);

// SHOW THE DATA WE USED AND THE IMAGES
echo "R=$r, G=$g, B=$b, A=$a";
echo PHP_EOL . '<br clear="all" />';
echo "<img src=\"$url\" />";
echo PHP_EOL . '<br clear="all" />';
echo "<img src=\"$d$s\" />";
echo PHP_EOL . '<br clear="all" />';
echo "<img src=\"$d$f\" />";

Open in new window

color.png
greetings walkman69, ,  I can not get a good Idea of what sort of input and output you might need for a function that does what you say ->
"apply a selected colour to a monochrome image.. Making it blue-ish, or red-ish.. or whatever"

not to sure about what blueish means, I know what it means in speaking, but not when using it as PHP code requirements, LOL;

Anyway, as best I can understand what you said, you might test out the code I post below, it has the  colororize( ) function, which has three parameters, the first is the $file , which is a JPEG image file path on your server that you want to colorize, , , the next is the $Colorish , which I have defined 4 constants -

REDISH
GREENISH
BLUEISH
OLDPHOTO

the next is the $amount, which can be an integer from 5 to 75, there are 3 constants for it -

C_SLIGHT
C_MEDIUM
C_HEAVY

I did not know how to output this function so, for this I have it write a JPEG file to your server,
   WARNING, you must place the file path for your server in the line -
    @imagejpeg($grey, 'images3/addcolor.jpg');

for it to function properly, OR change the way you want it to output the resultant $grey GD image.

There are very many things to consider about image Color manipulations, I hope this will give a result that does what you may need.

Ask questions if you need more info.
<html><head><title>Colorize a Greyscale Image</title></head><BODY bgcolor="#e0f0ff"><h2>Colorize a Greyscale Image test</h2>

<?php

define('REDISH', 0);
define('GREENISH', 1);
define('BLUEISH', 2);
define('OLDPHOTO', 3);

define('C_SLIGHT', 10);
define('C_MEDIUM', 18);
define('C_HEAVY', 33);

function colororize($file, $Colorish = OLDPHOTO, $amount = C_MEDIUM) {
if (!$grey = imagecreatefromjpeg($file)) return false;
$width = imagesx($grey);
$height = imagesy($grey);

$image=imagecreatetruecolor($width, $height);
switch ($Colorish) {
	case 0: $cish = imagecolorallocate($image, 242, 8, 0); break;
	case 1: $cish = imagecolorallocate($image, 10, 240, 0); break;
	case 2: $cish = imagecolorallocate($image, 0, 10, 240); break;
	default: $cish = imagecolorallocate($image, 238, 163, 10); break;
	}

imagefilledrectangle($image, 0, 0, $width, $height, $cish);
if ($amount < 5) $amount = 5;
if ($amount > 75) $amount = 75;
imagecopymerge($grey, $image, 0, 0, 0, 0, $width, $height, $amount);
@imagejpeg($grey, 'images3/addcolor.jpg');
    // WARNING this only outputs a server file write of a JPEG FILE
@imagedestroy($grey);
@imagedestroy($image);
return true;
}

$imageFile = 'images3/greyscale.jpg';
if (!colororize($imageFile, BLUEISH, C_HEAVY)) echo 'ERROR - File does NOT exist or is NOT a JPEG image';


?>

<p><img src="images3/addcolor.jpg"></p>
</body></html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of walkman69
walkman69

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of walkman69
walkman69

ASKER

Slick812:: It doesn't neccessarily have to be an monochrome image.
That was just a bad way of going about things that I thought of late yesterday.

Thanks however for writing that nice code. However i'm not sure it's what i'm looking for,
I'll have a closer look at it in the morning though.
i didn't fint any way to make it more exactly like the Photoshop Hue effect, but it is close enough.