I have a project in which a user uploads an image through a form and the server does some thumbnails. The thumbnail making process is very slow so I thought that doing the image resizing with a non-blocking function could be a good solution. I mean: the server process the form (wich have more fields) gives the "ok" feedback to the user and then calls the thumbnailing function. How can I do that?
Thanks in advance
So, yeah, PHP is blocking, so using a timeout with a function was out of the question. There are a number of workarounds, but they're not very robust. But then I remembered Amp. Amp (and ReactPHP) are frameworks for asynchronous programming in PHP.
Blocking functions let you execute custom code that modifies the result of a user registering or signing in to your app. For example, you can prevent a user from authenticating if they don't meet certain criteria, or update a user's information before returning it to your client app.
stream_set_blocking(resource $stream , bool $enable ): bool. Sets blocking or non-blocking mode on a stream . This function works for any stream that supports non-blocking mode (currently, regular files and socket streams).
Your best option would be to implement Gearman. It's a Job Queue system where you can implement either synchronous of asynchronous jobs. http://gearman.org/
Better solution I usually go for: Create the thumbnails dynamically when needed, not upon upload.
You create a script that generates thumbnails on the fly, and all your image tags point to this script:
<img src="/thumbnail.php?image=foobar.jpg&size=150" />
This delays the thumbnail generation until it is needed and works "asynchronously". With some .htaccess rewrite magic you can even make it look like a normal image file and cache images in a way that the Apache server will serve them the next time without invoking the script.
To be a little more detailed, I use this for user profile images:
Image tags:
<img src="/img/users/123456/50.jpg" />
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrites requests for user images to match directory structure.
# E.g.: URL /img/users/123456/50.jpg -> /img/users/123/123456/50.jpg
# Intermediate directory level is introduced to avoid cramming too many directories into the same directory.
RewriteRule ^img/users/(\d{1,3})(\d*)/(\d+\.\D+)$ img/users/$1/$1$2/$3 [nocase,last]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
This first of all rewrites image requests to a deeper directory structure. If the image exists, Apache will serve it as usual. If it doesn't, my regular application is invoked. In the app, I route /img/users/...
URLs to a module that ends up with two pieces of information: the user id 123456
and the requested size 50
. It then generates a thumbnail roughly according to this logic:
123456
/img/users/123/123456/50.jpg
, where it will be picked up by Apache next timeIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With