0

so I'm using Autocomplete. I'm using it to display images left to the search results. The image displayed is from the products table, and that image is a around 400x400 px. But, in the autocomplete, it wouldn't be possible to show that image. Suppose I need an image of 50x50 px, how would I make the function that re-sizes the image (just to show) and the actual image is still 400x400 px ?

Here's my products table :-

productid         productname  price  image
autoincrement       somename    100    ../productimages/someimage.extension

Now this image is available in php as $result->image. I have no idea as to how I can start. Any suggestions are welcome.

user3605847
  • 65
  • 2
  • 10

2 Answers2

1

It would be a lot of hassle to save a temporary image etc. Why don't you just display it in HTML using the desired width?

<img src="someimage.extension" class="thumbnail"/>
<style>
    .thumbnail {
        width: 50px;
    }
</style>
dcclassics
  • 896
  • 1
  • 12
  • 38
  • @user3605847 How many images do you have in your dropdown? Because now you are needlessly sending 160'000 pixels (250kb?) per image, in stead of 2'500 pixels (4kb?) per image. Many images or low bandwidth may result in a slow website. If your dropdown shows multiple images at once, this is not a good idea. – nl-x May 15 '14 at 20:37
  • It would have around 10 images at once. – user3605847 May 15 '14 at 20:42
1

Actually, the situations where on the fly resizing is feasible are little. Just resize all images once and keep these thumbnails on disk next to the original ones. Add a prefix or suffix to the filename to not have to store an extra filename in your database. Eg someimage_thumb.extension

For the exact steps to do the resize, the number of answered questions about this is big. Example: https://stackoverflow.com/a/9655694/1209443 . An important question however... Does your PHP support GD or ImageMagick ?

Community
  • 1
  • 1
nl-x
  • 11,762
  • 7
  • 33
  • 61
  • Especially with how cheap storage is these days, having a copy of 50x50 pixel thumbnails of every image isn't a big deal – andrewtweber May 15 '14 at 20:25
  • I agree with all of this, I just thought for a new programmer using a quick HTML fix would work. This is the optimal way to do it though, so users don't have to load a 2000x2000 image every time just for a 50x50 thumbnail. – dcclassics May 15 '14 at 20:31
  • Yes, my PHP supports GD. I'll take a look at your solution. Currently using one posted by @dcclassics. – user3605847 May 15 '14 at 20:31