Best way to cache resized images using PHP and MySQL

前端 未结 9 1555
庸人自扰
庸人自扰 2020-12-22 23:54

What would be the best practice way to handle the caching of images using PHP.

The filename is currently stored in a MySQL database which is renamed to a GUID on upl

9条回答
  •  旧巷少年郎
    2020-12-23 00:40

    One note worth adding is to make sure you're code does not generate "unauthorized" sizes of these images.

    So the following URL will create a 200x200 version of image 1234 if one doesn't already exist. I'd highly suggest you make sure that the requested URL contains image dimensions you support.

    /images/get/200x200/1234.jpg
    

    A malicious person could start requesting random URLs, always altering the height & width of the image. This would cause your server some serious issues b/c it will be sitting there, essentially under attack, generating images of sizes you do not support.

    /images/get/0x1/1234.jpg
    /images/get/0x2/1234.jpg
    ...
    /images/get/0x9999999/1234.jpg
    /images/get/1x1/1234.jpg
    ...
    etc
    

    Here's a random snip of code illustrating this:

    
    

提交回复
热议问题