diff --git a/application/controllers/info_amp.php b/application/controllers/info_amp.php index c1d031df..3f2fca4e 100644 --- a/application/controllers/info_amp.php +++ b/application/controllers/info_amp.php @@ -362,25 +362,42 @@ class Info_amp extends CI_Controller public function handel_word_source() { $file = $_FILES['file']; - if (!$file) return; + if (!$file) { + echo(json_encode(array( + 'status' => 'error', + 'msg' => "文件上传失败。" + ))); + return; + } //转移文件 - $md5_name = md5($file["name"]).strrchr($file["name"],'.'); + $md5_file = md5($file['name'].$file['size']); + //ext + $ext = mb_strtolower(strrchr($file["name"],'.')); + if ($ext != '.docx') { + echo(json_encode(array( + 'status' => 'error', + 'msg' => '只能接收word(.docx)文档。' + ))); + return; + } + $md5_name = $md5_file.$ext; $path = realpath('../cht-system/views/information/word_source').'/'.basename($md5_name); if (move_uploaded_file($file['tmp_name'], $path)) { - $msg = "file path: ".$path; - } else { - $msg = "file upload error!"; - } - - if($file) { + $this->load->library('wordphp'); + $rt = new WordPHP(); + $rt->img_dir = realpath('../cht-system/views/information/word_img'); + $rt->img_prex = $md5_file; + $html = $rt->readDocument($path); + $html = str_ireplace($rt->img_dir, '//202.103.68.62:9096/information-view/information/word_img', $html); echo(json_encode(array( 'status' => 'success', - 'msg' => $msg + 'path' => $path, + 'html' => $html ))); } else { echo(json_encode(array( 'status' => 'error', - 'msg' => $msg + 'msg' => "word文件解析失败。" ))); } } diff --git a/application/libraries/wordphp.php b/application/libraries/wordphp.php index 71f6a497..a240782c 100644 --- a/application/libraries/wordphp.php +++ b/application/libraries/wordphp.php @@ -37,10 +37,13 @@ class WordPHP private $file; private $rels_xml; private $doc_xml; - private $doc_media = []; + private $doc_media = array(); private $last = 'none'; private $encoding = 'ISO-8859-1'; private $tmpDir = 'tmp'; + //图片目录和前缀 + public $img_dir = ''; + public $img_prex = ''; /** * CONSTRUCTOR @@ -167,7 +170,8 @@ class WordPHP if($reader->name == "w:shd" && $reader->getAttribute("w:val") != "clear" && $reader->getAttribute("w:fill") != "000000") { $f .="background-color: #".$reader->getAttribute("w:fill").","; } - if($reader->name == 'w:drawing' && !empty($reader->readInnerXml())) { + $temp_rx = $reader->readInnerXml(); + if($reader->name == 'w:drawing' && !empty($temp_rx)) { $r = $this->checkImageFormating($reader); $img = $r !== null ? "" : null; } @@ -283,7 +287,13 @@ class WordPHP $ext = strtolower($arr[$l-1]); $im = imagecreatefromstring($image); - $fname = $this->tmpDir.'/tmp/'.$relId.'.'.$ext; + if (empty($this->img_dir)) { + $fname = $this->tmpDir.'/tmp/'.$relId.'.'.$ext; + } else { + $ext = str_ireplace('jpeg', 'jpg', $ext); + $fname = $this->img_dir.'/'.$this->img_prex.'_'.mb_strtolower($relId).'.'.$ext; + } + switch ($ext) { case 'png': @@ -404,7 +414,7 @@ class WordPHP $tc2 = new SimpleXMLElement($tc->readOuterXML()); foreach ($tc2->children('w',true) as $ch) { - if (in_array($ch->getName(), ['left','top','botom','right']) ) { + if (in_array($ch->getName(), array('left','top','botom','right'))) { $line = $this->convertLine($ch['val']); $style .= " border-".$ch->getName().":".$ch['sz']."px $line #".$ch['color'].";"; } @@ -418,16 +428,16 @@ class WordPHP private function convertLine($in) { - if (in_array($in, ['dotted'])) + if (in_array($in, array('dotted'))) return "dashed"; - if (in_array($in, ['dotDash','dotdotDash','dotted','dashDotStroked','dashed','dashSmallGap'])) + if (in_array($in, array('dotDash','dotdotDash','dotted','dashDotStroked','dashed','dashSmallGap'))) return "dashed"; - if (in_array($in, ['double','triple','threeDEmboss','threeDEngrave','thick'])) + if (in_array($in, array('double','triple','threeDEmboss','threeDEngrave','thick'))) return "double"; - if (in_array($in, ['nil','none'])) + if (in_array($in, array('nil','none'))) return "none"; return "solid";