php检测pdf中图像的dpi [英] php detect dpi of image in a pdf

查看:198
本文介绍了php检测pdf中图像的dpi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过使用 Imagick::getImageResolution 进行的一些测试在PDF上,我不知道如何获得嵌入在PDF中的图像的分辨率(和色彩空间).我曾尝试将图像从PDF中提取出来,但是在此过程中,DPI似乎被任意设置为72,与我的工作无关.

I've tried a few tests using Imagick::getImageResolution on a PDF, and I can't figure out how to get the resolution (and colourspace) of an image embedded in a PDF. I've tried ripping the image out of the PDF, but during that process it seems the DPI is arbitrarily set to 72 not mater what I do.

我在 1564529 中看到有人说DPI对PDF无关紧要,但这不是真的(当图像嵌入到PDF中,则在PostScript中定义了有关图像的一些属性(例如分辨率). PHP中是否有一种方法(可能与 PSLib 配合使用)来弄清楚是什么?嵌入式图像的DPI是?

I saw in 1564529 someone said DPI doesn't matter to a PDF, but that is not true (when an image is embedded in a PDF, several attributes about the image, like resolution, are defined in the PostScript). Is there a way in PHP (possibly with PSLib?) to figure out what the DPI of an embedded image is?

推荐答案

PDF(或PostScript)中图像的"dpi"比您想象的要模糊得多.这是因为可以以不同的比例渲染PDF,因此actul dpi会有所不同.

The 'dpi' of an image in PDF (or PostScript) is more nebulous than you may think. This is because it is possible to render the PDF at different scales, and so the actul dpi will vary.

您正确的是,存在有关文档中嵌入的图像比例因子的信息.这是当前变换矩阵",但它不像单个值甚至单个矩阵那么简单.

You are correct that there is information regarding the scale factor of the image mebedded in the document. This is the Current Transformation Matrix, but it is not as simple as a single value, or even a single matrix.

CTM将坐标映射到理想化的用户空间",该用户空间名义上以点(每英寸72)定义,但可以无限细分.在渲染方面,用户空间"已应用了进一步的转换,以将其正确缩放到设备空间",这是必需的,因为设备可能不是72 dpi.

The CTM maps co-ordinates into an idealised 'user space' which is nominally defined in points (72 per inch), but is infinitely subdivisible. When it comes to rendering, the 'user space' has a further transformation applied to scale it properly to the 'device space', the transformation is required because the device probably isn't 72 dpi.

您可以在《 PDF参考手册》中找到更全面的说明,尤其是1.7参考中的4.2.1节.

You can find a much fuller explanation of this in the PDF Reference Manual, especially section 4.2.1 in the 1.7 reference.

因此,您似乎所需要做的就是从图像字典中获取已声明的/Width和/Height,并应用/Matrix确定用户空间中图像的大小.假设用户空间有效为72 dpi,那么您将知道图像被缩放到了多少英寸,图像包含了多少像素以及一个简单的除法就可以为您提供所需的答案.

So it would seem that all you need to do is take the declared /Width and /Height from the image dictionary, and apply the /Matrix to determine how big the image is in user space. Given that user space is effectively 72 dpi, then you would know how many inches the image was scaled to, how many pixels the image contains, and a simple division would give you the answer you want.

的确,在某些情况下,这是可行的.但是,从您的角度来看,问题之一是可能确实确实很常见,可以连接矩阵以影响当前缩放,因此仅查看应用于图像的矩阵将不会为您提供应用于该缩放比例的比例因子.图片,因为其他因素可能已经扩展了CTM.另外,PDF包含"UserUnit"合并,允许文件更改用户空间的默认缩放比例.

Indeed, in may cases this will work. However, one of the problems from your point of view, is that is possible, indeed common, to concatenate matrices to affect the current scaling, so simply looking at the matrix applied to an image won't give you the scale factor applied to that image, because something else may have already scaled the CTM. In addition PDF contains the 'UserUnit' kludge which allows a file to alter the default scaling of user space.

因此计算出图像的"dpi"的唯一方法是将页面描述解释到渲染图像的位置,计算出该位置的总缩放比例,然后从中确定图像的面积盖子.然后根据图像的宽度和高度计算出dpi.

So the only way to work out the 'dpi' of an image is to interpret the page description to the point where the image is rendered, work out the total scaling at that point and from there figure out how much area the image covers. Then given the width and height of the image, work out its dpi.

顺便说一句,这对你来说是个难题.完全有可能使用相同的图像数据在PDF中多次绘制相同的图像.您只需要包含一次图像数据.如果我绘制的图像是100像素乘100像素,并且将其绘制为覆盖一平方英寸,则分辨率为100 dpi.现在,我绘制相同的图像,但是我将其缩放为覆盖半英寸.现在,渲染图像的分辨率为200 dpi.

In passing, here's a conundrum for you; its entirely possible to draw the same image multiple times in PDF, using the same image data. You only have to include the image data once. If I draw an image which is 100 pixels by 100 pixels and I draw it to cover one square inch, the resolution is 100 dpi. Now I draw the same image, but I scale it to cover half an inch. The resolution of the rendered image is now 200 dpi.

那么图像的dpi"是什么?

So what is the 'dpi of the image' ?

这篇关于php检测pdf中图像的dpi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆