How to make a page display a different image based on url ? – PHP

  php

Q(Question):

Let’s say I have a page called page.php. And let’s also say I have an images folder.
How can I make the php page display the images on the folder?

But that’s not the whole thing… What I really want is to be able to change the image based on the url.
Like, for e.g. www.example.com/page.php?image=1.jpg

My images are like this 1, 2, 3… so I would do something like this:
www.example.com/page.php?image=2.jpg
www.example.com/page.php?image=3.jpg

(I put the extension of the images on the url but I don’t know if it is needed)

I’m a php begginer so if you could, please give me full source code examples….

Can you help me plz? I’ve searched and searched without any luck.

Thanks,

Antonio Azevedo

A(Answer):

Hey.

please give me full source code examples

Sorry, we don’t do that. We are happy to help you create your code, but we won’t write it for you.
With that said…

Do you want to have PHP display the image as a part of a HTML page, or do you want the PHP page to become the image?

Either way, you would start by doing the following:Retrieve the file name from the URL. You do that by fetching the appropriate element from the "$_GET" array. ($_GET[‘image’], in your case.) Make sure the image name is valid. Meaning; it is just the name of an image, not the path to some arbitrary file on your system (like /etc/passwd…)
A simple way to do that is to use the basename function on the file name. This strips a way all but the end of the path; the file name and it’s extension. Finally you would want to make sure the image actually exists, by running it through the file_exists function. This is important, because if you try to display an image that doesn’t exists, it will obviously not work.
After that you display it. If you just want to include it in a HTML page, you simply echo the <img> tag:

echo "<img src=\"{$imagePath}\" alt=\"{$imageName}\">";

If you want PHP to become the image, you need to set the Content-type and Content-length headers, and then print the image data.

header('Content-type: image/jpeg'); // For JPEG images.
header('Content-length: ' . filesize($imagePath));
readfile($imagePath);

A(Answer):

Hy

I’m sorry, but you didn’t quote the whole phrase.. I sad "if you could…"

Thank you for your quick reply.

It’s just that I’m so a beginner on php that I don’t know how to implement the things you say….

Now, to answer you, I want to

1- Display the image on the php file (as part of), and then be able to change it using the page’s url;

OR

2- Make the php become the image, like u said

Either way will do. I don’t think it is possible to do so with HTML is it?
I mean, I know we can use php on html, but if I did that, I wouldn’t be able to change the image using the url, or would I?

Could you explain $_GET[‘image’] to me?
I mean, how do I use it? I’ve searched but I can’t understand

Thank you.

A(Answer):

Could you explain $_GET[‘image’] to me?
I mean, how do I use it? I’ve searched but I can’t understand

The $_GET array is a super-global containing all the key-pair values that are in the URL. – For your example.com?image=1.jpg, PHP would put one element into the $_GET array, named "image" with the value "1.jpg".

For example, you could do something like this:

<?php
// Assuming the following request was sent:
// - example.php?name=Atli&joined=2006-11-25
// Fetch the name and prepare it for output
$name = $_GET['name'];
$formatted_name = htmlentities($name);
// Fetch the joined date and re-format it.
$joined = $_GET['joined'];
$formatted_date = date("F jS, Y", strtotime($joined));
// Print stuff
echo "Hello, {$formatted_name}.\n";
echo "You joined this site on {$formatted_date}.";
// This prints:
/*
* Hello, Atli.
* You joined this site on November 25th, 2006.
*/
?>

See the manual entry for more details on the $_GET super-global.

Either way will do. I don’t think it is possible to do so with HTML is it?
I mean, I know we can use php on html, but if I did that, I wouldn’t be able to change the image using the url, or would I?

It doesn’t really matter how you choose to display the image, changing the URL (and thus; re-submitting the request) will always re-call the PHP script and refresh the response. – PHP works on the server-side, and it doesn’t really care which type of response you send. It just executes, sends the headers and content you specify, and stops until a new request is sent.

You can choose to have PHP output a normal HTML page and include the image in a <img> tag, or you can have PHP imitate the image itself. That is; you can have PHP "trick" the browser into thinking it is a normal image, and have it display it as such.

The HTML method is easier for you to control. It allows you to tell the browser how to display the image. The other method just sends the image and lets the browser deal with how it should be handled; whether it should be displayed or saved.

Either way, you should start by just getting PHP to fetch the image name from the URL and print it. Once you’ve gotten that to work, you can use either of the snippets I posted in my first post to send the actual image.

I’m sorry, but you didn’t quote the whole phrase.. I sad "if you could…"

No worries. I was just explaining the situation. – We get a lot of people in here trying to get us to do their assignments for them, so they can just copy/paste them and hand them in to their bosses/teachers, so we have a strict policy against providing full-code solutions. – Not that I’m accusing you of being one of them. The same rules just have to apply to everybody :]

A(Answer):

No worries. I was just explaining the situation.

Ok, I understand perfectly.

Thanks for the good detailed explanation! I understand much better now.

I’ll try to write the code and let you know if I have any problems.

A(Answer):

How do I display the image on the php file?

I want to use

echo "<img src=\"{$imagePath}\" alt=\"{$imageName}\">";

How do I edit the code above? I have the

$image = $_GET['image'];

But how do I make the echo retrieve data from GET?

Thank you

A(Answer):

Don’t need to reply!

I got it!

Weee 🙂 I’m so happy!

Will give a url soon

Thanks for all the help!

LEAVE A COMMENT