473,434 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

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

106 100+
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
Apr 8 '10 #1
6 7163
Atli
5,058 Expert 4TB
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:
  1. Retrieve the file name from the URL. You do that by fetching the appropriate element from the "$_GET" array. ($_GET['image'], in your case.)
  2. 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.
  3. 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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1. header('Content-type: image/jpeg'); // For JPEG images.
  2. header('Content-length: ' . filesize($imagePath));
  3. readfile($imagePath);
Apr 8 '10 #2
londres9b
106 100+
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.
Apr 8 '10 #3
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Assuming the following request was sent:
  3. // - example.php?name=Atli&joined=2006-11-25
  4.  
  5. // Fetch the name and prepare it for output
  6. $name = $_GET['name'];
  7. $formatted_name = htmlentities($name);
  8.  
  9. // Fetch the joined date and re-format it.
  10. $joined = $_GET['joined'];
  11. $formatted_date = date("F jS, Y", strtotime($joined));
  12.  
  13. // Print stuff
  14. echo "Hello, {$formatted_name}.\n";
  15. echo "You joined this site on {$formatted_date}.";
  16.  
  17. // This prints:
  18. /*
  19.  * Hello, Atli.
  20.  * You joined this site on November 25th, 2006.
  21.  */
  22. ?>
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 :]
Apr 8 '10 #4
londres9b
106 100+
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.
Apr 9 '10 #5
londres9b
106 100+
How do I display the image on the php file?

I want to use

Expand|Select|Wrap|Line Numbers
  1. echo "<img src=\"{$imagePath}\" alt=\"{$imageName}\">";
How do I edit the code above? I have the

Expand|Select|Wrap|Line Numbers
  1. $image = $_GET['image'];
But how do I make the echo retrieve data from GET?

Thank you
Apr 9 '10 #6
londres9b
106 100+
Don't need to reply!

I got it!

Weee :) I'm so happy!

Will give a url soon

Thanks for all the help!
Apr 9 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Graham J | last post by:
Hello, Apologies for the somewhat wordy and garbled subject as I couldn't think how to phrase it and this has hindered my searching for any previous answers. It could be a really simple...
2
by: Catherine Lynn Wood | last post by:
I need to know how to overlap DIV content within 'relative' associated rendering. I am building div layers in the middle of a page and when I set positioning to absolute in the CSS, it references...
16
by: juglesh | last post by:
Hello, I need to look through the text on a page and replace certain words with an image or other word something like: read document find all instances of the word "blah" change all...
67
by: Sandy.Pittendrigh | last post by:
Here's a question I don't know the answer to: I have a friend who makes very expensive, hand-made bamboo flyrods. He's widely recognized (in the fishing industry) as one of the 3-5 'best' rod...
26
by: Yeah | last post by:
I have a web site which changes the header logo based on the upcoming holiday. For example, from December 10th to 25th, XMAS.JPG is displayed. From October 20th to 31st, HALLWEEN.JPG is...
6
by: Dariusz Tomon | last post by:
Hi How can I get url of page so taht I can pass it to string varaible. I have got several urls in my IIS under one folder. I want to have one default.aspx where code under it recognize which...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
2
by: SR | last post by:
I have started a web site using ASP.NET 2.0. I would like to centralize all of my classes in a StyleSheet but I cannot figure out how to link the StyleSheet to a Content Page since there is no...
1
by: i.sobha | last post by:
Hi, I tried retreiving an image from an access data base and display it to the asp page . The code is shown below. But the same is not working. Can someone provide me with some valuable...
6
by: Jeremy | last post by:
I've got a floating div which becomes visible when a link is clicked. I want the div to be hidden when the user clicks anywhere on the page except for whithin the div. What is the best way to do...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.