0

I want to convert a multipages PDF to a series of png files, while the PDF is portrait and png file is landscape, using imagemagick. I have achieve the conversion from PDF portrait to png protrait(code attached below). But have no idea how to make portrait image to landsacpe with no distortion, (not rotate the image). Can anyone kindly help me? Thanks.

$infile=$direcory."/Test.pdf";
$images=new Imagick();
$bodercolor=new ImagickPixel("white");

$images->setResolution(220, 220);
$images->readimage($infile);
$images->setimageformat("png");

foreach ($images as $i=>$image){    
   //set backgroud color = white
   $image->setimagebordercolor($bodercolor);
   $image->borderimage($bodercolor, 0, 0);

   $image->writeimage($direcory."/test-pg".$i.".png");  
}

$images->clear();
$images->destroy();

Actually, what I want is rotate the vertical page to horizontal with the objects on it have almost no changes. Like what we are able to do using Microsoft Word/PowerPoint when you change page orientation to landscape.

Bunny
  • 3
  • 4
  • You can't change a rectangle's aspect ratio (i.e. turn a tall rectangle into a wide one) without either distorting or cropping it. – Wyzard Jul 25 '14 at 06:11
  • Thanks for replying. I just edited my post. Please see the last paragraph, which state what i actually want. Do you have any idea, Wyzard? Or any one else? – Bunny Jul 30 '14 at 08:39

2 Answers2

1

Well, your task then seems to be two steps: gettng png from pdf (which you say you have done) and getting png image to change sizes.

Question yourself what do you want to do - rotate the vertical image to be horizontal? Crop some part of the vertical image and fit it into horizontal space?

What you need to do is to decide the answer to the above question. After that - take a pen, piece of paper and draw the process. Draw the vertical rectangle, then fill in some area that is you content on that page, then rotate/transform/resize/fit and etc - whatever your answer was that content area into horizontal rectangle.

After that you can write your code - deal with pixel width/height, rotation angle, fitting the area to the horizontal space and etc.

Anatoliy Kim
  • 768
  • 4
  • 13
  • Thanks for replying. I just edited my post. Please see the last paragraph, which states what i actually want. And I do need do the 2 steps. But I don't mind change the order of the 2 steps, which means if changing the orientation of pdf file, and then change pdf to png is also acceptable, as long as I can get what I want. – Bunny Jul 30 '14 at 08:49
  • Copy and resize your image to fit horizontal limits. Use the paper - draw the vertical rectangle and horizontal. Place content on the vertical and see what you need to do to transform it to horizontal. Apply necessary calculations to the image. PHP has many functions to deal with rotating, resizing and transforming an image - use them. – Anatoliy Kim Jul 30 '14 at 12:19
  • Thanks for replying. Do you means resize vertical image and directly put it in middle of landscape page? which means I need to do scaling, make the height of vertical image equal to height of horizontal image and my result image will have a wide border at both side? – Bunny Jul 31 '14 at 03:12
  • Draw it, man... When in doubt - take paper and draw the steps out. See what you have - input, see what you want to receive - output, see what you need to do - process, function, method. Create algorithm for calculating what you need to do. Consider all the different cases. That's the programming. Knowing the language and all the different functions and commands in it comes after learning how to plan and create program - algorithm. – Anatoliy Kim Jul 31 '14 at 07:17
1

When you change a slide's orientation in PowerPoint, it recomputes the whole layout: centered elements are shifted left or right based on the new width, multi-line text is re-wrapped, and so on. This is possible because PowerPoint knows how the elements are supposed to be formatted.

You can't do that with a PDF file because it doesn't contain that kind of high-level formatting information; a PDF is basically just a description of where to put ink on paper. Reading a PDF file, you don't know that a particular line of text is supposed to be centered; you just know the (X, Y) coordinates where its top-left corner should be positioned. If one line of text appears below another, there's nothing that tells you whether words at the beginning of the second line should be moved to the end of the first when the page gets wider (e.g. word-wrapping) or if they're unrelated items (like separate bullet points) that should not be combined.

Fundamentally, a PDF is more like a picture than an editable document.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
  • Thanks for replying. But what if I only have one or two images placed in the pdf file? can I know the x,y coordinates of the image 4 corner? Thus, I can abstract the images and put it properly in landscape blank page. – Bunny Aug 01 '14 at 01:13