How to display a .jpg or .gif file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sharky
Forum Newbie
Posts: 17
Joined: Sun Jun 04, 2006 12:31 pm

How to display a .jpg or .gif file?

Post by sharky »

I'm going to display a .jpg or .gif file from my db. What I did --> In my db ::

FIELD = photos --> 'images.jpg'
TYPE = VARCHAR
LENGTH/VALUES = 20


I stored my .jpg files in my localhost under same path as my phpmyadmin. This is what I did ::

Code: Select all

<tr>
              <td width="104" height="101" align="center"><img src="<?php echo $row['photos']; ?>" width="51"                   
                 height="88">
              </td>
              <td width="10" valign="">
              </td>
</tr>
how do I store/display the images properly? Thanks
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You've pretty much got it. Probably what's happening is your path to the image is wrong. Try:

Code: Select all

<img src = "/phpmyadmin/<?php echo $row['photos']; ?>" .... />
Make sure you've got the full path to the image.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
sharky
Forum Newbie
Posts: 17
Joined: Sun Jun 04, 2006 12:31 pm

Post by sharky »

Oh. The 'images.jpg' is stored in the same path as phpmyadmin, which is the localhost. So, there's no path (I think). I could just type ::

Code: Select all

<img src="<?php echo $row['photos']; ?>" width="51" height="88">
No? Or maybe the easiest way is to declare the path with a variable? How can I do that?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

So the images are stored right at the root? The full url of an image would be:

Code: Select all

http://www.yourdomain.com/someimage.jpg
???

If so, just put a slash in front of the image name to make the tag look like:

Code: Select all

<img src = "/<?PHP echo $row['photos']; />" ... />
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
sharky
Forum Newbie
Posts: 17
Joined: Sun Jun 04, 2006 12:31 pm

Post by sharky »

Yes, my image is stored in ::

Code: Select all

http://mydomain.someImage.jpg
I didn't put the slash. Then I added the slash. It looks like ::

Code: Select all

<img src="/<?php echo $row['photos']; ?>/" width="51" height="88">
I also tried ::

Code: Select all

<img src="/<?php echo $row['photos']; ?>" width="51" height="88"/>
Does not seem to work. Not sure if the slashes are at the right place.

In my phpmyadmin :: should I use VARCHAR or the BLOB for the type? Right now I'm using VARCHAR(20).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Whoa! Are you storing the image data in the database as well? By image data I mean are you reading in the JPG and storing the actual pixel-by-pixel information in the DB? If so, ~bdlang was correct. Do a Google search about storing images in a database. One of your search results should have everything you need to know.

Don't use a varchar - it's not nearly big enough. Use a blob (for small images) or mediumblob (for medium sized images).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Hmm. I definitely thought based on the initial post (edited?) that the images were stored in the database. Maybe it was too early for PHP questions.... :) Only problem is, I'm still confused, and that can't be good. :?
sharky
Forum Newbie
Posts: 17
Joined: Sun Jun 04, 2006 12:31 pm

Post by sharky »

Well. I did store something in the database.

FIELD :: photo
TYPE :: mediumBLOB
VALUE :: (I browsed .jpg from my localhost, same path as my phpmyadmin) myImage.jpeg


When TYPE is mediumBLOB, it displays garbage in my showProduct.php file. When TYPE is tinyBLOB, it displays a broken .jpg file. My php ::

Code: Select all

<img src="\<?php echo $row['photos']; ?>" width="51" height="88">

Anything else I should add? Or my slash should be somewhere else.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Did you do a Google search? You can't just echo that data, you have to use a PHP file to dump the jpg headers & then the image data - essentially mimicing a JPG file.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply