Dalma's avatar
Level 6

Spatie Media Library Some PDF thumbnails render with black background

Hi everyone, I started with work with the Spatie Media Library late last week and I love the functionality. I'm providing the ability to upload multiple file types and the image files work great and I have started to use the conversion process to have PDF's generate a thumbnail and this is working now. I'm finding that some of the generated thumbnails are all/mostly black whereas the original document had a white background on the first page.

To get this working I had to install Ghostscript and Imagick and I used versions currently available for Ubuntu 18.04.

Not all of my PDF's generate with a black background but enough do that I need to find a solution.

Any ideas?

0 likes
4 replies
Dalma's avatar
Level 6

I have tried the following:

changing my addMediaConversion to change the output from jpg to png with no changes

setting the background color to white with no change.

setting the background color to black with no change (in case of a logic !logic issue)

This seems to apply only to multi-page pdf's but I have reviewed the documentation and by default it only renders the first page and looking into the conversion class I see that it defaults to page 1. Further the thumbnail is of the first page of my document.

Dalma's avatar
Level 6

Well I found how to correct this issue. This package has a dependency on spatie pdf-to-image and in the src\Pdf.php class, getImageData Method which reads as:

  public function getImageData($pathToImage)
    {
        $imagick = new \Imagick();

        $imagick->setResolution($this->resolution, $this->resolution);

        $imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $this->page - 1));

        $imagick->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);

        $imagick->setFormat($this->determineOutputFormat($pathToImage));

        return $imagick;
    }

Should be:

  public function getImageData($pathToImage)
    {
        $imagick = new \Imagick();

        $imagick->setResolution($this->resolution, $this->resolution);

        $imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $this->page - 1));

        $imagick = $imagick->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);

        $imagick->setFormat($this->determineOutputFormat($pathToImage));

        return $imagick;
    }

I'm not sure how to correctly implement this change without modifying the vendor contribution.

2 likes
albertesp's avatar

Hi Dalma! Same here, your solution works! The way to go will be changing the vendor file and reporting the fix to Spatie so they can implement it on next releases.

Thanks!

Please or to participate in this conversation.