tcpdf根据左下角的坐标放置图像

时间:2017-10-28 16:01:53

标签: php tcpdf

我使用TCPDF,我需要根据图像左下角的坐标放置图像。

TCPDFs image()方法使用左上角作为锚点,我发现无法改变这一点:

Image($file, $x = '', $y = '', $w = 0, $h = 0, $type = '', $link = '', $align = '', $resize = false, $dpi = 300, $palign = '', $ismask = false, $imgmask = false, $border = 0, $fitbox = false, $hidden = false, $fitonpage = false, $alt = false, $altimgs = array() )

我能做的是确定图像的y尺寸,并从左下角的给定y坐标中减去图像的y尺寸。但我也不知道在放置图像之前如何获得图像大小。

1 个答案:

答案 0 :(得分:1)

如果给定左下角的y坐标,则首先运行带有$ y值的图像方法,并将属性$ hidden设置为true。然后使用方法getImageRBY()来检索隐藏图像的底部y坐标。从getImageRBY()得到的坐标中扣除$ y值,这样就可以得到图像的高度。

然后从你的底部y坐标中扣除图像的高度,你得到Image()方法放置图像所需的$ y值:

// my bottom left coordinate of the image
$my_bottom_y_coordinate = 'somevalue';

// This is just to calculate the height
$dummy_y = 'somedummyvalue';

// Run the Image function with $hidden set to true, so the image won't be shown.
$tcpdf->Image($file, $x, $dummy_y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border, $fitbox, TRUE, $fitonpage, $alt, $altimgs);

// get the bottom y-coordinate of the dummy image and deduct it from the
// $dummy_y variable (which was the upper y coordinate of the dummy image) to retrieve the height
$height = $tcpdf->getImageRBY() - $dummy_y;

// deduct the height from the given bottom y coordinate you really want to use. This yields the upper y coordinate you need for the image function.
$y = $my_bottom_y_coordinate - $height;

// run the Image() method again this time with hidden false so the image is actually placed on the pdf page
$tcpdf->Image($file, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border, $fitbox, FALSE, $fitonpage, $alt, $altimgs);