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以释放服务器空间

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