Lossless compression (II) _ PHP Tutorial

Source: Internet
Author: User
Lossless compression image experience (2 ). The image uploading activities that the user has previously participated in have not undergone lossless compression processing. I want to use a script to process images uploaded in January. However, on the Amazon_S3 service cluster, I can only use the images uploaded by my user on the fever network without lossless compression. I want to use scripts to process images uploaded in January, but the Amazon_S3 service cluster can only use some simple APIs they provide. so we can only go down first, compress the data, and then upload it to overwrite the original image.

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

The code is as follows:

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

Upload the uploaded image in March January. However, the amazon_s3service cluster can only use it...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.