使用PHP提取Excel文件(xls)中的图片/图像 [英] Extracting pictures/images within an Excel file (xls) using PHP

查看:741
本文介绍了使用PHP提取Excel文件(xls)中的图片/图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要使用PHP导入的电子表格.我可以使用PHPExcel导入单元格数据,但无法弄清楚如何从电子表格中使用图像.

I have a spreadsheet that I would like to import using PHP. I can import the cell data using PHPExcel, but can't figure out how to use images from within the spreadsheet.

有没有一种方法,然后使用PHP中的图像保存到服务器等?

Is there a way of doing this and then using the images within PHP to save to the server etc?

非常感谢您的帮助! :)

Many thanks for the help! :)

更新:

@ mark-baker-非常感谢您的帮助!

@mark-baker - thank you so much for your help with this!

我在带有一个JPG的测试XLS文件上使用了以下代码:

I have used the code below on a test XLS file with one JPG:

$objPHPExcel = PHPExcel_IOFactory::load("SH.xls");

foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing) {
    if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
        ob_start();
        call_user_func(
            $drawing->getRenderingFunction(),
            $drawing->getImageResource()
        );
        $imageContents = ob_get_contents();
        ob_end_clean();
    }
}

我想我可以输出JPEG标头和$imageContents的内容来显示图像.

I think I can then output JPEG headers and the contents of $imageContents to show the image.

如何通过"Picture1"获取电子表格中图像的实际名称? PHPExcel_Worksheet_MemoryDrawing是否可能?

How would I get the actual name of the image in the spreadsheet though eg "Picture1"? Is this possible with PHPExcel_Worksheet_MemoryDrawing?

我感激不尽!

推荐答案

有人,我不知道是否是你自己,已经在PHPExcel板上提出了类似的问题……我还没有完全回答的办法.

Somebody, I don't know if it's yourself, has asked a similar question on the PHPExcel board... that I haven't got round to answering yet.

$objPHPExcel->getActiveSheet()->getDrawingCollection()

将返回活动工作表中所有图像对象的ArrayObject.

will return an ArrayObject of all the image objects in the active worksheet.

这些对象将是PHPExcel_Worksheet_Drawing或PHPExcel_Worksheet_MemoryDrawing对象:您可以使用 is_a().然后,您可以使用适合该类的方法(如API中所述)从文件(对于PHPExcel_Worksheet_Drawing对象)读取图像数据,或者直接从PHPExcel_Worksheet_MemoryDrawing对象本身读取图像数据. getName()和getDescription()方法可用于检索图像对象的相关值.

These objects will be either PHPExcel_Worksheet_Drawing or PHPExcel_Worksheet_MemoryDrawing objects: you can identify which using is_a(). You can then use the methods appropriate to that class (as described in the API) either to read the image data from file (for PHPExcel_Worksheet_Drawing objects) or directly from the PHPExcel_Worksheet_MemoryDrawing object itself. The getName() and getDescription() methods can be used to retrieve the relevant values fro the image object.

请注意,还可以将图像对象与打印头相关联:

Note that it's also possible to have image objects associated with print headers:

$objPHPExcel->getActiveSheet()->getHeaderFooter()->getImages()

可用于从页眉/页脚检索图像.这是一个PHPExcel_Worksheet_HeaderFooterDrawing对象的数组.所有PHPExcel_Worksheet_Drawing方法都可用于从这些对象中提取图像文件.

can be used to retrieve images from the header/footer. This is an array of PHPExcel_Worksheet_HeaderFooterDrawing objects. All the PHPExcel_Worksheet_Drawing methods can be used to extract the image file from these objects.

编辑

根据您修改后的问题中的代码:

Based on your code in the modified question:

$drawing->getName();

应该给您您所需要的

这篇关于使用PHP提取Excel文件(xls)中的图片/图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆