I’m building out an admin area for an ecommerce site where the user can create a new product and upload multiple images to be used for the product. I have a table that lists all of the products, each row shows the first image returned from the database. I can scale down a large image to 100px x 100px but the user is still downloading a big image, not a true thumbnail.
I see two ways of doing this:
1. I can make the user choose which img will be the thumbnail so that the regular img is upload and also a smaller version of the file.
2.I can create thumbnails for every img that is uploaded and append to the filename of the thumbnail img so that I can return the first image that ends with a certain string.
Is there a more elegant way to do this or am I on the right track?
Create a cache directory, then create a script called something like image.php. Link your images like this
Then in image.php, it should first check in the cache directory if the file exists.
Call the file
"img.png&width=100&height=100"and save it in the cache directory. That way you can easily check if it exists, but there is enough entropy for someone to change it towidth=101andheight = 101so that the image will be regenerated.Each time you create the thumbnail, just store it in the cache directory. If it exists, do a
header()call and anecho file_get_contents()and thendie().