某系统中用户可以上传头像,可是某些用户实在是不知大小,居然把生活照都传上来了,只好想办法给它压缩一下。
幸好Linux下有ImageMagick这样的大牛,在Ubuntu下只需apt-get install imagemagick即可。
一般的jpg图像120*120大小的大概是50k左右,由于不知道用户上传的图像的大小和比例,所以把压缩门槛设为100k。不管用户上传的图片有多大,只要大于100k,就给它压缩50%。经过若干轮的压缩,就会得到理想的120*120左右的图片,这有点递归的意思。
根据这个思路,写一个脚本resize.sh如下:
#!/bin/bash
echo “resize image who is bigger than 100k”;
for i in `find . -size +100k`;
do
convert $i -resize 50% $i;
echo “resize image $i to 50%”;
done
$ chmod +x resize.sh
$ ./resize.sh
resize image who is bigger than 100k
resize image ./2010-01-13/e0110c4b-97bf-4ef6-808a-9d8f844bf950.JPG to 50%
resize image ./2010-03-16/e9b75d65-a122-4a77-b409-4b0f0e0851d7.jpg to 50%
resize image ./2010-03-23/24c7e4f5-90a2-4052-8429-d1a13d77a127.jpg to 50%
resize image ./2010-01-12/992132d1-f630-4e50-809e-41ef836305e7.jpg to 50%
resize image ./2010-03-31/2e217345-c04c-4c52-90a6-566167c58c07.JPG to 50%
resize image ./2010-03-31/59bb335d-00b6-4bda-9b1a-6cede53d8d6a.JPG to 50%
$ ./resize.sh
resize image who is bigger than 100k
resize image ./2010-03-31/2e217345-c04c-4c52-90a6-566167c58c07.JPG to 50%
resize image ./2010-03-31/59bb335d-00b6-4bda-9b1a-6cede53d8d6a.JPG to 50%