简体   繁体   English

使用Imagemagick和PHP将PDF纵向转换为图像横向

[英]Convert PDF portrait to image landscape using Imagemagick and PHP

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. 我想使用imagemagick将多页PDF转换为一系列png文件,而PDF是纵向文件,而png文件是横向文件。 I have achieve the conversion from PDF portrait to png protrait(code attached below). 我已经实现了从PDF肖像到png protrait的转换(下面附有代码)。 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. 就像我们将页面方向更改为横向时使用Microsoft Word / PowerPoint所能够执行的操作一样。

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. 好吧,您的任务似乎有两个步骤:从pdf中获取png(您说完成了)和获取png图像以更改大小。

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. 之后,您可以编写代码-处理像素的宽度/高度,旋转角度,使区域适合水平空间等。

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. 当您在PowerPoint中更改幻灯片的方向时,它将重新计算整个布局:居中的元素根据新的宽度向左或向右移动,重新包装多行文本,依此类推。 This is possible because PowerPoint knows how the elements are supposed to be formatted. 这是可能的,因为PowerPoint知道元素应该如何格式化。

You can't do that with a PDF file because it doesn't contain that kind of high-level formatting information; 您无法使用PDF文件来执行此操作,因为它不包含这种高级格式信息。 a PDF is basically just a description of where to put ink on paper. PDF基本上只是对在纸上放置墨水的位置的描述。 Reading a PDF file, you don't know that a particular line of text is supposed to be centered; 阅读PDF文件时,您不知道应该将特定的一行文本居中。 you just know the (X, Y) coordinates where its top-left corner should be positioned. 您只知道(X,Y)坐标应该位于其左上角。 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 (eg 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. 从根本上讲,PDF比可编辑文档更像图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM