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 in Database by Edureka
• 9,320 points
101 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 by Edureka
• 8,820 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 in Database by Edureka
• 8,820 points
34 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 in Database by Edureka
• 8,820 points
151 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 in Database by Edureka
• 8,820 points
51 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 in Database by Edureka
• 8,820 points
27 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 in Database by Edureka
• 8,820 points
24 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 in Database by Edureka
• 8,820 points
51 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 in Database by Edureka
• 8,820 points
20 views
0 votes
0 answers

Laravel - Datatables export excel from filtered data

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

Mar 28 in Database by Edureka
• 9,320 points
215 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 in Database by Edureka
• 8,820 points
38 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP