使用PHP在Excel文件(xls)中提取图片/图像

时间:2022-10-23 09:48:26

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导入一个电子表格。我可以使用PHPExcel导入计算单元数据,但是我不知道如何在电子表格中使用图像。

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

有没有一种方法可以做到这一点,然后用PHP中的图像保存到服务器上?

Many thanks for the help! :)

非常感谢你的帮助!:)


Update:

更新:

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

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

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

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

$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();
    }
}

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

然后我可以输出JPEG标题和$imageContents的内容来显示图像。

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

如何在电子表格中获取图像的实际名称(如“Picture1”)?是否可以使用PHPExcel_Worksheet_MemoryDrawing?

I can't thank you enough!

我感激不尽!

2 个解决方案

#1


3  

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.

我不知道是不是你,有人在PHPExcel板上问过类似的问题……我还没来得及回答。

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

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

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

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.

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

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

注意,也可能有与打印头相关的图像对象:

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

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.

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

EDIT

编辑

Based on your code in the modified question:

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

$drawing->getName();

should give you what you need

应该给你所需要的吗

#2


0  

You can do the following :

你可以做以下事情:

$im = {excel data} <br>
header("Content-type: image/jpeg"); // or whatever <Br>
$image = imagejpeg($im, NULL, 100);

#1


3  

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.

我不知道是不是你,有人在PHPExcel板上问过类似的问题……我还没来得及回答。

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

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

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

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.

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

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

注意,也可能有与打印头相关的图像对象:

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

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.

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

EDIT

编辑

Based on your code in the modified question:

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

$drawing->getName();

should give you what you need

应该给你所需要的吗

#2


0  

You can do the following :

你可以做以下事情:

$im = {excel data} <br>
header("Content-type: image/jpeg"); // or whatever <Br>
$image = imagejpeg($im, NULL, 100);