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-
class ExcelImport
{
/**
* @var
*/
protected $excel;
/**
* @var
*/
protected $work_sheet;
/**
* @var array
*/
protected $excel_data = [];
/**
* ExcelImport constructor.
* @param Request $request
* @throws \PHPExcel_Exception
* @throws \PHPExcel_Reader_Exception
*/
public function __construct(Request $request)
{
//Load file from request
$this->excel = PHPExcel_IOFactory::load($request->file('file'));
//Get active sheet
$this->work_sheet = $this->excel->getActiveSheet();
}
/**
* @return array
*/
public function import()
{
//Iterate through drawing collection
foreach ($this->work_sheet->getDrawingCollection() as $drawing) {
//check if it is instance of drawing
if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
//creating image name with extension
$file_name = str_replace(' ', '_', $drawing->getName()).'.'.$drawing->getExtension();
//Get image contents from path and store them in Laravel storage
Storage::put('public/'.$file_name, file_get_contents($drawing->getPath()));
//create images array initially
$this->excel_data[] = [
'image' => $file_name
];
}
}
//Map other data present in work sheet
return $this->rowData();
}
/**
* @return array
*/
private function rowData()
{
$i = 0;
//Iterate through row by row
foreach ($this->work_sheet->getRowIterator(2) as $row) {
//iterate through cell by cell of row
foreach ($row->getCellIterator() as $cell) {
//In case of image data that would be null continue
//We have already populated them in array
if(is_null($cell->getValue())){continue;}
//Map other excel data into the array
$this->excel_data[$i]['name'] = $cell->getValue();
}
$i++;
}
//Return final data array
return $this->excel_data;
}
}
source (my blog): https://meramustaqbil.com/2019/02/23/how-to-extract-images-from-excel-file-using-maat-websites-laravel-excel/
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_DrawingorPHPExcel_Worksheet_MemoryDrawingobjects: 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 (forPHPExcel_Worksheet_Drawingobjects) or directly from thePHPExcel_Worksheet_MemoryDrawingobject 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_HeaderFooterDrawingobjects. All thePHPExcel_Worksheet_Drawingmethods 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