Rob Fenwick
Rob Fenwick

Reputation: 169

What is the best way to associate a thumbnail with an image in a mysql database?

I am building a database for a website that will be a cataloge of products. I am having a problem with the table of images. I have a php script that resizes the image with three sizes, renames them with a suffix, writes them to a directory and adds a reference to them in a table of images.

I am having trouble figuring out how the thumbnails (the small images) will be associated with the medium and large images since I'm planning to have multiple images for each product. I want to be able to add all thumbnails to a page with a link either to the medium or large image via a php script.

Here is what I have so far,

image-id  product-id  filename              size    caption  width  height  alt
13        1           PassPro150_large.jpg  large   NULL     632    569     NULL
14        1           PassPro150_medium.jpg medium  NULL     250    225     NULL
15        1           PassPro150_small.jpg  small   NULL     115    103     NULL

Is there a standard way to do this? Should I make three tables, one for each size and associate them with a primary-foreign key? I also thought I could do it with string manipulation, replace _small with _large since all images uploaded will have the same postfix. Any suggestions?

Upvotes: 4

Views: 2146

Answers (3)

Joseph
Joseph

Reputation: 119827

  1. Create one table for recording the images.
  2. once you upload the images, rename the image to correspond to the item ID
  3. to add to that, append also what image number it is
  4. use a PHP script to resize your images and append as a suffix the size

now you will have this format:

imageid_imagenumber_imagesize.jpg

where:

  • imageid - corresponds to the item id
  • imagenumber - to distinguish it from the other images of the same product
  • imagesize - the suffix of the size

place them somewhere in your website, and you are good to go. no need for a database

as for image dimensions, and other meta data about the image, read more about getting the EXIF data using PHP

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157828

I wonder why to use a database for this purpose at all.

Can't you just add a word "small" to the image name and put it in the img tag, along with predefined dimensions?

Upvotes: 2

Broncha
Broncha

Reputation: 3794

What Joseph said is one solution. Or you could create multiple folders different sizes of thumbnails, like thumb_small, thumb_medium and save the images in there with same name.

You just keep the filename in the database. and when you need to display the image you adjust the url to the particular folder.

Upvotes: 1

Related Questions