PDA

View Full Version : HELP! Need different images to load on a single page depending on URL or variables!!!


megahertzman
10-20-2002, 03:25 AM
Hi. I am looking for a script (or whatever does the job), either in PHP, JavaScript or DHTML, or whatever. Most likely PHP would do I guess.
I want one and only one page to be created from the script.
And there will be one main image on the page in the center. And
in that spot where the image appears, different images can appear there instead, depending on the way it is referenced.
For instance:
If I put a link like this on my site:
mydomain.com/images.html?image=pic1
...then pic1 will be displayed on the outputted HTML page
And if I put a link like this:
mydomain.com/images.html?image=pic2
...then pic2 will be displayed rather than the default image or pic1 on the outputted HTML page.

Get it?
Please reply as soon as you can if you can.
THANKS!!

Oh, and I don't want the images to be loaded from an SQL DB or anything. I want the images to be grabbed from an images folder.

firepages
10-20-2002, 07:36 AM
asking for stuff ASAP! is not the best way to get help here !!!


anyway since its a one liner...


<?
$im='mydomain.com/images/'.$_GET['image'].'.jpg';$ims=getimagesize($im);
?>
<img src="<?=$im;?>" <?=$ims[3];?> >



ok 2 liner

megahertzman
10-20-2002, 08:24 PM
Ok sorry. I changed it.

...Someone gave me this code. Will it work?

<?php
$ID = $_GET['ID'];
print "<img src=$ID.jpg>";
?>

...and on the code you posted... can I eleminate that place where you say getimagesize because I don't need that?

This is how I implemented it:

<?
print "<html><head><title>Test Page</title></head><body>";
$im='pics/'.$_GET['image'].'.jpg';
print "<img src=\"$im\" border =\"0\">";
print "</body></html>";
?>

Now, I have two questions:
What is the default image to appear if I just go directly to
images.php. It will just show a broken link, pointing to the picture ".jpg". I want it to show no picture at all, or maybe just one valid picture for the default.

Also, what if there were a couple hundred images in the directory, and 99% of them were in the *.jpg format, but the remaining happen to be in *.gif format. Then how would I go about selecting the GIF image from a link as such: mydomain.com/images.php?image=100 if image 100 was gif.

I would need some kind of IF/ELSE statement, wouldn't I?

Nightfire
10-20-2002, 11:02 PM
Might not be the best way to do it, but I'd use this

if(!$image){
echo '<img src="defaultimage.jpg">';
}else{
if(file_exists($image.gif)){
echo '<img src="'.$image.'.gif">';
}elseif(file_exists($image.jpg)){
echo '<img src="'.$image.'.jpg">';
}else{
echo '<img src="notfound.jpg">';
}
}

megahertzman
10-20-2002, 11:11 PM
Hmmmmmm.........
it's kind of complicated, but I'll try it and see what happens.

megahertzman
10-21-2002, 01:14 AM
Okay, I tried it. It doesn't seem to work. I think it's because there isn't a function file_exists in PHP.

Nightfire
10-21-2002, 01:22 AM
Make sure that the file_exists($image.jpg) is pointing to your image directory, ie file_exists(images/$image.jpg) then change the path in the image tags. When you say it's not working, what error(s) is it showing?

http://php.net/file_exists

megahertzman
10-21-2002, 01:42 AM
Actually, all the images are in the root, so it should have worked.

This is the code I uploaded:

<?php

if(!$image){
echo '<img src="defaultimage.jpg">';
}else{
if(file_exists($image.gif)){
echo '<img src="'.$image.'.gif">';
}elseif(file_exists($image.jpg)){
echo '<img src="'.$image.'.jpg">';
}else{
echo '<img src="notfound.jpg">';
}
}

?>

When I point to images.php?image=1 AND THERE IS AN IMAGE CALLED 1.jpg, all it shows is what seems to have been an image; it's a broken link with property: <img src=notfound.jpg>
...that means it goes to notfound.jpg no matter what.

megahertzman
10-21-2002, 02:02 AM
Yes! I got it it!

I tried typing some PHP myself and it worked! This is what I did:


<?php
$ID = $_GET['ID'];
if (file_exists($ID.'.jpg')) {
print "<img src=$ID.jpg>";
}
else {
if (file_exists($ID.'.gif')) {
print "<img src=$ID.gif>";
}
else {
print "Cannot find .jpg or .gif file for var $ID";
}
}

?>


Thanks!