tinypng图片压缩

站点页面上占用数据较多的一般是图片,图片的压缩比越大 ,访问页面速度会越快。wordpress下有相关的如WP Smush.it可以用于图片压缩,不过对于压缩比例和保真层度来看,不如tinypng 。其对wordpress也提供了相应的插件,不过免费版每个邮箱用户每月只提供五百个请求的压缩。这里结合其官方API ,做了一个shell版本的小工具,另外根据之前的php上传及tinypng的php API也写了一个web版的工具。

一、shell 版本

通过CURL请求,直接执行请求API URL时:

[root@361way tmp]# curl -i --user api:aSiDFS1VD1UfX7pIPdoDFSvoaYTaAvN --data-binary @delrepo2.png https://api.tinypng.com/shrink
执行结果 HTTP/1.1 100 Continue
HTTP/1.1 201 Created
Cache-Control: no-cache
Compression-Count: 2
Content-Type: application/json; charset=utf-8
Date: Wed, 04 Mar 2015 03:13:34 GMT
Location: https://api.tinypng.com/output/3molv9psquoqou8u.png
Server: Apache/2
Strict-Transport-Security: max-age=31536000
X-Powered-By: Voormedia (voormedia.com/jobs)
Content-Length: 158
Connection: keep-alive
{"input":{"size":10628,"type":"image/png"},"output":{"size":7865,"type":"image/png","ratio":0.74,"url":"https://api.tinypng.com/output/3molv9psquoqou8u.png"}}
<br />

其中Location地址为执行后返回压缩后的图片在服务器上存放的地址。最后可以通过wget或curl 将该地址从远程取回来。

以下是写的shell 版,如下 :

#!/bin/bash
Help()
{
   cat << EOF
   ====================================================================
   Usage:  tinpny.sh  input.png output.png
   ====================================================================
EOF
}
if [ $# != 2 ];then
    Help
else
    httpcont=`date +%s`
    url=`curl --silent -i --user api:aSiDFS1VD1UfX7pIPdoDFSvoaY_TaAvN --data-binary @$1 https://api.tinypng.com/shrink |grep Location|awk '{print $2}'| tr -d '\r'`
    echo $url
    wget $url -O $2
fi
<br />

二、php版

可以先参看PHP图片上传程序(完整版) ,这里使用的是PHP with curl API版,代码如下:


 "https://api.tinypng.com/shrink",
      CURLOPT_USERPWD => "api:" . $key,
      CURLOPT_POSTFIELDS => file_get_contents($input),
      CURLOPT_BINARYTRANSFER => true,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_HEADER => true,
      /* Uncomment below if you have trouble validating our SSL certificate.
         Download cacert.pem from: http://curl.haxx.se/ca/cacert.pem */
      // CURLOPT_CAINFO => __DIR__ . "/cacert.pem",
      CURLOPT_SSL_VERIFYPEER => true
    ));
    $response = curl_exec($request);
    if (curl_getinfo($request, CURLINFO_HTTP_CODE) === 201) {
      /* Compression was successful, retrieve output from Location header. */
      $headers = substr($response, 0, curl_getinfo($request, CURLINFO_HEADER_SIZE));
      foreach (explode("\r\n", $headers) as $header) {
        if (substr($header, 0, 10) === "Location: ") {
          $request = curl_init();
          curl_setopt_array($request, array(
            CURLOPT_URL => substr($header, 10),
            CURLOPT_RETURNTRANSFER => true,
            /* Uncomment below if you have trouble validating our SSL certificate. */
            // CURLOPT_CAINFO => __DIR__ . "/cacert.pem",
            CURLOPT_SSL_VERIFYPEER => true
          ));
          file_put_contents($output, curl_exec($request));
        }
      }
    } else {
        print(curl_error($request));
      /* Something went wrong! */
      print("Compression failed");
    }
}
?>



361way图片上传程序



上传文件:
允许上传的文件类型为:
已经成功上传
文件名: ".$destination_folder.$fname."
"; echo " 宽度:".$image_size[0]; echo " 长度:".$image_size[1]; echo "
大小:".$file["size"]." bytes"; if($watermark==1) { $iinfo=getimagesize($destination,$iinfo); $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); $white=imagecolorallocate($nimage,255,255,255); $black=imagecolorallocate($nimage,0,0,0); $red=imagecolorallocate($nimage,255,0,0); imagefill($nimage,0,0,$white); switch ($iinfo[2]) { case 1: $simage =imagecreatefromgif($destination); break; case 2: $simage =imagecreatefromjpeg($destination); break; case 3: $simage =imagecreatefrompng($destination); break; case 6: $simage =imagecreatefromwbmp($destination); break; default: die("不支持的文件类型"); exit; } imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); switch($watertype) { case 1: //加水印字符串 imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); break; case 2: //加水印图片 $simage1 =imagecreatefromgif("361way.gif"); imagecopy($nimage,$simage1,0,0,0,0,85,15); imagedestroy($simage1); break; } switch ($iinfo[2]) { case 1: //imagegif($nimage, $destination); imagejpeg($nimage, $destination); break; case 2: imagejpeg($nimage, $destination); break; case 3: imagepng($nimage, $destination); break; case 6: imagewbmp($nimage, $destination); //imagejpeg($nimage, $destination); break; } //覆盖原上传文件 imagedestroy($nimage); imagedestroy($simage); } $output=$destination_folder.$upfile_name; Compressimg($destination,$output); unlink($destination); if($imgpreview==1) { echo "
图片预览:
"; echo "\"图片预览:\r文件名:".$output."\r上传时间:\""; } } ?>
<br />

最后:

官方还提供了Java、ruby、node.js、python版的API,本来想使用下python版,不过本地的使用python版本是2.7版本的,而官方提供的是python3.x版本的。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注