Home > php教程 > php手册 > 无损压缩图片心得(二)

无损压缩图片心得(二)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:31:15
Original
1056 people have browsed it

用户之前在发烧网参加的上传图片活动都没有经过无损压缩处理.想用脚本对一月内传上去的图片进行处理,但Amazon_S3服务集群上只能使用他们提供的一些简单的API.所以只能先down 下来,压缩处理后,再传上去覆盖原来的图片.

经过多次调试,最终写了个php的脚本对之进行处理:详见这里.

代码如下:

<span class="lnum">   1:  </span><?php
Copy after login
<span class="lnum">   2:  </span><span class="kwrd">function</span> compress_img ($source) {
Copy after login
<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>);
Copy after login
<span class="lnum">   4:  </span>    $start_time = strtotime(<span class="str">"-30 day"</span>);
Copy after login
<span class="lnum">   5:  </span>    exec(<span class="str">"s3cmd ls s3://fever38-us-static/hotdeals/{$source}/ > ./tmp.txt"</span>);
Copy after login
<span class="lnum">   6:  </span>    $rs = file(<span class="str">'./tmp.txt'</span>);
Copy after login
<span class="lnum">   7:  </span>&#160;
Copy after login
<span class="lnum">   8:  </span>    <span class="kwrd">foreach</span>($rs <span class="kwrd">as</span> $line) {
Copy after login
<span class="lnum">   9:  </span>        $r =  array_filter(explode(<span class="str">' '</span>, $line));
Copy after login
<span class="lnum">  10:  </span>        <span class="kwrd">if</span>(!empty($r[0])){
Copy after login
<span class="lnum">  11:  </span>            $r[0] = trim($r[0]);
Copy after login
<span class="lnum">  12:  </span>            $time = strtotime($r[0]);
Copy after login
<span class="lnum">  13:  </span>        }
Copy after login
<span class="lnum">  14:  </span>        <span class="kwrd">if</span>(!empty($time) && $time >= $start_time){
Copy after login
<span class="lnum">  15:  </span>            <span class="kwrd">if</span>(!empty($r[10])){
Copy after login
<span class="lnum">  16:  </span>                $img = trim($r[10]);
Copy after login
<span class="lnum">  17:  </span>                $path_info = pathinfo($r[10]);
Copy after login
<span class="lnum">  18:  </span>                $ext = trim($path_info[<span class="str">"extension"</span>]);
Copy after login
<span class="lnum">  19:  </span>                $file_name = strtolower(trim($path_info[<span class="str">"basename"</span>]));
Copy after login
<span class="lnum">  20:  </span>&#160;
Copy after login
<span class="lnum">  21:  </span>                exec(<span class="str">"s3cmd get "</span>.$img);
Copy after login
<span class="lnum">  22:  </span>                exec(<span class="str">"cp {$file_name} /mnt/heisoo/s3/{$source}/"</span>);
Copy after login
<span class="lnum">  23:  </span>&#160;
Copy after login
<span class="lnum">  24:  </span>                <span class="kwrd">if</span> (in_array($ext,$exts)) {
Copy after login
<span class="lnum">  25:  </span>                    system(<span class="str">"/usr/bin/optipng -o5 "</span>.$file_name);
Copy after login
<span class="lnum">  26:  </span>                }
Copy after login
<span class="lnum">  27:  </span>                <span class="kwrd">if</span> ($ext == <span class="str">"jpg"</span> || $ext == <span class="str">"jpeg"</span>) {
Copy after login
<span class="lnum">  28:  </span>                    system(<span class="str">"/usr/bin/jpegoptim -o --strip-all "</span>.$file_name);
Copy after login
<span class="lnum">  29:  </span>                }
Copy after login
<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>);
Copy after login
<span class="lnum">  31:  </span>                unlink($file_name);
Copy after login
<span class="lnum">  32:  </span>            }
Copy after login
<span class="lnum">  33:  </span>        }
Copy after login
<span class="lnum">  34:  </span>    }
Copy after login
<span class="lnum">  35:  </span>&#160;
Copy after login
<span class="lnum">  36:  </span>    unlink(<span class="str">'./tmp.txt'</span>);
Copy after login
<span class="lnum">  37:  </span>}
Copy after login
<span class="lnum">  38:  </span>&#160;
Copy after login
<span class="lnum">  39:  </span>compress_img(<span class="str">"promotion_main_pic"</span>);
Copy after login
<span class="lnum">  40:  </span>compress_img(<span class="str">"src_thumb"</span>);
Copy after login
<span class="lnum">  41:  </span>compress_img(<span class="str">"uploadImage"</span>);
Copy after login
<span class="lnum">  42:  </span>compress_img(<span class="str">"dialog_image"</span>);
Copy after login
<span class="lnum">  43:  </span>compress_img(<span class="str">"joinPicture"</span>);
Copy after login
<span class="lnum">  44:  </span>?>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template