PHP Imagick help, how to cut pictures with Imagick

Source: Internet
Author: User
I need to view one of the pages in a PDF document to crop a portion of it and generate a new PNG-formatted document.
GD does not support PDF format, Imagick supports opening PDFs, and turns one page into PNG, but how do you crop up part of this page?

Reference code from the network
The code is as follows
 function Pdf2png ($pdf, $path, $page =-1) {if (!extension_loaded (' Imagick ')) {return false;     } if (!file_exists ($pdf)) {return false;     } $im = new Imagick ();     $im->setresolution (600,600);   $im->setcompressionquality (100);   if ($page ==-1) $im->readimage ($pdf); else $im->readimage ($pdf. "   [". $page."] "); foreach ($im as $Key + $Var) {$height = $Var->getimageheight ();//Get the height of the original image $width = $Var->getimagewid th ();//Get the width of the original//echo "H". $height. " W: ".       $width;  $im _cut = new Imagick (); This sets up an instance of the new diagram $im _cut->setresolution (600,600); Resolution $im _cut->setcompressionquality (100);//Compression ratio//$im _cut->setimageformat (' png '); Format the new diagram here is an error//$im _cut->setimagetype (0);//$im _cut-> Next do not know how to do//how to set the height length of the new diagram//How to copy part of the $var to         New figure,//How to save the new diagram $Var->setimageformat (' png ');       $Var->setimagetype (0); $filename = $path. " /". MD5 ($Key. Time ()). PNG ';       if ($Var->writeimage ($filename) = = True) {$Return [] = $filename;  }} return $Return;   }


Reply to discussion (solution)

$myurl = '/pdf/mypdf.pdf '; $image = new Imagick (Realpath ($myurl). ' [0] '); [0] Indicate the number of the wanted page$image->setresolution, $image->setimageformat ("png"); $image ->writeimage (Realpath ('./mypdf.png '));

It's been settled or thank you.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.