![]() |
|
|||||||
| Notices |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Guest
Posts: n/a
|
newbie question - variable data types?
I'm sure the solution to this is very simple and I'm just overlooking
something. I'm using the following code to color an image: $thishex= 0xFF0000; $red = imagecolorallocate($im, 0, 0, $thishex); That works perfect. The problem is, the hex color is coming in the URL string, without the preceding "0x". To remedy this, I tried: $hex1 = "0x"; $hex2 = "FF0000"; $thishex = $hex1 . $hex2; $red = imagecolorallocate($im, 0, 0, $thishex); This does not work, despite the fact that it echos 0xFF0000. As a flash developer, I have run up against this problem before when I don't use strict data types. As a php newbie, I am not sure that there is a similar phenomenon in PHP. Anybody got the answer to this one? I'm sure it's very simple. |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Re: newbie question - variable data types?
Nerd Monster wrote:
> I'm sure the solution to this is very simple and I'm just overlooking > something. I'm using the following code to color an image: > > $thishex= 0xFF0000; > $red = imagecolorallocate($im, 0, 0, $thishex); > > That works perfect. The problem is, the hex color is coming in the URL > string, without the preceding "0x". To remedy this, I tried: > > $hex1 = "0x"; > $hex2 = "FF0000"; > $thishex = $hex1 . $hex2; > $red = imagecolorallocate($im, 0, 0, $thishex); > > This does not work, despite the fact that it echos 0xFF0000. As a flash > developer, I have run up against this problem before when I don't use strict > data types. As a php newbie, I am not sure that there is a similar > phenomenon in PHP. > > Anybody got the answer to this one? I'm sure it's very simple. > > > > > Two problems: 1) 0xFF0000 = 16711680 (not 255 like you want) 2) Your second example is using a string (which equates to 0), not a hexadecimal value. As long as $hex is a string (as it should be coming in as GET/POST/REQUEST: This will work with or without adding '0x', $red = imagecolorallocate($im, 0, 0, base_convert($hex, 16, 10)/512/128)) This will only work if you add '0x' to it: $red = imagecolorallocate($im, 0, 0, ($hex/512/128)) Norm |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Re: newbie question - variable data types?
> Two problems: > > 1) 0xFF0000 = 16711680 (not 255 like you want) > 2) Your second example is using a string (which equates to 0), not a > hexadecimal value. > > As long as $hex is a string (as it should be coming in as > GET/POST/REQUEST: > > This will work with or without adding '0x', > > $red = imagecolorallocate($im, 0, 0, base_convert($hex, 16, 10)/512/128)) > > This will only work if you add '0x' to it: > > $red = imagecolorallocate($im, 0, 0, ($hex/512/128)) > > > Norm Thanks Norm! Here's the problem, and you can test these complete examples on your own machine to verify. The following works correctly: ----- <?php $im = imagecreatetruecolor(100, 100); $fillcolor = imagecolorallocate($im, 0, 0, 0x00FF00); imagefill($im, 0, 0, $fillcolor); header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?> ----- Using your example, the following does not work (assuming you pass $urlcolor in the url): ------ <?php $im = imagecreatetruecolor(100, 100); $fillcolor = imagecolorallocate($im, 0, 0, base_convert($_GET["urlcolor"], 16, 10)/512/128); imagefill($im, 0, 0, $fillcolor); header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?> ------ Any clues? |
|
|
|
#4 |
|
Guest
Posts: n/a
|
Re: newbie question - variable data types?
Nerd Monster wrote:
>> Two problems: >> >> 1) 0xFF0000 = 16711680 (not 255 like you want) >> 2) Your second example is using a string (which equates to 0), not a >> hexadecimal value. >> >> As long as $hex is a string (as it should be coming in as >> GET/POST/REQUEST: >> >> This will work with or without adding '0x', >> >> $red = imagecolorallocate($im, 0, 0, base_convert($hex, 16, 10)/512/128)) >> >> This will only work if you add '0x' to it: >> >> $red = imagecolorallocate($im, 0, 0, ($hex/512/128)) >> >> >> Norm > > Thanks Norm! Here's the problem, and you can test these complete examples on > your own machine to verify. > The following works correctly: > ----- > <?php > $im = imagecreatetruecolor(100, 100); > > $fillcolor = imagecolorallocate($im, 0, 0, 0x00FF00); > imagefill($im, 0, 0, $fillcolor); > > header('Content-type: image/png'); > imagepng($im); > imagedestroy($im); > ?> > ----- > > Using your example, the following does not work (assuming you pass $urlcolor > in the url): > > ------ > <?php > $im = imagecreatetruecolor(100, 100); > > $fillcolor = imagecolorallocate($im, 0, 0, base_convert($_GET["urlcolor"], > 16, 10)/512/128); > imagefill($im, 0, 0, $fillcolor); > > header('Content-type: image/png'); > imagepng($im); > imagedestroy($im); > ?> > ------ > > Any clues? > > > > Ok, you switched your variable on me. ![]() You need to: if (baseconvert(...) > 512) { divide by 512; } if (new value > 128) { divide again by 128 ) something like that... The hex numbers your passing in are too big. Must be 0-255 (0x00 - 0xFF) Norm |
|
|
|
#5 |
|
Guest
Posts: n/a
|
Re: newbie question - variable data types?
..oO(Nerd Monster)
>Thanks Norm! Here's the problem, and you can test these complete examples on >your own machine to verify. >The following works correctly: But it is not correct! >----- ><?php >$im = imagecreatetruecolor(100, 100); > >$fillcolor = imagecolorallocate($im, 0, 0, 0x00FF00); As said, the numbers passed to imagecolorallocate() have to be 0..255. To split a given hex string into its color parts, you could use string functions or something like this: function imageColorAllocateHex($im, $color) { $c = hexdec($color); return imageColorAllocate($im, 0xFF & $c >> 0x10, 0xFF & $c >> 0x8, 0xFF & $c ); } Usage: $color = imageColorAllocateHex($im, '00FF00'); Micha |
|
|
|
#6 |
|
Guest
Posts: n/a
|
Re: newbie question - variable data types?
Michael Fesser wrote:
> .oO(Nerd Monster) > >> Thanks Norm! Here's the problem, and you can test these complete examples on >> your own machine to verify. >> The following works correctly: > > But it is not correct! > >> ----- >> <?php >> $im = imagecreatetruecolor(100, 100); >> >> $fillcolor = imagecolorallocate($im, 0, 0, 0x00FF00); > > As said, the numbers passed to imagecolorallocate() have to be 0..255. > > To split a given hex string into its color parts, you could use string > functions or something like this: > > function imageColorAllocateHex($im, $color) { > $c = hexdec($color); > return imageColorAllocate($im, > 0xFF & $c >> 0x10, > 0xFF & $c >> 0x8, > 0xFF & $c > ); > } > > Usage: > > $color = imageColorAllocateHex($im, '00FF00'); > > Micha Thanks Micha, I can't believe what a horrible post I gave... yuck! I was five minutes to work when I realized how bad it was. Norm |
|
|
|
#7 |
|
Guest
Posts: n/a
|
Re: newbie question - variable data types?
"Norman Peelman" <.com> wrote in message news:46dd7b2b$0$15366$... > Michael Fesser wrote: >> .oO(Nerd Monster) >> >>> Thanks Norm! Here's the problem, and you can test these complete >>> examples on your own machine to verify. >>> The following works correctly: >> >> But it is not correct! >> >>> ----- >>> <?php >>> $im = imagecreatetruecolor(100, 100); >>> >>> $fillcolor = imagecolorallocate($im, 0, 0, 0x00FF00); >> >> As said, the numbers passed to imagecolorallocate() have to be 0..255. >> >> To split a given hex string into its color parts, you could use string >> functions or something like this: >> >> function imageColorAllocateHex($im, $color) { >> $c = hexdec($color); >> return imageColorAllocate($im, >> 0xFF & $c >> 0x10, >> 0xFF & $c >> 0x8, >> 0xFF & $c >> ); >> } >> >> Usage: >> >> $color = imageColorAllocateHex($im, '00FF00'); >> >> Micha > > Thanks Micha, I can't believe what a horrible post I gave... yuck! I was > five minutes to work when I realized how bad it was. > > Norm Thanks to both of you for the help! I've been working on a colorpicker wysiwyg using Flash with remoting to choose the colors and then php to process the resulting images. I plan to post my source files here and on sourceforge when I'm done. Thanks again! |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Newbie question | John Peach | PHP | 3 | 05-09-2007 11:30 AM |
| Question on Template Functionality for Variable Text Length | Monsmord | Microsoft Office | 0 | 28-08-2007 01:49 PM |
| Question on Template Functionality for Variable Text Length | Monsmord | Microsoft Office | 0 | 28-08-2007 01:48 PM |
| REG_SZ vs REG_EXPAND_SZ data types for %SystemRoot% in registry | FUBARinSFO | Windows XP | 10 | 16-08-2007 11:02 PM |
| Help Question: What are the types of Mainboards, VGA, and Harddisks? | ab_6421 | Technical Discussions | 3 | 28-11-2006 06:38 PM |
< Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |