PHP dynamic images

I took the PHP CodeIgniter website, which dynamically generates modified images and thumbnails.

Here is an example of an img tag on a site that generates a 100px by 100px thumbnail:

 <img src="/media/image/ImageName.jpg/100/100" /> 

This is generated using the helper:

 echo img('media/image/ImageName.jpg/100/100'); 

There is some logic to the image function in the media controller, which first checks the cache folder and then serves the cached image if it exists.

It still seems like this is not an elegant approach and looking at how other PHP frameworks like SilverStripe do it would be better to do something like this:

 echo img_link('ImageName.jpg', 100, 100); 

This new img_link function in the helper will first check the cached image and write the link directly to the image file or generate a new image and send a direct link to html.

My thoughts are that if PHP serves every img using this type of code

 $this->output->set_content_type('jpeg')->set_output(file_get_contents($file_path)); 

he will always be ineffective.

Anyone's thoughts?

+4
source share
1 answer

I agree, I did exactly what you offer (direct mail, if it is already cached), and it works fine.

The only time I can think that you will be caught - if your html is cached and you deleted the cached image, so the direct link to the image file is now broken.

In my case, I never had to delete cached images, so I haven't hit it yet.

The only other time that I see that this is a problem is if you have personal images. In this case, php will have to check who has permission before displaying the image, you cannot just serve the cached file as a direct link, as people can share this direct link and security will no longer be applied.

If you need it, and you can install the modules on apache (provided that you use apache), there is an "x-sendfile" module that will allow you to transfer the file delivery process back to apache, and not through php. This is good for large files in which php can time out, etc.

+1
source

Source: https://habr.com/ru/post/1411512/


All Articles