ImageMagick is giving me some weird “corrupt image” error message when try to add text to a PNG image.
I’ve created a function implemented in WordPress that takes form input data, and adds the input as text to a PNG image, which is then later merged with a couple of other PNGs and outputted as multi paged PDF.
When running the function, ImageMagick tells me Fatal error: Uncaught exception "ImagickException" with message "corrupt image "/home/1/u/utoya/www//wp-content/uploads/pdf/innside.png" ..
(link to full error message at the bottom).
However, the same code works perfectly on my local webserver, but not in the production environment. I’m using exactly the same files, at the same locations and with the same chmod settings.
Is the PNGs not readable because of the huge filesize, is there a memory limit somewhere, or how should I handle this error message?
My code looks like this:
function generate_pdf( $purchaseData )
{
$toName = $purchaseData['toName'];
$fromName = $purchaseData['fromName'];
$email = $purchaseData['recipientEmail'];
$filename = date('dmY') . '-' . md5(date('dmY')) . '.pdf';
// die( '../..' . '/uploads/pdf/innside.png' );
// Create new Imagick PDF container
$combined = new Imagick();
// Create new Imagick object of inside page
$image = new Imagick( ABSPATH . '/wp-content/uploads/pdf/innside.png' );
// Create drawing capabilities
$draw = new ImagickDraw();
// Set text color
$draw->setFillColor('black');
// Add 'To' text
$draw->setFont( ABSPATH . 'fonts/Georgia-Bold-Italic.ttf' );
$draw->setFontSize(44);
$image->annotateImage($draw, 2020, 268, 0, $toName);
// Add 'From' text
$draw->setFont(ABSPATH . 'fonts/Georgia-Italic.ttf');
$image->annotateImage($draw, 2020, 987, 0, $fromName);
// Add all pages to PDF
$combined->addImage( new Imagick( ABSPATH . '/uploads/pdf/forside.png' ) ); // Front page
$combined->addImage( $image ); // Inside page
$combined->addImage( new Imagick( ABSPATH . '/uploads/pdf/bakside.png' ) ); // Back page
// Set output format
$combined->setFormat('pdf');
// Save pages to file
$pdf = $combined->writeImages( ABSPATH . '/generated/' . $filename, TRUE );
$attachments = array( ABSPATH . '/generated/' . $filename );
wp_mail( $email, 'Innledergavekort til ' . $toName, 'Hei, her kommer ditt gavekort', NULL, $attachments );
}
The (very long) error can be found here: http://pastebin.com/db216Qvz
I worked it out. Seems like PNG was the issue. Switched to using JPEGs (with a bit of compression for file size) and it now works.
No idea why it works, but it does.