Question

How can I pass an image processed with ImageMagick (not saved) to Zend_Pdf_Image? I want to transfer $image = new Imagick($imageFile); directly to $pdfImage = Zend_Pdf_Image:: ..., without saving. Is that possible?

Thanks for any advice, Juergen

Was it helpful?

Solution

I was about to simply say "no", but then I thought I would double-check the code and consider if there were any other possibilities.

The imageWithPath() method utilizes some helper methods in other classes to decode the image that is referenced by the $filePath parameter. The very first thing each of these helper methods does is open the $filePath using fopen().

So what that means is that you can pass in any kind of resource to the imageWithPath() method that is supported by your PHP installation.

Generally speaking this means files and URLs, but since PHP 5.2 it also includes the data:// wrapper. I haven't used this myself (I've been writing out temp files then passing them to imageWithPath() for years), but according to the man pages it might just enable you to pass in the modified data without having to save it to a file first.

More info: http://php.net/manual/en/wrappers.data.php

Please let us all know if you get something working using this method. :-)

OTHER TIPS

I will just add my 3 cents to James answer above. He is perfectly right - it is allowed to use data: wrapper.

Example method showing how to convert svg to Zend_Pdf_Image using ImageMagick:

class My_Pdf extens Zend_Pdf {

    function imageFromSvg($path, $w, $h) {
        if (!class_exists('Imagick') || !file_exists($path)) return false;
        $svg = file_get_contents($path);
        $im = new Imagick();
        $im->readImageBlob($svg);
        $im->setImageFormat('png8');
        $im->adaptiveResizeImage($w,$h);
        return new Zend_Pdf_Resource_Image_Png('data:image/png;base64,'.base64_encode   ($im));
    }
}

Note that Zend_Pdf_Resource_Image_Png doesn't support pngs with bit depth more then 8. Returned object can be easily embeded in Zend_Pdf using its drawImage method. Example:

$pdf = new My_Pdf();
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$page->drawImage($pdf->imageFromSvg($svg,400,400),400,100,100,100);

I used svg rescaled to 400x400, because I get better quality print then. You can experiment with this according to your needs. Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top