Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   File to Display image (http://www.programmingforums.org/showthread.php?t=12306)

PhilBon Jan 4th, 2007 12:37 PM

File to Display image
 
So right now I'm working on some php so I can have <img src="http://www.philbon.com/getimage.php?id=3" /> that it will display the image based on the id. So far I have
[PHP]<?
$id = $_REQUEST['id'];
header('Content-Type: image/jpg');
echo file_get_contents("http://www.philbon.com/pictures/" . $id . ".jpg");
?>[/PHP]
When I go to http://www.philbon.com/getimage.php?id=3 it works just fine, but when i put the img tag in a webpage like: http://lucy.philbon.com it doesn't display the picture but instead just has spaces. I've tried it in both IE and FF and I get the same results. Can anyone help me out?
Here is the code for http://lucy.philbon.com:
[html]
Programming Forums: The image tag is right under this:<br />
<img src="http://www.philbon.com/getimage.php?id=3" />
[/html]

DaWei Jan 4th, 2007 1:03 PM

http://www.philbon.com/getimage.php?id=3 doesn't work for me.

PhilBon Jan 4th, 2007 2:06 PM

Could you give me some feedback as to what is present?
What is the title?
What is display? just a white page?
What does the source look like?
Are you pomted to download/save/open the file?
What browser are you using?
I think I might have to rethink this idea.

Nevermind... I will Work on it later

Jimbo Jan 4th, 2007 2:15 PM

Worked for me once, but now it's not. Here's what I get from viewing the source:
Quote:

<br />
<b>Warning</b>: imagejpeg(): supplied argument is not a valid Image resource in <b>/home/philbon/public_html/getimage.php</b> on line <b>4</b><br />
And what gets rendered in the browser window is just the URL.

[edit:] That's on Firefox 1.5

xavier Jan 4th, 2007 2:16 PM

Firefox 2.0 here:
Title :getimage.php
Content on screen : http://www.philbon.com/getimage.php?id=3
After some time : the download window apears.
Source code :
:

<HTML>
<body>
  <img alt="http://www.philbon.com/getimage.php?id=3" src="http://www.philbon.com/getimage.php?id=3">
</body>
</html>

Also with the webDeveloper extension from firefox, the view source option shows :
:

<br />
<b>Warning</b>:  imagejpeg(): supplied argument is not a valid Image resource in <b>/home/philbon/public_html/getimage.php</b> on line <b>4</b><br />


titaniumdecoy Jan 4th, 2007 3:21 PM

:

  1. <?
  2. $path = $_SERVER["DOCUMENT_ROOT"] . "/pictures/" . $_REQUEST['id'] . ".jpg";
  3. header("Content-type: image/jpeg");
  4. $fo = fopen($path, "r");
  5. $img = fread($fo, filesize($path));
  6. echo $img;
  7. ?>

You must use a relative filepath for the image; you cannot prefix it with a domain.

Pizentios Jan 4th, 2007 3:21 PM

Quote:

Originally Posted by PhilBon (Post 122154)
So right now I'm working on some php so I can have <img src="http://www.philbon.com/getimage.php?id=3" /> that it will display the image based on the id. So far I have
[php]<?
$id = $_REQUEST['id'];
header('Content-Type: image/jpg');
echo file_get_contents("http://www.philbon.com/pictures/" . $id . ".jpg");
?>[/php] When I go to http://www.philbon.com/getimage.php?id=3 it works just fine, but when i put the img tag in a webpage like: http://lucy.philbon.com it doesn't display the picture but instead just has spaces. I've tried it in both IE and FF and I get the same results. Can anyone help me out?
Here is the code for http://lucy.philbon.com:
[html]
Programming Forums: The image tag is right under this:<br />
<img src="http://www.philbon.com/getimage.php?id=3" />
[/html]


Why not cut out the middle man and do this:

[php]
echo "<img src=\"http://www.philbon.com/pictures/" . $id . ".jpg\">";
[/php]

btw, when coding in php, you don't need to worry about testing your php code on different browsers as php is server side only (unless you count php-gtk, but that's a different story). The only things you should be testing on different browsers is javascript/html/CSS etc...

titaniumdecoy Jan 4th, 2007 3:36 PM

Quote:

Originally Posted by Pizentios (Post 122165)
btw, when coding in php, you don't need to worry about testing your php code on different browsers as php is server side only (unless you count php-gtk, but that's a different story). The only things you should be testing on different browsers is javascript/html/CSS etc...

That's not totally true. For example, older browsers will attempt to download a file with a .php extension, regardless of the headers you send.

You can use Apache to prevent this from happening: Rename getimage.php to getimage.jpg and upload a file named .htaccess to the same directory containing the following:
:

<Files getimage.jpg>
ForceType application/x-httpd-php
</Files>

This tells the server to treat getimage.jpg as a PHP file.

DaWei Jan 4th, 2007 3:39 PM

This is what I get.
http://www.daweidesigns.com/images/what.jpg

titaniumdecoy Jan 4th, 2007 3:42 PM

It works for me at this point (Safari on a Mac). I suggest using the .htaccess technique I suggested so that all browsers will treat the file as a jpeg.

Out of curiosity, DaWei, if you load lucy.philbon.com in your browser, are the inline images displayed, or do you receive the same download prompt?


All times are GMT -5. The time now is 1:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC