dongqiang2024 2015-05-28 07:32
浏览 42
已采纳

Zend_Pdf使用drawImage()图像没有移动到左上角

I am using Zend PDF to generating the pdf file, I used below to create a image or logo in page top left,

$image = Zend_Pdf_Image::imageWithPath('my_image.jpg'); 
$pdfPage->drawImage($image, 100, 100, 400, 300);

But images shown in bottom left, when ever I increase or decrease the float value, only the image size will changes not move to top left corner.

Thanks!!

  • 写回答

1条回答 默认 最新

  • doupai8533 2015-06-02 04:27
    关注

    Trying to render images directly with Zend_Pdf is frustrating, mainly because you have to provide the drawImage() function with the co-ordinates of all four corners of the area in which you want the image placed and remember that the co-ordinate system has its origin at the bottom left corner, not the top left corner. If you get the co-ordinates wrong you'll end up with images being rendered upside down, with the wrong aspect ratio, etc.

    I have a wrapper that I use when dealing with Zend_Pdf. I ripped the code below out of the wrapper and tried to adjust it so it would work stand-alone for you. I have not tested this code, but hopefully it still serves as a useful illustration of how to solve your problem.

    function image( $page, $filename, $x_mm, $y_mm, $w_mm = 0 )
    {
        $paperHeight = 297; // For this example, we're using a paper size of A4 in portrait
    
        $size = getimagesize( $filename );
        $width = $size[0];
        $height = $size[1];
    
        if ( $w_mm == 0 )
        {
            $w_mm = pointsToMm( $width );
        }
    
        $h_mm = $height / $width * $w_mm;
    
        $x1 = mmToPoints( $x_mm );
        $x2 = mmToPoints( $x_mm + $w_mm );
        $y1 = mmToPoints( $paperHeight - $y_mm - $h_mm );
        $y2 = mmToPoints( $paperHeight - $y_mm );
    
        $page->drawImage( Zend_Pdf_Image::imageWithPath( $filename ), $x1, $y1, $x2, $y2 );
    
        return $h_mm;
    }
    
    function pointsToMm( $points )
    {
        return $points / 72 * 25.4;
    }
    
    function mmToPoints( $mm )
    {
        return $mm / 25.4 * 72;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制