Insert image with Laravel-Excel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Inserting Images into Excel with Laravel-Excel: A Deep Dive As a senior developer working with data export in Laravel, we often rely on powerful packages like `Maatwebsite/Laravel-Excel` to handle complex spreadsheet generation. This package is incredibly efficient for exporting Eloquent collections into `.xlsx` files. However, as you correctly pointed out, the documentation often focuses on cell data and formulas, leaving a gap regarding more complex features like embedding image files directly into the workbook. This post will dive into how you can successfully insert images from your local files into an Excel document using Laravel-Excel, addressing the common confusion developers face about extending package capabilities. ## The Challenge: Images Beyond Cell Data The `Laravel-Excel` package acts as a beautiful wrapper around the powerful underlying library, PhpSpreadsheet. While PhpSpreadsheet supports advanced features, inserting binary objects like images requires interacting directly with the file stream and the spreadsheet object's drawing methods, which aren't always exposed through simple Eloquent calls. This is why the documentation might not feature a direct, one-line method for image insertion—it often requires understanding the underlying library structure. The good news is that this functionality *is* achievable by leveraging the raw capabilities provided by PhpSpreadsheet, which Laravel-Excel exposes. ## The Solution: Leveraging PhpSpreadsheet Internals To insert an image, you need to read the external file (e.g., a JPG or PNG) and use the appropriate methods within the worksheet object to embed that image into the spreadsheet grid. This process involves reading the image data into a stream and then applying it as a drawing object on the target sheet. Here is a practical example demonstrating how to achieve this. We will assume you have an image file ready in your project directory. ### Step-by-Step Implementation First, ensure you have the necessary dependencies installed. For this demonstration, we'll focus on reading a local file and inserting it onto the sheet generated by Laravel-Excel. ```php use Maatwebsite\Excel\Facades\Excel; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; class ImageExporter { public function exportWithImage(array $data, string $imagePath) { // 1. Prepare the data for export $filename = 'report.xlsx'; // Use the standard Excel export method Excel::spreadsheet($data, $filename); // --- Manual Image Insertion using PhpSpreadsheet methods --- // Load the spreadsheet generated by Laravel-Excel (we need to access it) $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filename); $sheet = $spreadsheet->getActiveSheet(); // 2. Define image location and size (Example dimensions) $imagePath = $imagePath; // Path to your local image file $drawing = new Drawing(); $drawing->setName('Product Image'); $drawing->setDescription('Image of the Product'); $drawing->setPath($imagePath); $drawing->setCoordinates('A1'); // Insert image starting at cell A1 $drawing->setOffsetX(0); $drawing->setOffsetY(15); // Offset 15 rows down // 3. Add the drawing to the worksheet $sheet->addObject($drawing); // Clean up (optional: delete the temporary file if needed) unlink($filename); return true; } } // Example Usage within a Controller context: /* $data = [ /* Your data rows */ ]; $exporter = new ImageExporter(); $exporter->exportWithImage($data, '/path/to/your/image.png'); */ ``` ### Best Practices for File Handling When dealing with file insertion in Excel generation, performance and file management are crucial. Always handle file paths securely and ensure that the image files you intend to embed are accessible by the PHP process. Furthermore, if you are generating hundreds of files, consider whether embedding images is truly necessary. For extremely large datasets or many images, storing the images externally (e.g., in cloud storage) and linking to them via a hyperlink within the Excel sheet is often a more robust and scalable approach than embedding binary data directly into the XLSX file. ## Conclusion While `Laravel-Excel` provides an excellent high-level interface for moving Eloquent data, mastering advanced features like image insertion requires understanding how it interfaces with PhpSpreadsheet. By stepping down to the underlying library and utilizing its specific drawing methods, you gain full control over the output format. This approach ensures that you can build highly customized reports without being limited by the package's initial documentation, giving you complete power over your data presentation within the Laravel ecosystem. If you are building complex reporting tools on **Laravel**, understanding these underlying mechanics is key to unlocking true development potential.