thinkphp5 編輯器上傳word轉(zhuǎn)html
php 使用PhpOffice\PhpWord 將word文檔生成html 以及圖片本地化處理 html處理
use think\Db; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\IOFactory; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Shared\ZipArchive; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Shared\Converter; use PhpOffice\PhpWord\Style\TablePosition; use think\Env;
private $savePath = '/upload/word/';
public function woedjx(){
$file = request()->file('file');
$fileurl='http://x.hbsjsd.cn/static/conf/33';
if($file){
$info = $file->move(ROOT_PATH .$fileurl. DS . 'word');
$wordr= $info->getSaveName();
$word=$fileurl.DS.'word'.DS.$wordr;
$objWriter = IOFactory::createWriter(IOFactory::load(ROOT_PATH.$word), 'HTML');
$tempHtml = ROOT_PATH. '/public/static/' .substr($info->getFilename(), 0, strpos($info->getFilename(), '.')) . '.html';
$objWriter->save($tempHtml);
$html = file_get_contents($tempHtml);
$html = $this->getHtmlBody($html);
if ($html == '') {
throw new \Exception('上傳文件內(nèi)容為空');
}
$html = $this->saveImage($html);
$html = $this->clearStyle($html);
$html = $this->clearSpan($html);
}else{
// 上傳失敗獲取錯(cuò)誤信息
echo $file->getError();
}
return json(['code'=>1,"msg"=>$html]);
}
/**
* @param $html
* [url=home.php?mod=space&uid=155549]@Return[/url] string
* 匹配出body的內(nèi)容
*/
private function getHtmlBody($html) {
preg_match('/([\s\S]*)/', $html,$matches);
return isset($matches[1]) ? $matches[1] : '';
}
/**
* @param $html
* @return mixed
* 圖片處理
*/
private function saveImage($html){
//匹配圖片
ini_set('pcre.backtrack_limit', 9999999);
preg_match_all('/]*src="([\s\S]*?)"\/>/', $html,$imageMatches);
if (!$imageMatches[1]) {
return $html;
}
//print_r($imageMatches[1]);exit;
$imageUrl = [];
foreach ($imageMatches[1] as $image) {
$imageUrl[] = $this->saveLocalBase64Image($image);
}
return str_replace($imageMatches[1], $imageUrl, $html);
}
/**
* @param $base64_image_content
* @return string
* 保存圖片到本地
*/
private function saveLocalBase64Image($base64_image_content){
$imge_web_url = '';
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
//圖片后綴
$type = $result[2];
//保存位置--圖片名
$image_name = date('His') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT). '.' . $type; //圖片名稱
$image_file_path ='http://x.hbsjsd.cn/static/conf/33/word/img/'.date('Ymd'); //static/upload/word/image/20200522
$image_file = ROOT_PATH. $image_file_path;
$imge_real_url = $image_file . '/' . $image_name;
$imge_web_url = $image_file_path . '/' . $image_name;
if (!file_exists($image_file)){
mkdir($image_file, 0700, true);
}
//解碼
$decode = base64_decode(str_replace($result[1], '', $base64_image_content));
$res = file_put_contents($imge_real_url, $decode);
return $imge_web_url;
}
return $imge_web_url;
}
/**
* @param $content
* @return null|string|string[]
* 清除p,span標(biāo)簽的樣式
*/
private function clearStyle($content)
{
$patterns = array (
'//i',
);
return preg_replace($patterns, '', $content);
}
/**
* @param $content
* @return null|string|string[]
* 清除span標(biāo)簽
*/
private function clearSpan($content)
{
$patterns = array (
'//i',
'//i',
);
return preg_replace($patterns, '', $content);
}
/**
* @return mixed
*/
public function getErrorMsg()
{
return $this->errorMsg;
}
鄂公網(wǎng)安備42060002000147