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

walkman69
walkman69 used Ask the Experts™
on
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!

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Can you give us a definition or reference about HUI?  Thanks, ~Ray
Most Valuable Expert 2011
Top Expert 2016

Commented:
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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Most Valuable Expert 2011
Top Expert 2016

Commented:
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

I'm sorry I didn't specify what i was looking for..
I think I might not have known what it was I was looking for.
But it struck me that it's the ability to recreate the photoshop-HUE effect that i'm looking for.

I combined your code Ray with some more HUE-related code on PHP.net:
http://php.net/manual/en/function.imagefilter.php (the post by: mail at kavisiegel dot com 25-Jun-2009 06:11)

And did some research about alpha-channels in PHP and modified it to handle alpha-channels as well. But it's still not 100% what i'm looking for. Even though it's way better than just adding colorize, since colorize won't create the effect i'm looking for.

This is the code wich i modified so far, but it still needs to function more like the Photoshop HUE-function.

The PHP.net programmer also included an image of how his HUE-function makes a picture look:
http://img146.imageshack.us/img146/3167/imagefilterhuedemo.png

But I still want to get closer to Photoshop..

Isn't there some place on the internet where you can find descriptions of the inner workings of Photoshop HUE - tool ?

<?php

function imagefilterhue($im,$r,$g,$b){
    $rgb = $r+$g+$b;
    $col = array($r/$rgb,$b/$rgb,$g/$rgb);
    $height = imagesy($im);
    $width = imagesx($im);
    for($x=0; $x<$width; $x++){
        for($y=0; $y<$height; $y++){
            $rgb = ImageColorAt($im, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
			$a = ($rgb & 0x7F000000) >> 24;
            $newR = $r*$col[0] + $g*$col[1] + $b*$col[2];
            $newG = $r*$col[2] + $g*$col[0] + $b*$col[1];
            $newB = $r*$col[1] + $g*$col[2] + $b*$col[0];
			
            //imagesetpixel($im, $x, $y,imagecolorallocate($im, $newR, $newG, $newB));
			imagesetpixel($im, $x, $y,imagecolorallocatealpha($im, $newR, $newG, $newB, $a));
			
        }
    }
}


// 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 = 'images/';

// LOCATION OF A COLOR IMAGE
$url = $d . 'color3.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);

imagealphablending($img, true); // setting alpha blending on
imagesavealpha($img, true); // save alphablending setting (important)

// WRITE THE COLORIZED IMAGE
/*ImageFilter($img, IMG_FILTER_GRAYSCALE);*/

// To set hue to 1,1,1 is the same as not setting any hue at all.

imagefilterhue($img,50,50,50);

/*ImageFilter($img, IMG_FILTER_COLORIZE, $r, $g, $b, $a);*/

ImagePNG($img, $d . $f);

// SHOW THE DATA WE USED AND THE IMAGES

//echo "<body style=\"background-color:black;\"></body>";
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

Author

Commented:
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.

Author

Commented:
i didn't fint any way to make it more exactly like the Photoshop Hue effect, but it is close enough.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial