1

Craft v2.6.x

I want to write a plugin to grab thumbnails of PDFs.

I have my stand alone php code with imagemagick on server working like this:

$im = new Imagick();
$pdf = 'assets/documents/resources/after-release-impact-seafarers.pdf';

$im->setResolution(300,300);
//$im->setCompressionQuality(95);
//$im->scaleImage(200,0);
$im->readimage($pdf . '[0]');
$im->setImageFormat('jpeg');
//$im->resizeImage( 640, 905, imagick::FILTER_POINT, 1 );
//$im->resizeImage( 400, 565, imagick::FILTER_POINT, 1 );
$im->resizeImage( 180, 232, imagick::FILTER_POINT, 1 );

$im->writeImage('1a2w12thumb.jpg');
$im->clear();
$im->destroy();

However, how can I load imagemagick - which works on the server in my PDF thumbnail plugin class function? I have tried:

    $im = new \Imagick();

    $im->setResolution(300,300);
    $im->readimage($pdfAsset . '[0]');
    $im->setImageFormat('jpeg');
    $im->resizeImage( 640, 905, imagick::FILTER_POINT, 1 );
    $im->writeImage($pdfAsset->writepath.$pdfAsset->filename . '.jpg');
    $im->clear();
    $im->destroy();

But none of the functions for imagemagick work: Eg. error i get is Class 'Craft\imagick' not found

Is this a namespace issue with the imagine library? (I sound like I know what I am talking about but I am not sure I do!)

How to include/ use native imagemagick on server in Craft?

0

If you want to work with PECL imagick, which is the PHP wrapper for the ImageMagick library, then this should work:

$im = new \Imagick();

It's not namespaced, so it exists in the global namespace, which is what the \ does for you.

| improve this answer | |
  • Thx Brad.I load the library but the functions don't work. I get a craft namespace error : Class 'Craft\Imagick' not found on $im->resizeImage( 640, 905, Imagick::FILTER_LANCZOS, 1 ); [ or 'imagick::FILTER' etc] – joomkit Jan 4 '18 at 22:53
  • OK i needed the back slash on the filter too! Eg $im->resizeImage( 640, 905, \Imagick::FILTER_LANCZOS, 1 ) – joomkit Jan 4 '18 at 23:14
  • Ahh, correct. Probably should have mentioned that. :) – Brad Bell Jan 4 '18 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.