Retrieving image in PHPExcel

Googling phpexcel read image yielded this page as the second result. It tells you how to do this.

To quote the relevant info directly:

$objPHPExcel->getActiveSheet()->getDrawingCollection() will return an ArrayObject of all the image objects in the active worksheet.

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 from the image object.

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.


Check this example. I found it useful ..

$objPHPExcel = PHPExcel_IOFactory::load($_FILES['archivo']['tmp_name']);

$i = 0;
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();
        $extension = 'png';
    } else {
        $zipReader = fopen($drawing->getPath(),'r');
        $imageContents = '';

        while (!feof($zipReader)) {
            $imageContents .= fread($zipReader,1024);
        }
        fclose($zipReader);
        $extension = $drawing->getExtension();
    }
    $myFileName = '00_Image_'.++$i.'.'.$extension;
    file_put_contents($myFileName,$imageContents);
}

Source: http://phpexcel.codeplex.com/workitem/18189

Tags:

Php

Phpexcel