Monday, April 25, 2011

Create and store images on the fly

How would I go about catching a request for images that doesn't exist and create them as needed so as not to return a 404? These would then be stored on the server until no longer needed and deleted. The code would preferably be in PHP. Deletion will most likely occur in a cron job.

The images would be requested in batches of 10 or so at a time with names such as 084a007b0138024f0a.jpg.

From stackoverflow
  • Edit your htaccess, so in that folder requests for any image is redirected to a PHP script.

    Then, in your script, create the image (http://php.net/image/) and simply echo it.

    graham.reeds : Thanks, I'll give that a try.
    pestaa : You're welcome, thanks for the plus!
    graham.reeds : No problem. I was wondering about performance penalties. Could it handle a scenario where it first looked to see if the file existed and if not accessed the script? Or would you have to handle that in the script?
    vartec : that would call PHP *every* time you request that image.
    pestaa : Not necessarily. If htaccess configured correctly, PHP is only called when image is non-existent.
    : What's wrong with calling PHP every time? You can then contain all of the logic for managing creation and deletion of the images in a single PHP application.
    vartec : @pestaa if you're only echoing it, it'll never exist
    pestaa : @vartec, "creating the image" means saving it at the same time in this context. @mozboz: If the site has gained a rather big popularity, calling PHP for every image can be a resource hog.

0 comments:

Post a Comment