TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > PHP

Notices

newbie question - variable data types?

PHP


Reply
 
Thread Tools Display Modes
Old 05-09-2007, 11:31 AM   #1
Nerd Monster
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.





  Reply With Quote
Old 05-09-2007, 11:31 AM   #2
Norman Peelman
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
  Reply With Quote
Old 05-09-2007, 11:31 AM   #3
Nerd Monster
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?




  Reply With Quote
Old 05-09-2007, 11:31 AM   #4
Norman Peelman
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
  Reply With Quote
Old 05-09-2007, 11:31 AM   #5
Michael Fesser
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
  Reply With Quote
Old 05-09-2007, 11:31 AM   #6
Norman Peelman
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
  Reply With Quote
Old 07-09-2007, 09:32 PM   #7
Nerd Monster
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!


  Reply With Quote
Reply

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?

All times are GMT +5.5. The time now is 03:56 PM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional