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

Experience on Lossless Compression of Images (2)_PHP Tutorial

WBOY
Release: 2016-07-20 11:15:27
Original
1106 people have browsed it

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
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

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...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template