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