Hi everyone, I'm trying to display an image stored in a folder with php code. But I keep getting a tiny box in the top left of my screen when submitted. Can't seem to display the picture.
I have the file path saved in $image_path.
$image_path = "user_images/image.jpg";
$image=imagecreatefromjpeg($image_path);
header('Content-Type: image/jpeg');
imagejpeg($image);
This code to my understanding should allow the image to be selected and displayed. But it doesn't.
All responses greatly appreciated.
Trying to display image with imagejpeg()
Moderator: General Moderators
-
jeremydricard
- Forum Newbie
- Posts: 3
- Joined: Tue May 15, 2012 12:13 pm
-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
Re: Trying to display image with imagejpeg()
Is there are reason you aren't simply using....
aside: make sure the path is correct relative to the folder in which the script resides
Code: Select all
<?php
$image_path = "user_images/image.jpg";
?>
<img src="<?php echo $image_path; ?>">
<?php
-
x_mutatis_mutandis_x
- Forum Contributor
- Posts: 160
- Joined: Tue Apr 17, 2012 12:57 pm
Re: Trying to display image with imagejpeg()
Will work only if your $image_path is a relative path to document root. If your image path is "C:\image.jpg" then it will look for image.jpg on client's C drive, not on the server's.litebearer wrote:Is there are reason you aren't simply using....aside: make sure the path is correct relative to the folder in which the script residesCode: Select all
<?php $image_path = "user_images/image.jpg"; ?> <img src="<?php echo $image_path; ?>"> <?php
-
jeremydricard
- Forum Newbie
- Posts: 3
- Joined: Tue May 15, 2012 12:13 pm
Re: Trying to display image with imagejpeg()
the php page is in the root folder, and the image is inside "user_images" folder as image.jpeg. Everything seems to be fine, yet I still cant pull up the picture. Heres my path
root
C:\xampp\htdocs\jeremyricarddesign\user_images
root
C:\xampp\htdocs\jeremyricarddesign\user_images
Re: Trying to display image with imagejpeg()
Is that just a typo or does the file have an extension of .jpeg? Also, not sure what you mean by showing "root" apparently over "jeremyricarddesign". Ordinarily, the htdocs folder is the document root. Again, if that's the case, you're missing a folder in your path. I, too, wonder why you are not simply sending the image as an html image, without going through all of the imagecreatefromjpeg() stuff. Maybe there's more to it than you are explaining to us.jeremydricard wrote:the php page is in the root folder, and the image is inside "user_images" folder as image.jpeg. Everything seems to be fine, yet I still cant pull up the picture. Heres my path
root
C:\xampp\htdocs\jeremyricarddesign\user_images
-
jeremydricard
- Forum Newbie
- Posts: 3
- Joined: Tue May 15, 2012 12:13 pm
Re: Trying to display image with imagejpeg()
I'm trying to use it strictly to learn it. So using the html is more effecient in this case? I just want to know whatever is the more up to date way of doing things. So it's best I pull up the user image from the folder with img_src ? do i have to send out headers?
thanks, all appreciated. learning this <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> on the run.
thanks, all appreciated. learning this <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> on the run.
Re: Trying to display image with imagejpeg()
The functions like imagecreatefrom... are used when you have to generate a NEW image that doesn't exist in the format you need. They are resource intensive functions and should never be used if the image already exists in a format you can use. If you have an image in either .jpg, .gif, or .png format, they are universally recognized by all browsers and only need to be referenced as the src attribute of an <img... html element.jeremydricard wrote:I'm trying to use it strictly to learn it. So using the html is more effecient in this case? I just want to know whatever is the more up to date way of doing things. So it's best I pull up the user image from the folder with img_src ? do i have to send out headers?
thanks, all appreciated. learning this <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> on the run.
No, you do not need to explicitly send headers in this circumstance. Ordinarily you would send the normal html lines, such as <!DOCTYPE HTML> (that's for HTML5, earlier DocTypes are much lengthier), <html>, <head>, <title>, </title>, </head>, <body>, and later, the </body> and </html>. That can be done either by using either print() or echo(), if you need to insert a lot of dynamic content, or more commonly just put those lines outside the <?php ... ?> block.
Indeed, if you are just trying to display an image on a web page and you know the path to the image on the server, there is no reason to use PHP at all. You only need to use HTML:[text]<img src='user_images/image.jpg' alt='My Image' />[/text]
PHP would be used only when you can't predict what image, for example, will be requested. In that case you would probably have a script that presents a form for the user to indicate what image they want to see, or a list of choices, each of which is an anchor element, like[text]<a href='test.php?img="image8.jpg'>Number 8</a>[/text]
Then this script would use either $_POST (data from a form) or $_GET (data from a URL) to obtain the image name. In that case, your script might look more like this:
Code: Select all
<!DOCTYPE HTML>
<html>
<head>
<title>Image Test</title>
</head>
<body>
<?php
$filename = $_GET['img'];
$path = 'user_images/';
echo "echo <img src='$path.$filename' alt='Your image' />";
?>
</body>
</html>