与此搏斗了几个小时,无法弄清楚PDFlib的工作原理。我有这个图像,我们说它的大小为1000 x 300像素:
现在我想将图像的一部分放入一个20 x 12毫米(300 dpi)的盒子中。调整大小必须是这样的,如果没有进行裁剪,图像将在页面上占用70 x 21毫米。必须从顶部裁剪3毫米的图像,并从左侧裁剪2毫米。
示例和文档太弱,我无法准确理解fit_image()
的参数是如何工作的。我该如何做到这一点?
注意:我在PHP中这样做但我猜主要关注的是PDFlib需要什么参数,这与语言无关。
答案 0 :(得分:0)
要解决这个问题需要几个步骤,但当然很容易实现。您的问题包含必须解决的各种任务:
这是解决问题的一种方法:
# Load the image
$image = $p->load_image("auto", $imagefile, "");
# First we retrieve the dimension and the resolution for the loaded image
$imagewidth = $p->info_image($image, "imagewidth", "");
$imageheight = $p->info_image($image, "imageheight", "");
$dpix = $p->info_image($image, "resx", "");
$dpiy = $p->info_image($image, "resy", "");
# Calculate the scale factor, to fit the image to a width/height of 70 x 21 mm.
# Use a helper function to calculate the mm-values to the PDF points
$scalex = mm2pt(70) / $imagewidth;
$scaley = mm2pt(21) / $imageheight;
# For demonstrating the correct placing, fit the loaded image with a
# size of 70x21 mm with a light opacity (scaling it to this dimension
# might distort the image ratio) (final code would not include this)
$gstate = $p->create_gstate("opacityfill=.4");
$optlist = sprintf("gstate=%d scale {%f %f} dpi=72",
$gstate, $scalex, $scaley);
$p->fit_image($image, mm2pt(10), mm2pt(250), $optlist);
# Use dpi=72 to ignore the internal DPI value and interpret each image
# pixel without scaling.
# Now, specify the partial area with a matchbox clipping (remember that
# those values are the positions within the 70x21, and y goes from bottom to top)
$optlist = sprintf("scale {%f %f} matchbox={clipping={%f %f %f %f}} dpi=72",
$scalex, $scaley, mm2pt(2)/$scalex, mm2pt(6)/$scaley,
mm2pt(22)/$scalex, mm2pt(18)/$scaley);
# Set the reference point, so the origin of the clipped image will be the
# same as for the original image
$p->fit_image($image, mm2pt(10)+mm2pt(2), mm2pt(250)+mm2pt(6), $optlist);
function mm2pt($mm){
return $mm*2.83465;
}