Rumah > Artikel > pembangunan bahagian belakang > 无损压缩图片心得(二)_PHP教程
用户之前在发烧网参加的上传图片活动都没有经过无损压缩处理.想用脚本对一月内传上去的图片进行处理,但Amazon_S3服务集群上只能使用他们提供的一些简单的API.所以只能先down 下来,压缩处理后,再传上去覆盖原来的图片.
经过多次调试,最终写了个php的脚本对之进行处理:详见这里.
代码如下:
<span class="lnum"> 1: </span><?php
<span class="lnum"> 2: </span><span class="kwrd">function</span> compress_img ($source) {
<span class="lnum"> 3: </span> $exts = array(<span class="str">"png"</span>,<span class="str">"bmp"</span>,<span class="str">"gif"</span>,<span class="str">"pnm"</span>,<span class="str">"tiff"</span>);
<span class="lnum"> 4: </span> $start_time = strtotime(<span class="str">"-30 day"</span>);
<span class="lnum"> 5: </span> exec(<span class="str">"s3cmd ls s3://fever38-us-static/hotdeals/{$source}/ > ./tmp.txt"</span>);
<span class="lnum"> 6: </span> $rs = file(<span class="str">'./tmp.txt'</span>);
<span class="lnum"> 7: </span> 
<span class="lnum"> 8: </span> <span class="kwrd">foreach</span>($rs <span class="kwrd">as</span> $line) {
<span class="lnum"> 9: </span> $r = array_filter(explode(<span class="str">' '</span>, $line));
<span class="lnum"> 10: </span> <span class="kwrd">if</span>(!empty($r[0])){
<span class="lnum"> 11: </span> $r[0] = trim($r[0]);
<span class="lnum"> 12: </span> $time = strtotime($r[0]);
<span class="lnum"> 13: </span> }
<span class="lnum"> 14: </span> <span class="kwrd">if</span>(!empty($time) && $time >= $start_time){
<span class="lnum"> 15: </span> <span class="kwrd">if</span>(!empty($r[10])){
<span class="lnum"> 16: </span> $img = trim($r[10]);
<span class="lnum"> 17: </span> $path_info = pathinfo($r[10]);
<span class="lnum"> 18: </span> $ext = trim($path_info[<span class="str">"extension"</span>]);
<span class="lnum"> 19: </span> $file_name = strtolower(trim($path_info[<span class="str">"basename"</span>]));
<span class="lnum"> 20: </span> 
<span class="lnum"> 21: </span> exec(<span class="str">"s3cmd get "</span>.$img);
<span class="lnum"> 22: </span> exec(<span class="str">"cp {$file_name} /mnt/heisoo/s3/{$source}/"</span>);
<span class="lnum"> 23: </span> 
<span class="lnum"> 24: </span> <span class="kwrd">if</span> (in_array($ext,$exts)) {
<span class="lnum"> 25: </span> system(<span class="str">"/usr/bin/optipng -o5 "</span>.$file_name);
<span class="lnum"> 26: </span> }
<span class="lnum"> 27: </span> <span class="kwrd">if</span> ($ext == <span class="str">"jpg"</span> || $ext == <span class="str">"jpeg"</span>) {
<span class="lnum"> 28: </span> system(<span class="str">"/usr/bin/jpegoptim -o --strip-all "</span>.$file_name);
<span class="lnum"> 29: </span> }
<span class="lnum"> 30: </span> system(<span class="str">"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"</span>);
<span class="lnum"> 31: </span> unlink($file_name);
<span class="lnum"> 32: </span> }
<span class="lnum"> 33: </span> }
<span class="lnum"> 34: </span> }
<span class="lnum"> 35: </span> 
<span class="lnum"> 36: </span> unlink(<span class="str">'./tmp.txt'</span>);
<span class="lnum"> 37: </span>}
<span class="lnum"> 38: </span> 
<span class="lnum"> 39: </span>compress_img(<span class="str">"promotion_main_pic"</span>);
<span class="lnum"> 40: </span>compress_img(<span class="str">"src_thumb"</span>);
<span class="lnum"> 41: </span>compress_img(<span class="str">"uploadImage"</span>);
<span class="lnum"> 42: </span>compress_img(<span class="str">"dialog_image"</span>);
<span class="lnum"> 43: </span>compress_img(<span class="str">"joinPicture"</span>);
<span class="lnum"> 44: </span>?>