?
Solved

Changing Hue in a image dynamically using GD

Posted on 2008-11-16
4
Medium Priority
?
1,003 Views
Last Modified: 2012-05-05
Is it possible to change the hue in a image from a location (image/logo.png) and resave to another location?
0
Comment
Question by:Sam Cohen
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:cr4ck3rj4ck
ID: 22973832
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
 
LVL 8

Author Comment

by:Sam Cohen
ID: 22974113
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
 
LVL 6

Accepted Solution

by:
cr4ck3rj4ck earned 900 total points
ID: 22974219
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
 
LVL 8

Author Comment

by:Sam Cohen
ID: 23010747
One more question. Where would i add the values, inorder to change the color?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question