用PHP開發一個簡單的批量上傳圖片格式文件轉成WORD小程序

$('#test').click(function(){

//var fileobj = document.getElementById("file").files[0];

var url = 'test2.php';

var file= $('#form1')['0'].files;

var file= document.getElementById('file').files;

var form = new FormData();

for( i=0; i

{

form.append( "file["+i+"]", file[i] );

}

$.ajax( {

url:url,

type:'post',

data:form,

async:false,

cache:false,

contentType:false,

processData:false,

dataType:'json' ,

success:function(res){

console.log( res );

} } );

});

word.php

header("Content-type: text/html; charset=utf-8");

$arr=$_FILES["file"]["tmp_name"];

$arr2=$_FILES["file"]["type"];

foreach($arr2 as $vp){

if(!stristr($vp,'image/')) exit("");

}

foreach($arr as $k=>$v){

$fname=time().rand(100,999);

$houzui = strtolower(pathinfo($_FILES["file"]['name'][$k], PATHINFO_EXTENSION));//獲取後綴

$hz = '.'.strtolower($houzui);

move_uploaded_file($v,"up/" . $fname.$hz);

$pic_arr[]=$fname.$hz;

}

require_once 'orc/AipOcr.php';

// 你的 APPID AK SK

$APP_ID = ' ';

$API_KEY = ' ';

$SECRET_KEY = ' ';

$client = new AipOcr($APP_ID, $API_KEY, $SECRET_KEY);

$sl=0;

foreach($pic_arr as $vs){

$image = file_get_contents('up/'.$vs);

$sl++;

unlink('up/'.$vs);

// 調用通用文字識別, 圖片參數為本地圖片

$jg=$client->basicGeneral($image);

$jgs=$jg['words_result'];

if($jgs){

foreach($jgs as $v)

{

$words.=$v['words'].'
';

}

}

$words.='--------第'.$sl.'張圖內容----------
';

$words.='
';

}

echo '

Print

';

echo '

'.$words.'';

ob_start(); //打開緩衝區

header("Cache-Control: public");

Header("Content-type: application/octet-stream");

Header("Accept-Ranges: bytes");

if (strpos($_SERVER["HTTP_USER_AGENT"],'MSIE')) {

header('Content-Disposition: attachment; filename=OK.doc');

}else if (strpos($_SERVER["HTTP_USER_AGENT"],'Firefox')) {

Header('Content-Disposition: attachment; filename=OK.doc');

} else {

header('Content-Disposition: attachment; filename=OK.doc');

}

header("Pragma:no-cache");

header("Expires:0");

ob_end_flush();//輸出全部內容到瀏覽器

?>

在核心文件中,處理上傳,識別成文字,自動刪除圖片,自動生成WORD分頁,然後彈出下載提示,保存的word文件即時我們需要的內容了。

說明:以上PHP核心文件未考慮安全性,僅供學習使用,若需要投入到真正的使用過程中,請自行增加程序的安全性。

希望大家用來練手,能寫出更好的在線工具,為工作和生活提供更多的便利,也可以留言和我交流。


分享到:


相關文章: