如果做到用户上传带有格式,图片的word文档,读出它的内容。有试过使用phpword,但是只能获取到word文档中的文字部分,图片什么的获取不到(图片地址不是本地文件)。想问问大家有没有其他的方法实现
1 回答
守候你守候我
TA贡献1802条经验 获得超10个赞
PHPExcel_Worksheet_Drawing
了解一下,下面的copy的一份代码
<?php ini_set("display_errors",1); include 'Classes/PHPExcel.php';include 'Classes/PHPExcel/IOFactory.php'; define('EXCEL_EXTENSION_2003', "xls"); define('EXCEL_EXTENSION_2007', "xlsx"); $fileName2003 = "Standard_Format_File1.xls"; $fileName2007 = "Standard_Format_File1.xlsx"; $fileName = $fileName2003;//$fileName = $fileName2007; if(getExtendFileName($fileName) == EXCEL_EXTENSION_2003) { $reader = PHPExcel_IOFactory::createReader('Excel5'); }else if(getExtendFileName($fileName) == EXCEL_EXTENSION_2007) { $reader = new PHPExcel_Reader_Excel2007(); } $PHPExcel = $reader->load($fileName); $worksheet = $PHPExcel->getActiveSheet(); $imageInfo = extractImageFromWorksheet($worksheet,"importImg/"); print_r($imageInfo); function getExtendFileName($file_name) { $extend = pathinfo($file_name); $extend = strtolower($extend["extension"]); return $extend; } function extractImageFromWorksheet($worksheet,$basePath){ $result = array(); $imageFileName = ""; foreach ($worksheet->getDrawingCollection() as $drawing) { $xy=$drawing->getCoordinates(); $path = $basePath; // for xlsx if ($drawing instanceof PHPExcel_Worksheet_Drawing) { $filename = $drawing->getPath(); $imageFileName = $drawing->getIndexedFilename(); $path = $path . $drawing->getIndexedFilename(); copy($filename, $path); $result[$xy] = $path; // for xls } else if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) { $image = $drawing->getImageResource(); $renderingFunction = $drawing->getRenderingFunction(); switch ($renderingFunction) { case PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG: $imageFileName = $drawing->getIndexedFilename(); $path = $path . $drawing->getIndexedFilename(); imagejpeg($image, $path); break; case PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF: $imageFileName = $drawing->getIndexedFilename(); $path = $path . $drawing->getIndexedFilename(); imagegif($image, $path); break; case PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG: $imageFileName = $drawing->getIndexedFilename(); $path = $path . $drawing->getIndexedFilename(); imagegif($image, $path); break; case PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT: $imageFileName = $drawing->getIndexedFilename(); $path = $path . $drawing->getIndexedFilename(); imagegif($image, $path); break; } $result[$xy] = $imageFileName; } } return $result; }
- 1 回答
- 0 关注
- 2420 浏览
添加回答
举报
0/150
提交
取消