Converting to PDF; Accurately Overlaying PDFs

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
darkwingduke
Posts: 8
Joined: 2017-01-11T14:43:03-07:00
Authentication code: 1151

Converting to PDF; Accurately Overlaying PDFs

Post by darkwingduke » 2017-04-04T11:53:27-07:00

Here are my specs:
Ubuntu 14.04
ImageMagick 6.7.7
Ghostscript 9.10
Imagick 3.4.0
PHP 5.6.18

Here's my situation:
I'm sending a canvas element into a PHP file via AJAX. I'm utilizing the Fabric.js library, so that I'm able to add and manipulate text on a canvas that is 1050px wide and 675px high. When I use Imagick, make the conversion to PDF, and send that file into a local folder, the end result looks great. The resolution, placement, and size of the text seems to be accurate.
In addition to converting that canvas element to a PDF, I'm also converting a local EPS file to a PDF. (I'm needing this image to serve as my background when I overlay the first image on top of it.) When I go through a similar conversion process with only this separate image, I get another great looking PDF as an end result.
However, when I use compositeImage to overlay the text PDF on top of the background PDF, the placement and size of the text is incorrect. (The resolution still looks good, though.) I'm using setSize to make the columns and rows of both PDFs identical (the same as the canvas element: 1050 x 675), so I'm not sure why this is happening. (I noticed when I look at the two separate image in Preview, that the Page sizes are different.) What am I missing?

Here's my PHP code:

Code: Select all

//First Image: Background Image
$background = ***local EPS file***
$background_image = new Imagick();
$background_image->setResolution(450,450);
$background_image->setSize(1050,675);
$background_image->readImage($background);
$background_image->setImageFormat("pdf");

//Second Image: Text Image
$text = base64_decode($text_data);
$text_image = new Imagick();
$text_image->setResolution(450,450);
$text_image->setSize(1050,675);
$text_image->readImageBlob($text);	
$text_image->setImageFormat("pdf");

//Combining Images
$background_image->compositeImage($text_image, imagick::COMPOSITE_ATOP, 0, 0);

snibgo
Posts: 9403
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting to PDF; Accurately Overlaying PDFs

Post by snibgo » 2017-04-04T15:49:23-07:00

See the documentation http://php.net/manual/en/imagick.setsize.php . I suspect setSize doesn't do what you think it does.
snibgo's IM pages: im.snibgo.com

darkwingduke
Posts: 8
Joined: 2017-01-11T14:43:03-07:00
Authentication code: 1151

Re: Converting to PDF; Accurately Overlaying PDFs

Post by darkwingduke » 2017-04-07T16:18:41-07:00

You're right. I don't think setSize was doing anything.

I went back and produced a new source image for my background and set my canvas to match those dimensions and it ended up working. I was being way too reliant on setSize.

Thanks!

Post Reply