?
Solved

Display jpeg image

Posted on 2005-03-22
10
Medium Priority
?
498 Views
Last Modified: 2011-10-03
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
Comment
Question by:osiris247
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13603539
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
 
LVL 7

Author Comment

by:osiris247
ID: 13603816
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13605989
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:osiris247
ID: 13606069
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
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 2000 total points
ID: 13606322
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
 
LVL 7

Author Comment

by:osiris247
ID: 13606411
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13606457
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
 
LVL 8

Expert Comment

by:_Marcel_
ID: 13609415
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
 
LVL 7

Author Comment

by:osiris247
ID: 13609682
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13609728
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question