Cannot extract images from EXCEL file (xls)

Topics: Developer Forum, User Forum
Aug 18, 2014 at 9:11 AM
I am using PHp Excel

could extract / read data but cannot extract images

TEXT


which allows me to read excel file (only data)
but do not extract images

My code
<?php
include 'Classes/PHPExcel/IOFactory.php';

$inputFileName = 'advent-bb-products.xls';

// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}

$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
$array_data = array();
foreach ($rowIterator as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$rowIndex = $row->getRowIndex();
$array_data[$rowIndex] = array('A' => '', 'B' => '', 'C' => '', 'D' => '', 'E' => '', 'F' => '', 'G' => '');

foreach ($cellIterator as $cell) {
    if ('A' == $cell->getColumn()) {
        $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
    } else if ('B' == $cell->getColumn()) {
        $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
    } else if ('C' == $cell->getColumn()) {
        $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
    } else if ('D' == $cell->getColumn()) {
        $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
    } else if ('E' == $cell->getColumn()) {
        $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
    } else if ('F' == $cell->getColumn()) {
        $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
    } else if ('G' == $cell->getColumn()) {
        $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
    }
}
}

echo '<pre>';
// displays the row wise cell values

print_r($array_data);

echo '<br> extracting images from work sheet'; ////////////////////////////////////////////////
///////////// this code I had used from https://phpexcel.codeplex.com/discussions/257231

$worksheet = $PHPExcel->getActiveSheet();

// extract images from worksheet and save files: 0.jpeg, 1.jpeg, 2.png, ...
foreach ($worksheet->getDrawingCollection() as $i => $drawing) {
$filename = $drawing->getPath();
$imagesize = getimagesize($filename);

switch ($imagesize[2]) {

case 1:
    $image = imagecreatefromgif($filename);
    imagegif($image, "$i.gif");
    break;

case 2:
    $image = imagecreatefromjpeg($filename);
    imagejpeg($image, "$i.jpeg");
    break;

case 3:
    $image = imagecreatefrompng($filename);
    imagepng($image, "$i.png");
    break;

default:
    continue 2;

}
}
?>
Coordinator
Aug 18, 2014 at 10:52 AM
Have you read the developer docs? There's a section (4.6.37 entitled Reading Images from a worksheet) specifically explaining how to extract images.
$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();
        switch ($drawing->getMimeType()) {
            case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG :
                    $extension = 'png'; break;
            case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_GIF:
                    $extension = 'gif'; break;
            case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG :
                    $extension = 'jpg'; break;
        }
    } 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);
}