Extracting images / images in Excel file (xls) using PHP

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

Is there a way to do this, and then use images in PHP to save to the server, etc.

Many thanks for the help!:)


Update:

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

I used the code below in 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(); } } 

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

How to get the actual image name in a spreadsheet, for example, "Picture1"? Is this possible with PHPExcel_Worksheet_MemoryDrawing?

I can not thank you!

+6
source share
2 answers

Someone, I don’t know if he himself asked a similar question on the PHPExcel board ... that I have not yet received an answer to the question.

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

will return an ArrayObject of all image objects on the active sheet.

These objects will be either PHPExcel_Worksheet_Drawing objects or PHPExcel_Worksheet_MemoryDrawing objects: you can define using is_a () . Then you can use the methods corresponding to this class (as described in the API), either to read image data from a file (for PHPExcel_Worksheet_Drawing objects) or directly from the PHPExcel_Worksheet_MemoryDrawing object. The getName () and getDescription () methods can be used to retrieve the appropriate values ​​for the image object.

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

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

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

EDIT

Based on your code in a modified question:

 $drawing->getName(); 

should provide you with what you need

+3
source

You can do the following:

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

Source: https://habr.com/ru/post/888933/


All Articles