当前位置:Gxlcms > PHP教程 > 无损压缩图片心得(二)_PHP教程

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

时间:2021-07-01 10:21:17 帮助过:17人阅读

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

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

代码如下:

   1:  

  
   2:  function compress_img ($source) {
   3:      $exts = array("png","bmp","gif","pnm","tiff");
   4:      $start_time = strtotime("-30 day");
   5:      exec("s3cmd ls s3://fever38-us-static/hotdeals/{$source}/ > ./tmp.txt");
   6:      $rs = file('./tmp.txt');
   7:   
   8:      foreach($rs as $line) {
   9:          $r =  array_filter(explode(' ', $line));
  10:          if(!empty($r[0])){
  11:              $r[0] = trim($r[0]);
  12:              $time = strtotime($r[0]);
  13:          }
  14:          if(!empty($time) && $time >= $start_time){
  15:              if(!empty($r[10])){
  16:                  $img = trim($r[10]);
  17:                  $path_info = pathinfo($r[10]);
  18:                  $ext = trim($path_info["extension"]);
  19:                  $file_name = strtolower(trim($path_info["basename"]));
  20:   
  21:                  exec("s3cmd get ".$img);
  22:                  exec("cp {$file_name} /mnt/heisoo/s3/{$source}/");
  23:   
  24:                  if (in_array($ext,$exts)) {
  25:                      system("/usr/bin/optipng -o5 ".$file_name);
  26:                  }
  27:                  if ($ext == "jpg" || $ext == "jpeg") {
  28:                      system("/usr/bin/jpegoptim -o --strip-all ".$file_name);
  29:                  }
  30:                  system("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");
  31:                  unlink($file_name);
  32:              }
  33:          }
  34:      }
  35:   
  36:      unlink('./tmp.txt');
  37:  }
  38:   
  39:  compress_img("promotion_main_pic");
  40:  compress_img("src_thumb");
  41:  compress_img("uploadImage");
  42:  compress_img("dialog_image");
  43:  compress_img("joinPicture");
  44:  ?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440176.htmlTechArticle用户之前在发烧网参加的上传图片活动都没有经过无损压缩处理.想用脚本对一月内传上去的图片进行处理,但Amazon_S3服务集群上只能使用他...

人气教程排行