C
C
caps.com2014-03-05 14:10:03
PHP
caps.com, 2014-03-05 14:10:03

How to implement image resizing on the fly using php?

Good afternoon!
There was a question about optimizing the image resizing process using php on the fly. The resize link looks like this:
localhost/$controller/$config/$token/$image- >id.$image->format
The controller checks the token (it is unique for each image) so that the link is not forged. And if it is valid, the path to the image from the database to the id is obtained, the content is resized according to the config and given to the browser with headers. The whole thing is cached by Nginx for a month, so that when you request it again, it immediately returns the image from the cache.
It is necessary to make such a moment, if suddenly the storage has fallen off and there are no pictures, then a stub picture is given. And naturally, it gets into the Nginx cache, and even when the pictures appear, the cache is still given away, and you have to clean it, and this creates a significant load.
Can you please tell me how to solve this situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2014-03-05
@caps.com

This can be solved at the nginx level with try_files and X-Accel-Redirect, no cache at all.
For example, like this:

location <локейшен с картинками>{
    try_files $uri @resize;
}
location @resize {
    proxy_pass http://<адрес ресайзера с параметрами>;
}
location /notexist.gif {
    root /path/to/notexist/folder;
}

Then Angie will first try to find the file (the resizer should save it there before uploading), if it doesn’t find it, try resizing (the resizer will start, which, I remind you, should save the image to disk before uploading), and if the resizer did not find the original image, it needs to be returned X-Accel-Redirect header with uri /notexist.gif
You can read more about X-Accel here wiki.nginx.org/X-accel
Using angi caching for this is the wrong way, because you will get a non-deterministic result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question