Creating a thumbnail from uploaded pdf file - in the backend

Hi - I am trying to create a thumbnail from a pdf which is uploaded via the file uploaded - all on the backend. I was able to create the thumbnail using Imagick, but only if the pdf has already been uploaded. I did this using the beforeSave function:

if (!empty($this->invoice->disk_name))
            $pdfDiskName = $this->invoice->disk_name;
            $pdfImage = substr_replace($pdfDiskName, 'jpg', strrpos($pdfDiskName, ".") +1); 
            $pdfPathFile = $this->invoice->getPath();
            $image = new Imagick();
            $this->invoice_image = $pdfImage;       

Without the condition, you get an error because the $this->invoice_disk_name is null until the record is saved. Any suggestions as how to accomplish this when you are creating the record or when updating a record without a loaded pdf file are appreciated. I looked into deferred bindings but I was unable to get it to work.


Hi @jumpshotink

Try something like this

if (!empty(
    $invoice = $this->invoice()->withDeferred($this->sessionKey)->first()
)) { /* ... */ } 

This will include records that exist and are currently deferred.

Hi daftspunky,

Thanks for the help - I got it to work.

Happy holidays.

1 Like