Please share the code you tried and the error
can't import images using laravel excel
I'm using maatwebsite/excel package to import from excel file. I can't import images using that package. Any solutions.
on my controller
Excel::import(new ProductImport(), request()->file('import'));
On my product import class
public function model(array $row)
{
$product = new Product([
'category_id' => $row['category'],
'storename' => storename(),
'code' => $row['code'],
'name' => $row['name'],
'slug' => Str::slug($row['name']),
'description' => $row['description'],
'retail_price' => $row['retail_price'],
'wholesale_price' => $row['wholesale_price'],
'created_by' => auth()->id(),
]);
return $product;
}
I tried dd($row) and I'm getting null value on image field.
This is the excel file I'm trying to import https://pasteboard.co/Jl6YdDa.png
Edited:
@spjoshis on my controller
Excel::import(new ProductImport(), request()->file('import'));
On my product import class
public function model(array $row)
{
$product = new Product([
'category_id' => $row['category'],
'storename' => storename(),
'code' => $row['code'],
'name' => $row['name'],
'slug' => Str::slug($row['name']),
'description' => $row['description'],
'retail_price' => $row['retail_price'],
'wholesale_price' => $row['wholesale_price'],
'created_by' => auth()->id(),
]);
return $product;
}
I tried dd($row) and I'm getting null value on image field.
@spjoshis this is the excel file I'm trying to import. https://pasteboard.co/Jl6YdDa.png
Did you try reading the docs for the underlying system?
https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#reading-images-from-a-worksheet
@sinnbeck I read that one. but I don't know where to implement it. Is it in my Import class? or in controller?
@sinnbeck thank you. I implemented it inside my ProductImport class. its works fine.
@ajithlal can you provide the exact steps?
@ajithlal Can you please show us the exact steps? I have the exact same problem and don't know how to implement PHPSpreadSheets with LaravelExcel. Do I just rewrite the whole ProductImport File or just the image part?
@ajithlal Any small fiddle or small snippet would be very big help from your side. I am also in the middle of similar scenario and would like to import images from excel.
@ali88 @aungchanoo @mostafahamed Below is the code block I used for importing images.
php
$spreadsheet = IOFactory::load(request()->file('import'));
$i = 0;
foreach ($spreadsheet->getActiveSheet()->getDrawingCollection() as $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;
file_put_contents('images/products/' . $myFileName, $imageContents);
ProductImage::create([
'product_id' => $product->id,
'image' => $myFileName,
'created_by' => $product->created_by,
]);
}
Is this with package maatwebsite/excel please tell bro or share full code of controller.
@ajithlal Bro can please share the file because were we have use in the import file whether any thing need to be installed via composer apart from package. Please reply bro
It is in the import file
using the same excel package you mentioned. Below is the full code of import class. The code will not be pretty good but it works.
<?php
namespace App\Imports;
use App\Models\Product;
use App\Models\ProductImage;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
class ProductImport implements ToModel, WithHeadingRow
{
/**
* @param array $row
*
* @return Product
*/
public function model(array $row)
{
$product = Product::create([
'category_id' => $row['category'],
'storename' => storename(),
'code' => $row['code'],
'name' => $row['name'],
'slug' => Str::slug($row['name']),
'description' => $row['description'],
'retail_price' => $row['retail_price'],
'wholesale_price' => $row['wholesale_price'],
'created_by' => auth()->id(),
]);
$spreadsheet = IOFactory::load(request()->file('import'));
$i = 0;
foreach ($spreadsheet->getActiveSheet()->getDrawingCollection() as $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;
file_put_contents('images/products/' . $myFileName, $imageContents);
ProductImage::create([
'product_id' => $product->id,
'image' => $myFileName,
'created_by' => $product->created_by,
]);
}
return $product;
}
}
There is nothing much to do in controller. Just call the import function as specified in the laravel/excel doc.
bro here I want to take image from the row and insert. Is there any option. Please let me know.
<?php
namespace App\Imports;
use App\Stock;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\WithStartRow;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
class StockImport implements OnEachRow, WithStartRow
{
private $lab_id = null;
public function __construct($lab_id)
{
$this->lab_id = $lab_id;
}
public function onRow(Row $row)
{
$rowIndex = $row->getIndex();
$row = $row->toArray();
$spreadsheet = IOFactory::load(request()->file('stockfile'));
foreach ($spreadsheet->getActiveSheet()->getDrawingCollection() as $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 = Str::uuid()->toString() . '.' . $extension;
file_put_contents('images/stocks/' . $myFileName, $imageContents);
Stock::create(
[
'lab_id' => $this->lab_id,
'pid' => $row[0],
'name' => $row[1],
'specification' => $row[2],
'location' => $row[3],
'image' => $myFileName,
'current_stock' => $row[5],
'remarks' => $row[6],
'consumable_type' => ($row[7] == 'C') ? '0' : '1',
]
);
}
public function startRow(): int
{
return 2;
}
}
It is better to post your question as a new thread for better visibility. Since this is already answered your question wont get much attention.
I write this code in my product import and it's upload all image in the specified directory but give last uploaded image name to all products keep in mind i save image name in image column in products table
@ajithlal Need Help to resolve uploading excel with images, willing to make jobs payment ... would you be interested ?
@Elkomy I know its an old post , but i have this problem too. Did you find any solution about this ?
@M4verick I have already posted the code in the same thread. I have found one more work arround for the same. Instead of attaching the image on the excel file, create a zip folder with all the images and name of the images should match the row. For example you are uploading an excel file having the name of fruits.
- Apple
- Mango
On your upload script there should be an option to upload the excel and zip file. And the zip file should contain images with same names as Apple.png and Mango.png
On the the controller while importing the file also extract the zip and upload the file and store the path on the DB
@ajithlal i am using this same code but it's not working. what can i do ?
@ajithlal Thank you do much for your help, you saved me a lot of time it did worked like a charm. I highly appreciate that.
@sinnbeck Thank you so much for pointing everyone towards the solution. :)
Please or to participate in this conversation.