Jump to content


Photo

GD Problems


  • Please log in to reply
6 replies to this topic

#1 Beamer

Beamer
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 March 2006 - 12:24 PM

I'm pretty new to GD, but I want to make an image that grabs a hex color code from a 'color' string and puts it in the background of an transparent image. For example, I have a transparent circle with a black border. I decide I like it to be blue so I do circle.php?color=0000ff which results in me having a blue circle with a black border. I have tried doing a script with references from PHP.net but I can't get it to work.

Does anyone here have a script or can create one to what I would like it to do?

Cheers,
- B.

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 12:58 PM

modified from an example on php.net/imagefilledellipse. you'll need to change it again to suit your needs:

// break up color into its RGB parts
$color = $_GET['color'];
$r = hexdec(substr($color,0,2));
$g = hexdec(substr($color,2,2));
$b = hexdec(substr($color,4,2));

// fill the background color white
$bg = imagecolorallocate($image, 255, 255, 255);

// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, $r, $g, $b);

// draw the ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);

hope that helps. i've not tested it but should work or set you on your way...

Cheers
Mark


"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 Beamer

Beamer
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 March 2006 - 09:23 PM

My problem is that the top isn't an image, it's a rectangle button with a bit of transparent pixels which users can choose a color for.

I believe your script is using the ellipse function which was in my example, sorry about that.

EDIT: I get two errors when trying out: color=ff0000

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in test.php on line 9

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in test.php on line 12


#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 10:45 PM

[!--quoteo(post=357100:date=Mar 21 2006, 09:23 PM:name=Beamer)--][div class=\'quotetop\']QUOTE(Beamer @ Mar 21 2006, 09:23 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
EDIT: I get two errors when trying out: color=ff0000

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in test.php on line 9

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in test.php on line 12
[/quote]

did you create the image first? sorry, i didnt include that in my example as i thought you'd already have that bit, but you need to use:

$width = 800; 
$height = 600;

$image = imagecreate($width, $height);
before you use imagecolorallocate, etc. (you can also use 'imagecreatetruecolor' in the same way).
the script will work with any sort of GD drawing, whether it be an ellipse, rectangle, polygon, line, etc. have a look at the manual (http://php.net/imagecreate) for details on getting things set up and also links to all the other functions you may find useful.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 Beamer

Beamer
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 March 2006 - 10:54 PM

Sorry, but I mean the top layer is an external image from the server and I want to put that over a colored background.

#6 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 22 March 2006 - 11:07 AM

[!--quoteo(post=357130:date=Mar 21 2006, 10:54 PM:name=Beamer)--][div class=\'quotetop\']QUOTE(Beamer @ Mar 21 2006, 10:54 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Sorry, but I mean the top layer is an external image from the server and I want to put that over a colored background.
[/quote]

the priciples and practices are exactly the same whatever you want to do, if it is in fact the GD you want/need.

$color = $_GET['color'];
$r = hexdec(substr($color,0,2));
$g = hexdec(substr($color,2,2));
$b = hexdec(substr($color,4,2));

if it's an element you need to change, depending on the $_GET value of 'color', then just insert it where you would normally do it with CSS. for example:

<div style="background-color:#<?php echo $_GET['color']; ?>">
<p>hello world!<p>
</div>

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#7 Beamer

Beamer
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 March 2006 - 02:29 AM

I knew that thanks, but I'm having trouble making the image be put over the background using GD.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users