Home  >  Article  >  Backend Development  >  Experience on Lossless Compression of Images (2)_PHP Tutorial

Experience on Lossless Compression of Images (2)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 11:15:271080browse

None of the image upload activities that the user participated in before on Fever.com have been processed by lossless compression. I want to use a script to process the images uploaded within one month, but the Amazon_S3 service cluster can only use some simple ones they provide. API. So we can only download it first, compress it, and then upload it to overwrite the original image.

After many debuggings, I finally wrote a php script to process it: see here for details.

The code is as follows:

<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.htmlTechArticleNone of the image upload activities that users participated in before on Fever.com have been processed by lossless compression. I want to use a script to compress the images within one month. The uploaded images are processed, but only he can be used on the Amazon_S3 service cluster...
Statement:
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