#How to fetch an image from row cell in Laravel Excel when importing

33 messages · Page 1 of 1 (latest)

edgy ruin
#

Title pretty much says all

Using the MaatWebsite Excel Package

I'm currently using withHeadingRow and ToModel to transfer it to the database easier

#


class newImport  implements ToModel, WithHeadingRow, WithValidation
{
  /**
   * @param Collection $collection
   */

   protected $errors=[];

 
  public function model(array $row){

   //Here, get image in the $row["imagen"] cell
  }
  public function rules(): array
  {
    return [
  
 
    ];
  }
 
}

edgy ruin
#

bump

edgy ruin
#

BUMP

vocal python
#

BUUUUUUUUUUUUUUUUUMP trollface

leaden sluice
#

Instead of uselessly bumping a thread you could provide some more information. You haven't given any information, or anything you've tried. A few searches tells me the package you're using doesn't really support it, you'd need to use the underlying library; https://laracasts.com/discuss/channels/laravel/cant-import-images-using-laravel-excel
This is because a cell does not and cannot contain an image. It'll contain a reference to a drawing.

edgy ruin
#

Are you sure cells can't contain images?

#

What I'm trying to do is assign images to created table rows

leaden sluice
edgy ruin
#

So what is the $spreadsheet variable?

edgy ruin
#

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

Returns this

ArrayObject {#3539 ▼ // app\Imports\inmueblesImport.php:59
  storage: []
  flag::STD_PROP_LIST: false
  flag::ARRAY_AS_PROPS: false
  iteratorClass: "ArrayIterator"
}

on


 foreach ($spreadsheet->getActiveSheet()->getDrawingCollection() as $drawing) {
            if ($drawing instanceof MemoryDrawing) {
          ob_start();
          call_user_func(
              $drawing->getRenderingFunction(),
              $drawing->getImageResource()
          );
          $imageContents = ob_get_contents();
          dd($imageContents);
          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;
      dd($fileName);
      }```
leaden sluice
#

Perhaps they're not located on the active sheet then? 🤷‍♂️ Also, you might want to tone down your voice before sending code snippets

edgy ruin
#

What do you mean?

#

Oh

#

Sorry

#

Not directed to you

edgy ruin
edgy ruin
#

bump

edgy ruin
#

bump

edgy ruin
#

bump

leaden sluice
#

@edgy ruin could you stop bumping unnecessarily? You're not giving particularly helpful information here, you'd have to try some things yourself, you'd have to debug a bit. People can point you in the right direction, you can't just expect them to spoonfeed you the solution (you're the one building this thing after all, not other people)

edgy ruin
#

What more information can I give though?

#

From what I can gather, I can't retrieve images inside cells

#

It has to be outside the cells to retrieve images

leaden sluice
#

Because, like I said many times now; images aren't located in a cell. A cell does not contain an image. It only contains a reference to an image

edgy ruin
#

How can I access that reference

#

when I access the row value it gives me "#VALUE"

#

It'd be very helpful if there was some how some way to access that reference

edgy ruin
leaden sluice
# edgy ruin How much more specific can I be? or how much more helpful can I be here? I've tr...

Well for one, you're not giving a whole lot apart from "it's not working". And just $cell['image'] wouldn't really tell anyone what you're doing. We're not clairvoyant, we can't see what you're doing, what you're seeing etc.
Either way, like I mentioned earlier, the cell doesn't contain the image. I'm not sure how it works exactly, maybe you'd need to get the coordinate from that cell (like https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Cell/Cell.php#L155 on the underlying package) and then use that to figure out which drawing belongs to it.
So start to do some debugging, start do dig through the source code or the docs of the underlying package. My first step would be to get all the drawings out of the excel document, see if you can grab those. If you have those inspect them, look through the source/docs on all the methods it contains. Maybe it contains a method to get the accompanying cell.
Really, go from one step to another, figure stuff out

edgy ruin
#

Here's what I figured, I tried using phpspreadsheet for this

#

    $spreadsheet=  IOFactory::load($this->file);
   foreach($spreadsheet->getAllSheets() as $sheet){
 
    $headers = [];
    $cellIterator = $sheet->getRowIterator(1)->current()->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(true);
    foreach ($cellIterator as $cell) {
      $headers[] = $cell->getValue(); 
    }
    $rowIterator = $sheet->getRowIterator(2);
    foreach ($rowIterator as $row) {
      // Initialize an associative array to hold header-value pairs
      $rowValues = [];
  
      $cellIterator = $row->getCellIterator();
      $cellIterator->setIterateOnlyExistingCells(true);
  
      $columnIndex = 0;
  
      foreach ($cellIterator as $cell) {
          // Match the cell value with the corresponding header
          $header = $headers[$columnIndex] ?? 'Unknown'; // Avoid out-of-bounds errors
          $rowValues[$header] = $cell->getValue(); // Map the header to the cell value
          $columnIndex++;
      }
  
      // Output the header-value pairs for this row
      echo "Row {$row->getRowIndex()} - " . json_encode($rowValues["Codigo"]) . "\n";
  }
   echo "<br><br>Wait<br><br>";
  $drawings =$sheet->getDrawingCollection();
 
    foreach($drawings as $drawing){
      if($drawing instanceof BaseDrawing){
        $imagePath = $drawing->getPath();
        $anchorCell = $drawing->getCoordinates(); 
      echo $anchorCell;
      }
    }
    dd("Stop");
   }

The images inside the cells just aren't recognized by the $sheet->getDrawingCollection(); method