襄阳做网站,襄阳网站设计,襄阳网站开发,微信小程序开发,网站设计制作,专业建站公司

当前位置:

thinkphp5 编辑器上传word转html

常见问题

1943

文章来源(hbsjsd.cn)湖北高端网站定制开发公司-速建时代

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('上传文件内容为空');
        }
        $html = $this->saveImage($html);
        $html = $this->clearStyle($html);
        $html = $this->clearSpan($html);
    }else{
        // 上传失败获取错误信息
        echo $file->getError();
    }

    return json(['code'=>1,"msg"=>$html]);
}

/**
 * @param $html
 * [url=home.php?mod=space&uid=155549]@Return[/url] string
 * 匹配出body的内容
 */
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标签的样式
 */
private function clearStyle($content)
{
    $patterns = array (
        '//i',
    );
    return preg_replace($patterns, '', $content);
}

/**
 * @param $content
 * @return null|string|string[]
 * 清除span标签
 */
private function clearSpan($content)
{
    $patterns = array (
        '//i',
        '//i',
    );
    return preg_replace($patterns, '', $content);
}

/**
 * @return mixed
 */
public function getErrorMsg()
{
    return $this->errorMsg;
}

[声明]原创不易,请转发者备注下文章来源(hbsjsd.cn)【速建时代】。

速建时代高端网站定制开发回到顶部
站内SEO关键词搜索