Created
January 21, 2021 04:33
-
-
Save wildanm/f8c9d145a2a4f4fffc82e5f59393f791 to your computer and use it in GitHub Desktop.
phpspreadsheet_read_images
This file contains 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
<?php | |
$file = $request->file('file'); | |
$extension = $file->getClientOriginalExtension(); | |
$file_import = time().'.'.$extension; | |
Storage::disk('my_upload')->put('import_user/'.$file_import, File::get($file)); | |
$path = storage_path('dokumennya/import_user/'.$file_import); | |
$inputFileName = $path; | |
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); | |
$spreadSheet = $reader->load($inputFileName); | |
$workSheet = $spreadSheet->getActiveSheet(); | |
$i = 0; | |
$image_filenames = []; | |
foreach ($workSheet->getDrawingCollection() as $drawing) { | |
if ($drawing instanceof \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing) { | |
ob_start(); | |
call_user_func( | |
$drawing->getRenderingFunction(), | |
$drawing->getImageResource() | |
); | |
$imageContents = ob_get_contents(); | |
ob_end_clean(); | |
switch ($drawing->getMimeType()) { | |
case \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_PNG : | |
$extension = 'png'; | |
break; | |
case \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_GIF: | |
$extension = 'gif'; | |
break; | |
case \PhpOffice\PhpSpreadsheet\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(); | |
} | |
$filename = 'FotoIdentitas_'.time().++$i.'.'.$extension; | |
$destinationPath = storage_path('ktp_picture/thumbnail/'); | |
if(!is_dir($destinationPath)) mkdir($destinationPath, 0755, true); | |
$img = Image::make($imageContents); | |
$img->resize(300, 300, function ($constraint) { | |
$constraint->aspectRatio(); | |
})->save($destinationPath.$filename); | |
$destinationPath = storage_path('ktp_picture/'); | |
Storage::put('public/'.$filename, $imageContents); | |
$image_filenames[$i] = $filename; | |
$i++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment