Improve user interaction with yii by running image resize/write in background


I have a web form that uploads a image. While the user uploads the image, i would like to user to be able to carry on to browse my website while at the background, the image gets resize/converted/write to db. Uploading a image can take a long time and I would like to improve the user interaction with my yii website so user do not have to wait.

  1. I thought aftersave() was a function that I can achieve this. but the user still have to wait for all the functions in aftersave() to complete.

  2. So I guess I would need to write another model call ‘sync’ that will cron every 1 min in the background to ‘catch’ whether there is a new image uploaded.

Or is there a better way to do it in yii?

Can someone point me to the right direction?


Sync is best.

You can also use the log for user activities and then check which actions or processes that can be carried out in background and use cronjob for them to complete the tasks initiated by the user.


User Clicks on Image Load

User Selects The Image

User Click on Upload - Do in Background

User Click on Friends List

User Send a Request for New Friends - Do in Background

Hope this will help

PHP is a single-threaded language, so methods like before- and afterSave are just called in the same thread.

Besides a cron-job, you could also look into Gearman, I hear it’s quite a popular solution for this kind of problem.

Hi PeRoChAk,

I will stick to cron. Maybe in the next version of yii, there will be a build-in ‘function cronTask()’ for each model so we can populate the scripts that needs to be cron.

Then we can cron a ‘yiicron.php’ which will call the ‘function cronTask()’ each model? Just an idea.

Good advice below to run the friend request in the background as well!


Very interesting on being able to run tasks in parallel which means.

1)user upload images

2)user carry on to browse my webpage while image is uploaded in background.

3)image upload complete and prompt user to view

Will explore Gearman and learn how I can fit that into my web app.