Php下使用Curl調用PITBIT的PNG圖片壓縮功能

(轉載)原文地址: http://www.pitbit.cn/develperManual.html#php_curl_png

1、初始化獲取會話標籤

$serverIP='http://www.pitbit.cn';
$url = $serverIP . '/initevn.php'; 

header('Content-type:text/html; charset=utf-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);//用GET方式

preg_match('/Set-Cookie:(.*);/iU',$response,$str);
$cookie = $str[1];
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
$array = json_decode($body, 1) ;//第二參數爲1表示轉化爲數組
$id_part=$array[0]['id_part'];
$sess_Part=$array[0]['sess_Part'];
curl_setopt($ch,CURLOPT_COOKIE,$cookie);//設置cookie
echo $id_part . ';' . $sess_Part . ';' . $cookie ;

 

2、批量提交圖片文件

$curlPost = array('id_part'=>$id_part,//
'method'=>1,//壓縮方式:0、1
'level'=>40,//壓縮級別:10、20、30、40、50、60、70、80、90、100
'size'=>100,//壓縮目標大小(KB),用戶自定義
'count'=>10,//嘗試次數
'unit'=>'kb',
'resize'=>'false',//是否改變尺寸
'picWidth'=>0,//改變的目標像素寬度
'picHeight'=>0,//改變的目標像素高度
'file[0]'=> '@D:/png1.png' ,//文件名1,支持批量提交
'file[1]'=> '@D:/png2.png' ,//文件名2,支持批量提交
);

curl_setopt($ch, CURLOPT_URL, $serverIP . '/upload_pngfile.php');
curl_setopt($ch, CURLOPT_POST, 1); //POST提交
curl_setopt($ch, CURLOPT_POSTFIELDS,$curlPost);
$response = curl_exec($ch) ;

其中$response包含了壓縮包的名稱,用戶可以從http://www.pitbit.cn/download下載壓縮包

 

3、查詢壓縮情況

curl_setopt($ch, CURLOPT_URL, $serverIP . '/getfilestate.php');
$curlPost = array( 'id_part'=>$id_part);
curl_setopt($ch, CURLOPT_POST, 1); //POST提交
curl_setopt($ch, CURLOPT_POSTFIELDS,$curlPost);
$response = curl_exec($ch) ;

其中$response包含了當前完成壓縮的圖片的列表,若所有圖片名稱均列出,表明本次壓縮完成

 

4、清理服務器數據

curl_setopt($ch, CURLOPT_URL, $serverIP . '/clearevn.php');
$curlPost = array( 'id_parts'=>$id_part);
//可以添加多個id_part並用分號隔開,例如:'id_parts'=>$id_part1.';'.$id_part2.';'.$id_part3
curl_setopt($ch, CURLOPT_POST, 1); //POST提交
curl_setopt($ch, CURLOPT_POSTFIELDS,$curlPost);
curl_exec($ch); 
curl_close($ch);

下載壓縮包後需要調用clearevn.php以釋放服務器空間

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章