Search code examples
phppdfimagick

How to keep color quality with imagick


I am toying with Imagick in PHP8 and facing some issues. My first goal was to center a picture inside a SVG. In the end, I put the picture inside an image tag of the svg, then converting the whole SVG into a PNG and put that PNG inside a PDF file.

When applying the picture inside a PDF, the colors are a bit off in some PDF readers (like Adobe's), like "washed out". In some readers, like PSPDFKit's js viewer, it is normal.

This is how my image is supposed to look (orange bar on the left being the SVG) :

original

When passing it through imagick, the picture looks "washed out" :

washed out image

My code, very basic :

    $imagick->readImageBlob($svg);
    $imagick->setImageFormat('png');
    $png = $imagick->getImageBlob();

I tried mostly to see if opacity/transparency was responsible, also tried other image formats (other types of PNG seen here)

EDIT : It seems the issue comes from the picture generation, as just putting the picture inside the PDF (without converting it in a first place) doesn't affect its quality. The PDF is generated by another program I don't have the hand over.


Solution

  • So I solved the issue by switching my picture format to jpg instead of png. Considering my usecase, I won't be affected by the loss of transparency nor the lossy compression.

    So basically :

    $imagick->readImageBlob($svg);
    $imagick->setImageFormat('jpeg');
    $png = $imagick->getImageBlob();