• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Display jpeg image

Hi
I am trying to dislpaly a jpeg image, the image file path i am pulling from a mysql table.

The code i have is
<?//I have a session_start() and i intialise the session variable here.
  header("Content-type: image/jpeg");?>

This code is within a loop displaying the rows of the table from my query.

  $im = @imagecreatefromjpeg("&image_");//&image_ being the variable the file path is held in.
  imagejpeg($im,'',50);
  imagedestroy($im);

I have also tried by entering a filepath which i know is goo but i get the same error.
I have tried putting removing the '@' but i get another error ( but i am not sure what it does)

The error i get is
Warning: imagejpeg(): supplied argument is not a valid Image resource in ...../basket.php on line 127

Line 127 is imagejpeg();

Hopefully you guys can enlighten me as to what i am doing wrong.

Thanks
Steve
0
osiris247
Asked:
osiris247
  • 5
  • 4
1 Solution
 
Marcus BointonCommented:
If the path from the DB is put into $image (not &image - that's wrong), and the image is stored in an external file that's at the path location, all you need to do is:

header('Content-type: image/jpeg');
header('Content-length: '.filesize($image));
passthru($image);

you don't need to play with the image data using GD etc, just chuck it at the browser.
0
 
osiris247Author Commented:
OK, that worked in that i didnt give any errors but still no picture.
Tis is my code to display pic ina table

$im=passthru($image_);
                        
echo "<p><TR><TD ALIGN=\"center\"><img src=\"&im\" width=\"50\" height=\"50\">
</TD><TD>$id</TD><TD>$dtadded</TD><TD>$desc</TD><TD>$added</TD><TD>$price</TD></TR></p>";

I ahve put the header in a well, the above code is in a function, the filepath is put into $image from a my database, then $image is passed to the function as $image_.

How do i get this image to display in the table using img src? can this be done?  

I tried using passthru outside the table and i get an error.
Warning: passthru(): Cannot execute a blank command in /home/clementss/public_html/project/basket.php on line 46


Thanx
Steve            
0
 
Marcus BointonCommented:
I don't know what you're doing with these & instead of $ - it won't work whatever you do.

You've fallen for a common misconception - the image must be delivered via a separate HTTP transaction, probably something like this

echo "<p><TR><TD ALIGN=\"center\"><img src=\"getimage.php?name=$imagename\" width=\"50\" height=\"50\">
</TD><TD>$id</TD><TD>$dtadded</TD><TD>$desc</TD><TD>$added</TD><TD>$price</TD></TR></p>";

When the page is parsed, the browser will make a request to that PHP script to get the image data. You can't simply put image data inline - it just doesn't work like that. The script that delivers the image cannot deliver anything else at the same time, and similarly, the script that delivers the HTML cannot deliver an image at the same time. It's not uncommon for a single page load to require 50 or more HTTP transactions.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
osiris247Author Commented:
Sorry bud, the & are typo's, they are not in my code!! I promise!

I am not sure i follow what you mean..............
should I pass the image file path from the HTML page that the PHP page is called.  I do have other arguments being passed for ID etc.

Can i not extract the file path from mySQL table and use it that way?

Thanx
Steve
0
 
Marcus BointonCommented:
You can just use the path that MySQL gives you - it could be as simple as (assuming that $image contains a usable path to the image):

img src=\"$image\" width=\"50\" height=\"50\">

Otherwise you can pass in as many params as you need in order to select the appropriate image and output it as described previously.
0
 
osiris247Author Commented:
Thats how i tried it first time before getting into all this create jpeg stuff but it didnt work.  I even tried to type the path to be sure there was no issue with my SQL query.

img src=\"images/image1.jpg\"width=\"50\" height=\"50\">

but that didnt work either.?

I have checked and all files paths are perfect and they work fine in other html pages .

Steve
0
 
Marcus BointonCommented:
If that simple example doesn't work, you've got more serious problems! You MUST get an example like that working before anything more complex can be expected to work. Check and recheck your paths, make sure the image is readable by the web server, try using an absolute path, check your error logs, test the URL by itself in a browser etc.
0
 
_Marcel_Commented:
When you removed the @ what error did you get? (The @ represses an error-message on the function it is before.)

Did you check that the path you are using is an absolute path? or did you use a relative path, and did you make sure that that path is relative from the PHP script? (Not the documentroot!!)

Also make sure the PHP-script may read the imagefile.
0
 
osiris247Author Commented:
I am using a relative path to the image, I have checked and this works fine.  I have html pages in the same directory as the PHP page and they display fine.

I think there was an error with the my college hosting, as i reported the error and all of sudden it worked without using createjpeg() etc.  Which is  relief because I am not to sure how this works or what it does, if I can simply throw the path at the browser to display the image, what is the need for it? Creating new images?

Thanks for your help guys.
Steve
0
 
Marcus BointonCommented:
Yes, imagejpeg and friends are used when creating new images - see for example the jpgraph graph drawing class. It's also commonly used for things like generating thumbnails.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now