Changing Hue in a image dynamically using GD

Is it possible to change the hue in a image from a location (image/logo.png) and resave to another location?
LVL 8
Sam CohenConsultant to Digital/DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cr4ck3rj4ckCommented:
Hi there,

Having had a little look around, I stumbled across this link:
http://bugs.libgd.org/?do=details&task_id=179

It looks as if changing the hue in the GD Library is not directly catered for. An alternative suggestion I saw was to use the ImageMagick library:
http://uk.php.net/manual/en/book.imagick.php

Hope that helps,
CJ
0
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
OK THIS LINK
 http://php.happycodings.com/Graphics/code32.html
how can i use this with a common image located in the image folder?
0
cr4ck3rj4ckCommented:
Ok, the code from that page is shown below.

As far as my knowledge goes, you'd have to change 2 things on the following line:
$im = imagecreatefrompng("b2.png") or die ("Cannot Initialize new GD image stream");

You'd need to change imagecreatefrompng to suit your image format i.e. imagecreatefromjpeg. You'd also need to change the filepath to your image i.e. imagecreatefromjpeg("/images/example.jpg").

If you want to save it, you need to change the following line:
imagejpeg($im2,"",80);

and insert a file path like so:
imagejpeg($im2,"/.images/example2.jpg",80);

Hope that helps,
CJ
<?
 
function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
}
 
$ar=array(0,0,0,0);
srand(make_seed());
$onetwoorthree=rand(0,2);
$first=rand(0,100)/100;
$second=1-$first;
 
$ar[$onetwoorthree]=$first;
$ar[$onetwoorthree+1]=$second;
if ($onetwoorthree+1>2)
	$ar[0]=$second;
//echo " ".$ar[0]." ".$ar[1]." ".$ar[2];
 
$im = imagecreatefrompng("b2.png") or die ("Cannot Initialize new GD image stream");
 
header ("Content-type: image/jpeg");
error_reporting(0);
$height=imagesy($im);
$width=imagesx($im);
$im2=imagecreatetruecolor($width,$height);
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;
 
		$r2=$r;
		$g2=$g;
		$b2=$b;
 
		$r=$r2*$ar[0]+$g2*$ar[1]+$b2*$ar[2];
		$g=$r2*$ar[2]+$g2*$ar[0]+$b2*$ar[1];
		$b=$r2*$ar[1]+$g2*$ar[2]+$b2*$ar[0];
 
		$color = imagecolorallocate($im2, $r, $g, $b);
 
		imagesetpixel($im2,$x,$y,$color);
	}
}
imagejpeg($im2,"",80);
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
One more question. Where would i add the values, inorder to change the color?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.