I tried using "lsyncd" to create images for mobile pages in real time



Access analysis result of GIGAZINE for May 2010Although it is mentioned,モバイル版GIGAZINE(http://m.gigazine.net/)Improved image size and image quality. For creating the image,Lsyncd"Is used.

Lsyncd is a daemon for synchronizing files using a file system event monitoring mechanism called inotify introduced from Linux 2.6.13. In the latest implementation (currently 1.33), recursive monitoring of subdirectories, monitoring directory as an argument Command execution (rsync is executed by default) is possible.

Originally, this lsyncd is for synchronizing files between computers, but by using the function of "Execute command with monitoring directory as argument", scripts for creating reduced images are created in real time I tried.

Details are as below.
Lsyncd - Project Hosting on Google Code

■ Installation
1.In CentOS used for image server, lsyncd package exists in RPMforge. However, since this is a very old version and lacks necessary functions such as script execution, we will compile it by getting the latest package from lsyncd's official page. First of all, we get the package "libxml2-devel" necessary for lsyncd from RPMforge.

Yum --enablerepo = rpmforge - y install libxml 2 - devel

2.Get the tarball of lsyncd, compile and install.

Cd / usr / src
wget http://lsyncd.googlecode.com/files/lsyncd-1.33.tar.gz
Tar zxvf lsyncd - 1.33.tar.gz
Cd lsyncd - 1.33
./configure --prefix = / usr / local
Make
Make install

3.Modify the configuration file lsyncd.conf.xml as follows. Although it seems that any target is considered, specify / dev / null as a precaution.













Four.Create an image reduction script mkmbimg.sh. Since lsyncd monitors at the directory level and passes only directories as script arguments, in the script, all images in the directory are generated when images are added. Basically it just takes a directory as the first argument and creates a reduced image from the image on that directory. When extracting only the important part of this script, it becomes as follows. Do not forget to grant execute permission to the mkmbimg.sh you created.


Find "$ 1" - iname \ *. Jpg - or - iname \ *. Jpeg - or - iname \ *. Png - or - iname \ *. Gif | while read file; do
If [- f "$ file"]; then
Convert - type optize - thumbnail 150 x 150 - quality 40 - sampling - factor 1 x 1 - sharpen 1 x 1 "$ {file}" "$ outfile 1"
Convert - type optize - thumbnail 275 x 275 - quality 50 - sampling - factor 1 x 1 - sharpen 1 x 1 "$ {file}" "$ outfile 2"
Fi
Done

■ Execution
Enter the command as follows. By creating "--delay 5" or when creating a file, wait 5 seconds to prevent the script from being repeatedly executed when another file is created.

./lsyncd --delay 5


■ Devices for actual operation
In order to be able to automatically execute as a daemon at system startup, we create a startup script. In addition, GIGAZINE has about 16000 articles at the present time, and since the image directory is divided by year, month, day, article, total number of directories is about 19000. The number of watched directories in inotify can be changed, and by default it is increased to 65536 because it is as low as 8192.

Echo 'fs.inotify.max_user_watches = 65536' >> /etc/sysctl.conf


·bonus
I compared the size of image of mobile version GIGAZINE and how much image quality changed from the image of actual article. By the way, there are 4 to 5 times difference in file size before and after change.

Everyday cats are too relaxed


I attempted to challenge Lotteria's cheese burger × 10 pile layered "Tower Cheese Burger"


Female athletes whose uniforms have bitten and their butt is petty

in Mobile,   Web Service, Posted by darkhorse_log