Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving image in PHPExcel

Tags:

php

phpexcel


I want to read/retrieve an Image from excel file to PHP using PHPExcel. This code is used to retrieve a value from a particular cell.

    $objPHPExcel->getActiveSheet()->getCell('B5')->getValue();

This retrieves only the cell value, But i cant retrieve a image. Is there a way to do that?

like image 369
Salman Avatar asked Oct 25 '11 09:10

Salman


People also ask

How to read images from Excel data using PHP?

There are two readers that supports Image reads facility right now from PHPExcel API. The reading of Excel cells data using PHP is simple and many example can be found on PHPExcel API Official sites. Lets start with reading images from Excel Sheet using PHPExcel with 2007 reader class XLSX will be as follows.

How to extract images from an Excel file using HTML?

The excel_template folder contains an input Excel file with image URLs. This example code loads this file to extract images from the URL. Instead of using this fixed excel template, you can also allow users to choose an excel file. By adding a HTML form with a file input option user can choose their excel to explore extract.

How to extract images from a URL in PHP?

PHP contains built-in functions for extracting data including the images with a URL. This article is for PHP code to extract images from URLs existing in an excel file. I have used PhpSpreadsheet to read the URLs from an Excel file. Then, I created cURL script to extract images from the URL.

How to retrieve images from the header/footer using PHP?

The getName () and getDescription () methods can be used to retrieve the relevant values fro the image object. can be used to retrieve images from the header/footer. This is an array of PHPExcel_Worksheet_HeaderFooterDrawing objects.


2 Answers

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.

like image 71
DaveRandom Avatar answered Sep 30 '22 00:09

DaveRandom


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

like image 33
juanmanuele Avatar answered Oct 03 '22 00:10

juanmanuele