[SOLVED] Is This Possible

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
luketheduck
Forum Newbie
Posts: 18
Joined: Mon Apr 19, 2004 9:13 am
Location: Aylesbury, Bucks, UK

Is This Possible

Post by luketheduck »

Something I want to implement on my site, but don't know if it's possible.

The page searches for an image to display based upon 2 variables which are set at page load.

What I want to do is search the directory for the image, but if it doesn't exist, to display a default image instead.

Is this going to be possible? Would be based around an if statement I guess, if image doesn't exist, show xxx instead.
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

Yes this is possible and extremely easy. Do you have an example of how you would use such a thing?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I'm using for this purpose

Code: Select all

<?php
//returns an image
//if no image-file is found, it returns default "blank.gif"
	if (file_exists('graphix/'.$_GET["image"].'.gif')){
		$image="http://www.mydomain.org/graphix/".$_GET["image"].".gif";
	}
	else{
		$image="http://www.mydomain.org/graphix/blank.gif";
	}
	header("Location: $image");
?>
User avatar
luketheduck
Forum Newbie
Posts: 18
Joined: Mon Apr 19, 2004 9:13 am
Location: Aylesbury, Bucks, UK

Post by luketheduck »

Yes that is exactly what I was looking for...

Thanks!
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

I use getimagesize, that way you can format the image.

Assume $img_var is your image filename.

Code: Select all

<?php
if (@$img = getimagesize($img_var))
{
    print "<img src="$img_var" width="$img[0]" height="$img[1]">";
}
else
{
    print "<img src="blank.gif">";
}
?>
Last edited by Grim... on Wed Jun 09, 2004 8:23 am, edited 1 time in total.
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

See How You Are Patrik....

I was gonna make him work for it ;)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

You're right Saethyr - I shouldn't have posted the solution. I am such a spoilsport... ;)

Alas, just after I posted it I thought: "Damn, Saethyr has a point." But then, hey, I did. So, no dessert for me tonight (and it would have been fresh, juicy mango...:()
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Yum.

Everyone round Patrik's house for dinner :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Hey, I am voluntarily chastising myself and that is usually done in solitude. But you can come around after I'm finished chastising ;)
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Sussex is a bit of a train ride, though...

Can you post me the dinner?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

We're talking dessert, not dinner. Dinner is mine :)

there ya go:
Image

Bon appetit.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Ah, the techincal revolution.
And here's me expecting the postman to deliver it :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I can PM it to you, if you like...
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

:P
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

hehe, go ahead and get dessert THIS TIME! ;)

And I am thinking the 17 hour plane ride to Patrik's would spoil my appetite.
Post Reply