简体   繁体   English

Zend_Pdf使用drawImage()图像未移到左上角

[英]Zend_Pdf using drawImage() Image not moved to top left corner

I am using Zend PDF to generating the pdf file, I used below to create a image or logo in page top left, 我正在使用Zend PDF生成pdf文件,下面在左上角用于创建图片或徽标,

$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. 但是图像显示在左下角,无论何时增加或减小float值,只有图像大小都会改变,不会移动到左上角。

Thanks!! 谢谢!!

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. 尝试使用Zend_Pdf直接渲染图像令人沮丧,主要是因为您必须为drawImage()函数提供要放置图像的区域的所有四个角的坐标,并且要记住坐标系统具有原点位于左下角,而不是左上角。 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 . 我有一个在处理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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM