开发者

Watermark 2000+ images at once?

I have a image hosting service and people upload offline (they upload to another computer like a station without internet access) and then we sync the images (they aren't watermarked at this stage) and they are not approved at this point.

There is an admin area where the user would go to approve an image, and 开发者_JS百科we will apply the watermark at this stage.

I am wondering what method would be best to watermark 2000ish images using PHP (at a time). Would creating a system daemon and waiting for a response a good idea or?


Performing such tasks on such a high number of files is not a good idea with php, at least when you plan to execute that script via HTTP. The reason for that is the timeout of a php script, which is usually 30s (and shouldn't be set too high).

The best option to solve such things is to create a script or daemon that runs directly on the server and converts any new image automatically (or after being told so). So it will continue to run even when the current php script has finished. The background process could then for example update statistics into a database which the php script can fetch to get information about the process.

Another way, I personally use for the thumbnail generation of a private gallery, is to split the high number of tasks into smaller jobs that can be easily worked with in the 30s limit. In my case, I limit it to 50 pictures per execution (it could probably handle a lot more, but I want to be safe). So basically I call my script, it gets the first 50 not-already-completed filenames from a database, does its thing on each file and returns. And then I simply click F5 to let the script do the next 50 files. With ~2000 images, it would probably be wise to automate the F5-hitting, for example by simply putting a javascript on the page that refreshes it after it loaded completely.

If you have the possibilites to go with the first option, do that, but I have found my second option to be quite useful for shared hosting (which is where I use it currently).


Set up a script in cron, and do it in the background.


The best thing would be to run a Cron job/daemon in the background. But, instead of a PHP script, just use a simple bash script with ImageMagick. That is, if you don't have to access a database or anything to get the images.

If you do need to access a database, use PHP, but call shell_exec() to run the bash script on the correct images. If you're on a Windows server, you'd obviously use a batch script instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜