PhpImagick: how to use Imagick to crop images

Source: Internet
Author: User
PhpImagick for help. how to crop images using Imagick? I need to crop a part of a page in a PDF file & nbsp; to generate a new document in png format. gd does not support PDF format. & nbsp; Imagick & nbsp; supports opening pdf, & nbsp; and turning one of the pages to php Imagick for help. how to crop images using Imagick
I need to crop a part of a page in a PDF document to generate a new document in png format.
Gd does not support PDF format. Imagick supports opening pdf files and converting one of the pages into PNG files. but how can we crop a part of this page?

Reference code from network
The code is as follows:

Function compute 2png ($ 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 (); // Obtain the height of the source image.
$ Width = $ Var-> getImagewidth (); // Obtain the width of the source image.
// Echo "H". $ height. "w:". $ width;
$ Im_cut = new Imagick (); // Set the instance of the new graph here.
$ Im_cut-> setResolution (600,600); // resolution
$ Im_cut-> setCompressionQuality (100); // compression rate
// $ Im_cut-> setImageFormat ('PNG '); // An error occurred while setting the format of the new image.
// $ Im_cut-> setImageType (0 );
// $ Im_cut-> I don't know how to do it next
// How to set the height length of the new image
// How to copy a part of $ var to the new graph,
// How to save the new graph


$ Var-> setImageFormat ('PNG ');
$ Var-> setImageType (0 );
$ Filename = $ path. "/". md5(1_key.time(1_0000.'.png ';
If ($ Var-> writeImage ($ filename) = true)
{
$ Return [] = $ filename;
}
}
Return $ Return;
}
Share: More

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.