Probably a Silly Code Error

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am trying to use the below chunk of code to control the height and
width of displayed images, based on the users screen height.

This works if the $baseimage is set to 800 as shown.  The echo lines
display 1024800, as expected, on the web page and the width= and height
= lines set the displayed image size as desired.

If I try to use $baseimage = $displayheight instead (Which is what I
want) then the two echo lines dispay 10241024, as expected, on the web
page.  But it then sets width=0 and height=0 for the image, so the image
is not displayed.

The echo always displays 1024 on the screen, thus I assume that it is
not a conflict between the Javascript and PHP stuff since that does seem
to indicate the page knows what the height is, it just does not want to
use the number in the calculations.

If I omit all $baseimage stuff and in the calculations section use
$displayheight instead of $baseimage I have same problems as above.

If I add additional lines to check for is_numeric, then everything
indicates that $displayheight and $baseimage are both numeric.

The $picturespecs array comes from elsewhere and is good (Example 600,
800) since it works if I preset $baseimage to 800.

A bit lengthy, sorry, just trying to include anything possibly
pertinent.  Could someone tell me where I have gone wrong here.
Quoted text here. Click to load it
// xxxxxxxx Get the screen width and set base image size xxx
$displayheight = "<SCRIPT language=\"javascript\">\n var res =
screen.height;\n document.write(res);\n </SCRIPT>";
$baseimage = 800;
// $baseimage = $displayheight;
echo $displayheight;
echo $baseimage;

// xxxxxxxxx Calculate desired image height and width xxxxx
If ($picturespecs[0] > $picturespecs[1])
    {$percentage = ($baseimage / $picturespecs[0]);}
    {$percentage = ($baseimage / $picturespecs[1]);}
$width = round($picturespecs[0] * $percentage);
$height = round($picturespecs[1] * $percentage);
$imagesize = "width=" . $width . " height=" . $height;

Re: Probably a Silly Code Error

While the city slept, Marvin ( feverishly typed...

Quoted text here. Click to load it

A pretty futile exercise, as the user's screen size will not tell you the
available canvas area of their browser. For example, my display is set to
1152 x 864, but I very rarely maximise my browser. Even if you could assume
that everyone had their browser maximised, how would you account for the
different quantities of toolbars etc that people may or may not be using?


Nigel Moss
Mail address not valid., take the DOG. out!
In the land of the blind, the one-eyed man is very, very busy!

Re: Probably a Silly Code Error

Quoted text here. Click to load it

Well, once it worked I intended to decrease the numbers by 20% or some such
number to allow for some of the above issues.  I don't expect a perfect fit
for everybody (Or anybody) but for my intended purposes it is not really a
futile exercise so if anybody has a solution I would appreciate it.

Re: Probably a Silly Code Error

Quoted text here. Click to load it

Do you want help working out the algorithm for calculating the height/width
proportional to a given screen size, or do you want help implementing a
known algorithm.

I ask because you can't use javascript to tell php what size to use in an
IMG tag. If you want the algorithm analyzed, we can help here. If you want
the process to work (resizing the IMG on the fly) then you need to visit a
javascript group. I think you can modify the size of the image via
javascript. Once you know how to do that, then we can help you emit that
javascript, generating it dynamically to create what you need.

 - Virgil

Re: Probably a Silly Code Error

Marvin wrote:
Quoted text here. Click to load it

A) this won't work, you won't get what you want, use Javascript to resize on
the fly if that's what you need.Don't try this JS and PHP mix for this.
B) Resized pics look crummy most of the time. Save some different sizes (use
photoshop or irfanview or whatever) of each picture and have JScript pick
the one closest to screen size. But as mentioned, many have their browsers
set to other than full size and your script fails there. It's been tried a
lot, and mostly it's futile. Without proper indication of the true browser
size you're lost.


Site Timeline