PDF selection for JPG Imagic

Downloading answers on how to do this for the command line

convert /path/to/file/file.pdf[3] output.jpg 

excellent ... but what if I use in memory processing, I create a PDF with PDFlib and then output its buffer to the function that I want to create to view the jpg of the selected page. How? My code is:

  [...] $buf = $pdf->get_buffer(); //$buff is just a PDF stored in a string now. $im = new Imagick(); $im->readimageblob($buf); $im->setImageFormat("jpg"); $im->setimagecompressionquality(60); $len = strlen($im); header("Content-type: image/jpeg"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=test.jpg"); echo $im; 

This creates jpeg, but always returns the last page of the PDF. I want to be able to choose which one will be converted. This can be done without saving temporary files and using command line syntax (exec('convert /path/to/file/file.pdf[3] output.jpg')) ?

I will add that I tried

  $im->readimageblob($buf[2]); 

and it didn’t work :)

+1
source share
3 answers

Unfortunately, this is not good news, but I can definitely say that at the time of writing, ImageMagick (and PHP) libraries do not support the page designation you are trying to use. (For people from the future who find this: I check php-imagick-3.0.1 and imagemagick-6.6.0.4).

I'm trying to do the same thing as you, and I just spent the last few hours wading through the source, trying to figure out what it does and how it gets the pages, and it looks like it just won't use it when reading from the stream (i.e. calling readBlob ()).

That way, I'm just going to put it in a temporary file and reading it instead. Not so elegant, but it will work.

+1
source

For those who are still looking for a solution to read a specific page number from blob, check out this question Creating an array filled with images from PDF using PHP and ImageMagick

 $img_array = array(); $im = new imagick(); $im->setResolution(150,150); $im->readImageBlob($pdf_in); $num_pages = $im->getNumberImages(); for($i = 0;$i < $num_pages; $i++) { $im->setIteratorIndex($i); $im->setImageFormat('jpeg'); $img_array[$i] = $im->getImageBlob(); } $im->destroy(); 
+3
source

version 3.0.1 being in the last image or the first image in an imaginary object

$image_obj = new Imagick("test.pdf"); then you are in the last image in $ image_obj

if you use

 fp_pdf = fopen("test.pdf", 'rb'); $image_obj = new Imagick(); $image_obj -> readImageFile($fp_pdf); 

then you are on the first image in $ image_obj

In the second case, to switch to the last image, you can do

 fp_pdf = fopen("test.pdf", 'rb'); $image_obj = new Imagick(); $image_obj -> readImageFile($fp_pdf,2); // 2 can be any positive number? 

then you are in the last image in $ image_obj

 echo $image_obj->getNumberImages() // Returns the number of images in the object 

then

  if ($image_obj->hadPreviousImage) $image_obj->previousImage() //Switch to the previous image in the object } 

or

 if ($image_obj->hasNextImage()) { $image_obj->nextImage()) //Switch to the next image in the object } 

eg. if you have only 6 images and you need 4, then do from the end

 $image_obj->previousImage() $image_obj->previousImage() $image_obj->setImageFormat("png"); header("Content-Type: image/png"); echo $image_obj; 

EDIT: Another find is that you can

 foreach($image_obj as $slides) { echo "<br>".$Obj_img->getImageWidth(); //or wehatever you need to do. } 

EDIT 2: A very simple solution is to use this function $image_obj->setIteratorIndex(4) count starts from scratch.

+2
source

Source: https://habr.com/ru/post/1212473/


All Articles