如何使用PhpWord读取.docx和.doc中的图像?
当使用PHPWord读取没有图像的word文件时,我不会遇到任何问题。 一旦它包含一个图像,我就得到空白响应。 我试过使用代码如何使用PhpWord读取.docx和.doc中的图像?,php,phpword,Php,Phpword,当使用PHPWord读取没有图像的word文件时,我不会遇到任何问题。 一旦它包含一个图像,我就得到空白响应。 我试过使用代码 $source = "./62-curriculum-vitae-saisonnier.docx"; $data = ""; $phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'Word2007'); // Adding an empty Section to the document... $section
$source = "./62-curriculum-vitae-saisonnier.docx";
$data = "";
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'Word2007');
// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText($data);
// $name=basename(__FILE__, '.php');
$name="docxfile";
$source = __DIR__ . "/results/{$name}.html";
// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save($source);
$d = new DOMDocument();
$d->loadHTML(file_get_contents($source));
$body = $d->getElementsByTagName('body')->item(0);
$value = $body->nodeValue;
print_r($body->nodeValue);
此外,我尝试了自定义代码,但仍然没有得到解决方案
function read_file_docx($filename) {
$striped_content = '';
$content = '';
if(!$filename || !file_exists($filename)) return false;
$zip = zip_open($filename);
if (!$zip || is_numeric($zip)) return false;
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
if (zip_entry_name($zip_entry) != "word/document.xml") continue;
$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}// end while
zip_close($zip);
$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
$content = str_replace('</w:r></w:p>', "\r\n", $content);
$striped_content = strip_tags($content);
return $striped_content;
}
function read_file_docx($filename){
$striped_content='';
$content='';
如果(!$filename | |!file_存在($filename))返回false;
$zip=zip\u open($filename);
如果(!$zip |是数值($zip))返回false;
而($zip\u entry=zip\u read($zip)){
如果(zip_entry_open($zip,$zip_entry)==FALSE)继续;
如果(zip\u entry\u name($zip\u entry)!=“word/document.xml”)继续;
$content.=zip_entry_read($zip_entry,zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}//结束时
zip_close($zip);
$content=str_replace(“”,$content);
$content=str_replace(“”,\r\n“,$content);
$striped_content=strip_标签($content);
返回$striped_内容;
}