Extract Image from Laravel Excel

How to extract images from excel file using Maat Website’s Laravel Excel

Spread the love

Maat Website’s Laravel Excel is a Laravel package build as a wrapper on PHPExcel in previous version of this package and in its latest version it uses PHPSpreadsheet. In this tutorial I will guide you on how to extract images from excel files using Maat Website’s Laravel Excel.

Note:  As for this tutorial I am using version 2.1 of this package which at the core uses PHPExcel, but the code will be same as PHPExcel and PHPSpreadsheet are identical in most of its code.


Getting Started

First of all, I have created a demo excel file which contains two columns: image, and name. I will be using this demo file to illustrate the complete process.

I have also created a page from which we will upload excel file and then post it to the server.

To get started we will need Maat Website’s Laravel Excel package version 2.1 which can be downloaded from this link.

Step 1: I have created a method called Index in my HomeController of type post.

public function index(Request $request)
    {
        $excel_import = with(new ExcelImport($request))->import();
        dd($excel_import);
    }

Step 2: Register a route in web.php that will point to the method we have created in HomeController.

Route::post('/excel', 'HomeController@index');

Step 3: For this tutorial I have created a class called ExcelImport which will deal with importing excel file’s data including image.  This class accepts one parameter which is Request object containing excel file. Create object of that class and call its import method which will return an array containing excel data.

Below is the complete excel class with every line of code commented.

<?php

namespace App\Excel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use PHPExcel_IOFactory;
use PHPExcel_Worksheet_Drawing;
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;
    }
}

2 thoughts to “How to extract images from excel file using Maat Website’s Laravel Excel”

Comments are closed.