duanpi7578 2016-08-09 01:27
浏览 321
已采纳

如何使用PDFlib精确放置裁剪和调整大小的图像?

Wrestled with this for a couple of hours and just can't figure out how PDFlib works. I have this image that we'll say has the size of 1000 x 300 pixels:

enter image description here

Now I want to place a part of the image into a box of 20 x 12 millimeters (at 300 dpi). The resizing must be such that if no cropping were done the image would take up 70 x 21 mm on the page. 3 mm of the image must be cropped away from the top, and 2 mm cropped away from the left side.

enter image description here

The examples and documentation are too weak for me to understand exactly how the parameters for fit_image() work. How do I accomplish this?

Note: I'm doing this in PHP but I guess the main concern is what parameters PDFlib needs, which is language independent.

  • 写回答

1条回答 默认 最新

  • dongxuan58311366668 2016-08-09 04:54
    关注

    There are a few steps required to get this solved, but of course it's easily possible. Your question contains various task which must be solved:

    1. scale down the input image to a given area
    2. clip this scaled down image
    3. place the image scaled and clipped to a given position. (this was not exactly clear from your question, so you might adjust the x/y parameters of the fit_image() depending to your needs.

    This is one way to solve it:

    # 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;
    }
    

    so when using this code and one of the PDFlib sample Images to place the partially image on top of the original image, I get the following output: enter image description here

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部