file("story_img");;
// 4, 判断文件上传的结果
if($result) {
// 上传成功
$imageUrl="./Public/Admin/images/images/";
$zip = new \ZipArchive;
$word=$zip->open('Uploads/'.$result);
if($word==true){
for ($i = 0; $i < $zip->numFiles; $i++) {
/* 用zip压缩读取word中内容 */
$zipElement = $zip->statIndex($i);
/* 读取图片 */
if (preg_match("([^\s]+(\.(?i)(jpg|jpeg|png|gif|bmp))$)", $zipElement['name'])) {
$rand = uniqid();
$file = "{$rand}.png";
$imageFile =$imageUrl. "{$file}";
$im = imagecreatefromstring($zip->getFromIndex($i));
//生成处理过图片,给模糊接口使用
$imWidth = imagesx($im);
$imHeight = imagesy($im);
if ($imWidth > 580) {
$fixWidth = 580; //生成图片的固定宽度
$newHeight = intval($fixWidth / $imWidth * $imHeight);
$newIm = imagecreatetruecolor($fixWidth, $newHeight);
imagecopyresampled($newIm, $im, 0, 0, 0, 0, $fixWidth, $newHeight, $imWidth, $imHeight); //调整图片大小
imagepng($newIm, $imageFile); //生成图片
imagedestroy($im);
imagedestroy($newIm);
} else {
imagepng($im, $imageFile); //生成图片
imagedestroy($im);
}
$a=$this->getFileName($zipElement['name']);
$existImages[$a] = '/Public/Admin/images/images/'.$file;
} else {
//word内容,父级xml
if ($zipElement['name'] == "word/document.xml") {
$content .= $zip->getFromIndex($i);
}
//子级xml
if ($zipElement['name'] == "word/_rels/document.xml.rels") {
$childContent .= $zip->getFromIndex($i);
}
}
}
}
$zip->close();
$zip = null;
$documentContentFix = array('content' => $content, 'childContent' => $childContent, 'existImages' => $existImages);
$content=$this->dealWithContent($documentContentFix);
$pattern = '/
/';
preg_match_all($pattern,$content,$info);
foreach($info as $key => $value){
$title[]=$value[0];
}
$title=$title['1'];
$title=str_replace('>','',$title);
$userinfo=session('user');
$username=$userinfo['username'];
$data=array(
'filename'=>$result,
'imagename'=>$file,
'username'=>$username,
'title'=>$title,
'time'=>time());
M('Fileload')->add($data);
$this->ajaxReturn($content, 1, $_FILES['upfiledata']['name']);
}else {
// 上传失败
echo 333;
die;
}
}
public function dealWithContent($documentContentFix) {
$childContent = $documentContentFix['childContent'];
$existImages = $documentContentFix['existImages'];
$content = $documentContentFix['content'];
$search = array();
$replace = array();
//替换中出现图片位置
preg_match_all('//', $childContent, $matchs);
$keys = $matchs['1']; //id里面内容
$values = $matchs['2']; //word中资源位置及名称
foreach ($keys as $key => $imageKey) {
$imageArrayKey = $this->getFileName($values[$key]);
//判断获取到的资源是否为图片,并且存在
if (array_key_exists($key, $values) && array_key_exists($imageArrayKey, $existImages)) {
$search[] = "
{$imageKey}";
$replace[] = "
";
}
}
//提取xml正文
$content = preg_match("/(.*?)/", $content, $matches);
$content = $matches['1'];
$content = preg_replace('/.*?/', "", $content);
$content = preg_replace('/.*?/', "", $content);
$content = preg_replace('/|/', "", $content);
$content = preg_replace('/.*?/', "", $content);
$content = preg_replace('/.*?/', "", $content);
$content = preg_replace('/()?(
)?/', "", $content);
$content = preg_replace('/()?(
)?/', "\r\n", $content);
$content = preg_replace('//', "\r\n", $content);
$content = preg_replace('//', "\r\n", $content);
$content = preg_replace('/
/', "
", $content);
$content = preg_replace('/
/', "
", $content);
$content = preg_replace('//', "", $content);
$content = preg_replace('//', "", $content);
$content = preg_replace('//', "\r\n", $content);
$content = preg_replace('/.*?/', "", $content);
$content = preg_replace('/.*?/', "
$1", $content);
$content = preg_replace('/.*?/', "", $content);
$content = preg_replace('//', "", $content);
//var_dump($search);die;
return str_replace($search, $replace, $content); //替换图片位置
}
public function getFileName($file, $md5 = TRUE) {
$fileName = basename($file);
return $md5 ? md5($fileName) : $fileName;
}
public function ajaxReturn($msg, $status = 0, $name = '') {
echo json_encode(array('data' => $msg, 'status' => $status, 'name' => $name));
exit();
}
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved.
京ICP备09083238号