首页 > 后端开发 > php教程 > 无损压缩图片心得(二)_PHP教程

无损压缩图片心得(二)_PHP教程

WBOY
发布: 2016-07-20 11:15:27
原创
1104 人浏览过

用户之前在发烧网参加的上传图片活动都没有经过无损压缩处理.想用脚本对一月内传上去的图片进行处理,但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>&#160;
登录后复制
<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>&#160;
登录后复制
<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>&#160;
登录后复制
<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>&#160;
登录后复制
<span class="lnum">  36:  </span>    unlink(<span class="str">'./tmp.txt'</span>);
登录后复制
<span class="lnum">  37:  </span>}
登录后复制
<span class="lnum">  38:  </span>&#160;
登录后复制
<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>?>
登录后复制

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440176.htmlTechArticle用户之前在发烧网参加的上传图片活动都没有经过无损压缩处理.想用脚本对一月内传上去的图片进行处理,但Amazon_S3服务集群上只能使用他...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板