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?
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.
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.
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.
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.
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
orPHPExcel_Worksheet_MemoryDrawing
objects: you can identify which usingis_a()
. You can then use the methods appropriate to that class (as described in the API) either to read the image data from file (forPHPExcel_Worksheet_Drawing
objects) or directly from thePHPExcel_Worksheet_MemoryDrawing
object itself. ThegetName()
andgetDescription()
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 ofPHPExcel_Worksheet_HeaderFooterDrawing
objects. All thePHPExcel_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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With