Forum Moderators: coopster

Message Too Old, No Replies

simple image display script

call info from link URL

         

sssweb

2:24 pm on Feb 1, 2006 (gmt 0)

10+ Year Member



I need a simple script that will display different images based on info sent to it from a link on another page. For example:

file1.htm contains the link: <a href="/picture-show.htm?image1.jpg">Pic1</a>

file2.htm contains the link: <a href="/picture-show.htm?image2.jpg">Pic2</a>

etc.

The script would be on picture-show.htm, and display the image requested via the link info. The pics will be in different directories, so the script must be able to handle sub-directories in the link:

<a href="/picture-show.htm?images/folder1/image3.jpg">Pic3</a>

coopster

10:50 pm on Feb 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I may be missing something here, but how do you plan on building the urls in the first place? Or is that what you are wondering how to do?

sssweb

2:42 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



The link URL's will be set by me in the HTML pages that point to the image displayer. I already have a javascript to do that. I just need the page/script that displays the images.

Basically, this is a thumbnail viewer that points to a full-size image displayer. As I say, I already have a script that does everything, except that it's set up to point to a unique URL for each full-size image. This means I have to copy my image display template page over & over for each image (plus edit the filename for the image that is to display). It also means that the user's browser must load a new display page for every image, which adds to download time. A single dynamic display page only has to load once, and the only new content is the pic.

(No, I don't want to just point to the image URL itself; I want it to display on a template page with other content.)

I don't know PHP, but I thought this would be a simple script. Here's all it has to do:

[ Assume our display page is named pic-display.php, and a typical link it may receive is: /pic-display.php?/images/red/pic1.jpg ]

1) Read the appended value on the link, identifying the image: /images/red/pic1.jpg

2) Print the image via a template (say, pic-display.tpl)

That's it. I've already got the template -- all I would need is the variable name used by the PHP script, e.g. {IMAGE}.

sssweb

3:23 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



Or maybe I don't need a separate PHP page at all; maybe I can just insert a short PHP script into an HTML template page, and point the URL's there.

coopster

6:03 pm on Feb 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Then all you would need to do, if I understand this correctly, is accept that GET QUERY_STRING parm in your receiving PHP script ...
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) { 
// You should likely do some editing here
// to make sure the query string is legit,
// security first you know ;-)
if (the QUERY_STRING value looks legit) {
$myImage = $_SERVER['QUERY_STRING'];
}
} else {
// Some kind of default pic or something
// for when the QUERY_STRING isn't there
}

... and put it into your templates <img> src attribute:
<img src="<?php print $myImage; ?>" />

sssweb

8:25 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



Thanks for your help; I tried what you said a couple of ways but couldn't get it to work. Some questions:

Do you mean to insert your PHP code & variable string into my HTML template?

I tried that; the page loads and displays fine, but no image shows. Here's what I added to my HTML (I ignored the security issue for now; just want to get it working first):

[in the <head> section]:

<?php
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}
?>

[in the <body> section]:

<img src="<?php print $myImage;?>" />

==========

I couldn't get it to work in separate files either. First I just used the above PHP code in a file named picviewer.php, and made a template (pic-show.tpl) with the variable $myImage in it.

When I went to the URL: [mysite.com...] I got a blank screen -- obviously we need to call up the template (which you probably assumed I'd know how to do!) I don't know PHP, but here's what I pieced together from a working PHP part of my website:

[picviewer.php]

<?php
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}

// Load template
$template->set_filenames(array(
'body' => 'pic-show.tpl')
);

// Send variable to template
$template->assign_vars(array(
'IMAGE' => $myImage
)
);

// Display template
$template->pparse('body');

?>

When I went to the URL as before, I got this error message:

Fatal error: Call to a member function on a non-object in this line - $template->set_filenames(array(

That's about as far as I can go on my own.

coopster

8:49 pm on Feb 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Try viewing the source code of the generated HTML page and see if the <img> element is present and if so, what is in the 'src' attribute? Is it the path to your image as you expected?

sssweb

10:56 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



I assume you mean when using the separate PHP file method (otherwise the source code shows exactly as I coded it: <img src="<?php print $myImage;?>" /> )

I don't know what the source shows for the image src in the separate file method either, because I'm getting the error message that I indicated; when I go to view source, all that's there is <br />, then the error message.

From the error message, something's wrong with my line:

$template->set_filenames(array(
'IMAGE' => $myImage
)
);

I pulled that from a file that sends multiple variables to the template, hence the 'array'. I tried simply deleting the array reference (and associated parentheses); that seemed to take care of that issue, but then I got this error message:

Parse error: parse error, unexpected T_DOUBLE_ARROW in the next line:

'IMAGE' => $myImage

[that whole code section reads as follows]:

// Send variable to template
$template->assign_vars(
'IMAGE' => $myImage
);

coopster

3:22 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think you need to start dumping the variable out at certain stages of your process then to see what it contains and why your template routines are choking on it. By dumping the variable out, what I mean is to print it to the browser and exit the script so you can see what value it contains. Then take a look at the next step in the process to determine why your code is not liking what it is getting.

There are a couple of ways to dump variables, one of which is var_dump() [php.net]. Another that comes in handy when dumping arrays is print_r() [php.net]. I think you'll find both much easier to read if you enclose them in html <pre> elements when you dump them. Example:

<?php 
print '<pre>';
var_dump($myImage);
print '</pre>';
exit;
?>

sssweb

4:35 pm on Feb 4, 2006 (gmt 0)

10+ Year Member



Okay, I did what you said at every stage of the script; here are the results:

After:

if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}

Result:

'string(29)' then the CORRECT IMAGE URL in quotes "" -- (yeah!)

After:

// Load template
$template->set_filenames(array(
'body' => 'pic-show.tpl')
);

Result:

Fatal error: Call to a member function on a non-object in this line: $template->set_filenames(array(

That's the same error I got before. When I deleted the array reference, I got this error:

Parse error: parse error, unexpected T_DOUBLE_ARROW in this line: 'body' => 'pic-show.tpl'

In the PHP manual, it says the T_DOUBLE_ARROW applies to arrays, so I tried a simple = sign. That gave a similar error: parse error, unexpected =

So this is the section that's hanging things up:

// Load template
$template->set_filenames(array(
'body' => 'pic-show.tpl')
);

I assume I don't need the array reference in there, but I don't know how to code it without that.

coopster

5:22 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Me neither ;)

It's because of the templating system you are using. You need to have a look at that method (that function in your template class) to see what it is expecting to be passed and format your code accordingly.

sssweb

6:13 pm on Feb 4, 2006 (gmt 0)

10+ Year Member



I simply borrowed that template code from my phpbb forum. Is there a simple, generic templating format I can use instead?

Barring that, I got the whole thing to work a different way, but I'm not sure if this is good design practice; let me know. Here's what I did:

I have one file, called pic-show.php. It opens with your code:

<?php
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}

I then added:

print("

Then I pasted my entire HTML code, and after that, closed the whole page with:

") // to close the print() function
?>

I also escaped all quotation marks " in the HTML using \"

For the image variable, I simply used:

<img src=\"$myImage\">

If this is an acceptable way of doing things, I only have one glitch. This page will open in a new window when the user clicks on a thumbnail. In the HTML, I have a simple button that the user clicks to close the window (to return to the thumbnails). The code is (without the backslash escapes):

<input type="button" value="Close Window" onClick="window.close()"></form>

Now, in the PHP script, when you click the button, Internet Explorer comes up with an alert message that says, "The web page you are viewing is trying to close the window. Do you want to close the window?"

Is there a php command I can use instead to avoid that annoying message each time?

Also, now that I have a working script, I'd like to take care of any security issues. My image directories are already protected, and the true image URL's are disguised to the viewer. If there are other security issues, please give me a quick rundown or point me to a place I can read up on them.

Thanks.

sssweb

5:17 pm on Feb 6, 2006 (gmt 0)

10+ Year Member



I learned that I can simply use URL links like this:

<a href="/picviewer.php?image=/pic1.gif">Pic1</a>

and picviewer.php will automatically assign

$image = /pic1.gif

Is this better than using:

if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}

It seems simpler. Is the security issue the same, less, or more?