Question

I am creating a PHP thumbnail app for links. I am considering to options:

  1. Store image - and since I dont want to create new image everytime user shares the same link I would like to store the images with names referencing the url, I also want to avoid too-long file names if someone enters very long url. So I was thinking about hashing the url, but is md5() enaugh? Or should I use sha1() (8 characters more = 40), or does anyone else have a better idea how to store it ?

  2. Save just a link and image link - which raises another choices - store it in DB
    or as a files with name > as in 1. < containing only the link?

Thanks.

Was it helpful?

Solution

When creating virtual filesystems, I use sha1() even though collisions are fairly unlikely with md5(), it doesn't cost much more.

As for storing the thumbnails, I would recommend saving them to disk once they're creating. The method of detecting that you've already created the thumbnail vs. it being a first-time request will depend on how you're creating and storing them.

In one of my applications, users upload files which I store using the SHA1 hash onto the filesystem with the metadata in a MySQL DB. The file is retrieved using three parameters: file_id, width, and height. I retrieve the SHA1 hash from the DB for the specified file_id, then check the filesystem for the existence of a file named [hash][width][height]. If it doesn't exist, then I generate the thumbnail and serve it up. If it does exist, then this means I've already created the thumbnail of this size and I serve it up.

Since I'm using a script to generate/send the thumbnail on-demand, my script that serves the file checks for a last-modified from the client and tells the client to use its cached version, as appropriate, rather than spending bandwidth to re-send the same image.

OTHER TIPS

I recommend you to use an url outside the document root and call the image with a script. If the user has an url associated in the database the script will call this url. E.g.

 <img src="script.php" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top