用戶之前在發燒網參加的上傳圖片活動都沒有經過無損壓縮處理.想用腳本對一月內傳上去的圖片進行處理,但Amazon_S3服務集群上只能使用他們提供的一些簡單的API.所以只能先down 下來,壓縮處理後,再傳上去覆蓋原來的圖片.
經過多次調試,最終寫了個php的腳本對之進行處理:詳見這裡.
代碼如下:
1: <?php
2: function compress_img ($source) {
3: $exts = array("png","bmp","gif","pnm","tiff");
4: $start_time = strtotime("-30 day");
5: exec("s3cmd ls s3://fever38-us-static/hotdeals/{$source}/ > ./tmp.txt");
6: $rs = file('./tmp.txt');
7:
8: foreach($rs as $line) {
9: $r = array_filter(explode(' ', $line));
10: if(!empty($r[0])){
11: $r[0] = trim($r[0]);
12: $time = strtotime($r[0]);
13: }
14: if(!empty($time) && $time >= $start_time){
15: if(!empty($r[10])){
16: $img = trim($r[10]);
17: $path_info = pathinfo($r[10]);
18: $ext = trim($path_info["extension"]);
19: $file_name = strtolower(trim($path_info["basename"]));
20:
21: exec("s3cmd get ".$img);
22: exec("cp {$file_name} /mnt/heisoo/s3/{$source}/");
23:
24: if (in_array($ext,$exts)) {
25: system("/usr/bin/optipng -o5 ".$file_name);
26: }
27: if ($ext == "jpg" || $ext == "jpeg") {
28: system("/usr/bin/jpegoptim -o --strip-all ".$file_name);
29: }
30: system("s3cmd put {$file_name} {$img} --guess-mime-type --add-header 'Cache-Control:max-age=31536000' --add-header 'Expires: Thu, 01 Dec 2014 16:00:00 GMT' --acl-public");
31: unlink($file_name);
32: }
33: }
34: }
35:
36: unlink('./tmp.txt');
37: }
38:
39: compress_img("promotion_main_pic");
40: compress_img("src_thumb");
41: compress_img("uploadImage");
42: compress_img("dialog_image");
43: compress_img("joinPicture");
44: ?>