Last active
April 22, 2020 14:35
-
-
Save adibenc/70db24faf74d746e53fcdacdb2f565c7 to your computer and use it in GitHub Desktop.
extract_image.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
base : https://chandranrahul.wordpress.com/2017/12/04/extract-images-from-spreadsheet-using-phpexcel/ | |
modified : Rab Apr 22 21:32:00 WIB 2020 | |
- require | |
composer require phpoffice/phpspreadsheet | |
- imports | |
use PhpOffice\PhpSpreadsheet\IOFactory; | |
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; | |
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; | |
- test file : test.xlsx | |
https://drive.google.com/file/d/14h-2qs2WV39aJ4YAvWOmAw5khSSONiFb/view?usp=sharing | |
*/ | |
public function submit(){ | |
$file = './test.xlsx'; | |
// Replace questions variable with your variable name | |
// $objPHPExcel = PHPExcel_IOFactory::load($this->input->post('file')); | |
// $objPHPExcel = PHPExcel_IOFactory::load($file); | |
$objPHPExcel = IOFactory::load($file); | |
$objWorksheet = $objPHPExcel->getActiveSheet(); | |
preout($objWorksheet->getDrawingCollection()); | |
$i = 0; | |
$image_fields = []; | |
foreach ($objWorksheet->getDrawingCollection() as $drawing) { | |
// if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) { | |
if ($drawing instanceof MemoryDrawing) { | |
ob_start(); | |
call_user_func( | |
$drawing->getRenderingFunction(), | |
$drawing->getImageResource() | |
); | |
$imageContents = ob_get_contents(); | |
ob_end_clean(); | |
$cellID = $drawing->getCoordinates(); | |
// $myFileName = 'questions/questions_' . ++$i.time() . '.' . $extension; | |
// $path = Storage::put($myFileName, $imageContents); | |
$myFileName = 'file' . ++$i.time() . '.' . $extension; | |
$f=fopen($myFileName,'wb'); | |
fwrite($f,$imageContents); | |
fclose($f); | |
$image_fields[$drawing->getCoordinates()] = '<img src="'.$myFileName.'">'; | |
} | |
// if ($drawing instanceof PHPExcel_Worksheet_Drawing) { | |
if ($drawing instanceof Drawing) { | |
$zipReader = fopen($drawing->getPath(), 'r'); | |
$imageContents = ''; | |
while (! feof($zipReader)) { | |
$imageContents .= fread($zipReader, 1024); | |
} | |
fclose($zipReader); | |
$extension = $drawing->getExtension(); | |
// $myFileName = 'questions/questions_' . ++$i.time() . '.' . $extension; | |
// $path = Storage::put($myFileName, $imageContents); | |
$myFileName = 'file' . ++$i.time() . '.' . $extension; | |
$f=fopen($myFileName,'wb'); | |
fwrite($f,$imageContents); | |
fclose($f); | |
$image_fields[$drawing->getCoordinates()] = '<img src="'.$myFileName.'">'; | |
} | |
} | |
preout($image_fields); | |
echo "success"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment