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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?