Importing images along with other fields username subjectname from excel in laravel

0 votes

I am using phpspreadsheet. I want to import an excel sheet that have images too, it looks something like this, Picture of excel sheet

I am able to retrieve fields separately and images separately, I want to get them together. Problem I am facing is that Images are being accessed with

$spreadsheet->getActiveSheet()->getDrawingCollection() 

and for others field i have to access them like this

$spreadsheet->getRowIterator() 

as both of them requires separate loops, should i be merging them into one or what is the right way so that i am able to retrieve both(images and fields) together.

Images retrieve code:

$spreadsheet = IOFactory::load($request->import_file);
$i = 0;

foreach ($spreadsheet->getActiveSheet()->getDrawingCollection() as $key => $drawing) {


    if ($drawing instanceof MemoryDrawing) {
        ob_start();
        call_user_func(
            $drawing->getRenderingFunction(),
            $drawing->getImageResource()
        );
        $imageContents = ob_get_contents();
        ob_end_clean();
        switch ($drawing->getMimeType()) {
            case MemoryDrawing::MIMETYPE_PNG :
                $extension = 'png';
                break;
            case MemoryDrawing::MIMETYPE_GIF:
                $extension = 'gif';
                break;
            case 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();
    }

    $myFileName = time() .++$i. '.' . $extension;

$imagesCollection['answerImages_'.$key] =$myFileName;

    file_put_contents('images/products/' . $myFileName, $imageContents);

$a = Answers::create([
'answerImages'=>$myFileName,
'questionId'=>($key <=4)?1:2,
]);
}

I want to store them into my table in database such that in questionImage column of database it has image name like this

enter image description here

and it is storing it currently but as I mentioned earlier i have to store them separtely

This is how i am storing other fields

  $spreadsheet = IOFactory::load($the_file->getRealPath());
           $sheet        = $spreadsheet->getActiveSheet();
           $row_limit    = $sheet->getHighestDataRow();
           $column_limit = $sheet->getHighestDataColumn();
           $row_range    = range( 1, $row_limit );
           $column_range = range( 'F', $column_limit );
           $startcount = 2;
           $data = array();
           foreach ( $row_range as $row ) {
               $data[] = [
                   'courseName' =>$sheet->getCell( 'A' . $row )->getValue(),
                   'subjectName' => $sheet->getCell( 'B' . $row )->getValue(),
                   'question' => $sheet->getCell( 'C' . $row )->getValue(),
                   'questionImage' => $sheet->getCell( 'D' . $row )->getValue(),
                
               ];
               $startcount++;
           }
           DB::table('questions')->insert($data);

How to get them together so that i can store them in one table

Mar 30, 2022 in Database by Edureka
• 13,670 points
2,459 views

1 answer to this question.

0 votes
$objphpexcel = PHPExcel_IOFactory::load("MyExcelFile.xls");

foreach ($objphpexcel ->getSheetByName("My Sheet1")->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();
        $cellID = $drawing->getCoordinates();
        //  .... do your save here
    }
}

FOLLOW THIS CODE 
hope this will help you

answered Apr 1, 2022 by gaurav
• 23,260 points

Related Questions In Database

0 votes
1 answer

How to sort dates from Oldest to Newest in Excel?

Drag down the column to select the ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
4,624 views
0 votes
1 answer

how do I calculate discount with if function in excel?

Suppose a customer gets a 10 percent ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
9,688 views
0 votes
1 answer

Remove special characters from the specified string in excel

To erase a specific character from a ...READ MORE

answered Mar 15, 2022 in Database by gaurav
• 23,260 points
1,247 views
0 votes
1 answer

Prevent cell numbers from incrementing in a formula in Excel

In Excel, you can use a feature ...READ MORE

answered Mar 15, 2022 in Database by gaurav
• 23,260 points
7,744 views
0 votes
1 answer

how do I calculate discount with if function in excel

Notes about the release; Frameworks to aim ...READ MORE

answered Mar 25, 2022 in Database by gaurav
• 23,260 points
534 views
0 votes
1 answer

How can I calculate deciles with a range of 12,000 cells in excel?

1. Enter the following formula in cell ...READ MORE

answered Mar 25, 2022 in Database by gaurav
• 23,260 points
7,889 views
0 votes
1 answer

Remove time from date field in Excel formula

Use the Find And Replace function to ...READ MORE

answered Mar 30, 2022 in Database by gaurav
• 23,260 points
692 views
0 votes
0 answers

Laravel - Datatables export excel from filtered data

Hello i would like to export data ...READ MORE

Mar 28, 2022 in Database by Edureka
• 13,670 points
4,227 views
0 votes
1 answer

Generating username from first name and last name in excel

How to automatically merge first and last ...READ MORE

answered Mar 30, 2022 in Database by gaurav
• 23,260 points
4,784 views
0 votes
1 answer

Laravel Excel import using Maatwebsite Excel package with additional columns from View

How to fix: I know you're fed ...READ MORE

answered Apr 1, 2022 in Database by gaurav
• 23,260 points
11,650 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP