473,659 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

106 New Member
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 7233
Atli
5,058 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
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 New Member
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 New Member
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
1847
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 question. Anyway... I have a simple javascript function that opens up a new window and writes HTML code in it to display an image (I don't just display the image because of problems with different margins in different browsers). Nothing exotic there. ...
2
12021
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 back to 0,0 on the entire page. If I set it to relative, the div layers will not overlap as needed. I prefer to avoid javascripting an 'innerHTML' re-write of a single div and would instead like to build two layers that can reside at the same...
16
12361
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 instances of the word "blah" to <img src="MyPicture.jpg" >
67
5998
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 makers in the world. He gets (sic) close to $5000 per custom made flyrod. A surprising number of people buy these fishing rods and never use them....they buy them as art-like investments. He is, after all, the best there is. But if you search on...
26
2429
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 displayed. Etc. etc. If today's date is not near a holiday, then the default LOGO.JPG is displayed. A while back, I was looking for a JavaScript that does this automagically. But each snippet I found traditionally displays the default logo for a...
6
29643
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 url is used by user and then display appropriate data to that url (one url is about fashion, other about law and so on). By means of recognition of url I can display appropriate banners for example.
6
4868
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
2
6979
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 header. I tried to put the link tag in the Master page, but the classes are not recognized in the Content Page. How do I use a StyleSheet with the Content Page? TIA
1
1945
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 inputs. Save the belo code in an asp ShowPicture.asp
6
2511
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 this? Really, I only need to support ie6+ but cross browser is always nice.
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.