Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non blocking functions PHP

Tags:

php

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

like image 712
LuisClemente Avatar asked Feb 10 '11 08:02

LuisClemente


People also ask

Is PHP blocking or nonblocking?

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.

What functions are blocking?

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.

What is Stream_set_blocking?

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).


2 Answers

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/

like image 195
Miljar Avatar answered Oct 30 '22 23:10

Miljar


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:

  1. Find profile image for user 123456
  2. Generate thumbnail in requested size
  3. Write thumbnail to /img/users/123/123456/50.jpg, where it will be picked up by Apache next time
  4. Output image
like image 22
deceze Avatar answered Oct 31 '22 01:10

deceze