fpdf getTringHeight等效于getTringWidth

发布于 2025-01-21 05:00:33 字数 641 浏览 0 评论 0原文

我正在尝试实现(在PHP中使用 fpdf https://www.fpdf.org < /a>)a getStringHeight($ string)方法,让我回到了传递的字符串的像素


fpdf 具有非常有用的getStringwidth方法,但是,即使看了看源代码,我也找不到getstringheight
我唯一可能的想法是,我会在某个地方渲染它,然后当我看到一些像素时,我会检查位图本身...还有类似的东西(还有一些IFS ...):

y = yCoordinateOfTheString
while thisRowOfPixelsIsTotallyEmpty(y)
  ++y
start = y
while thereIsAtLeastOnePixelInThisRow(y)
  ++y
end = y
height = end - start + 1

当然,这将是疯狂的效率低下,所以...还有另一种方法吗?谢谢

I am trying to implement (in PHP using fpdf, https://www.fpdf.org) a getStringHeight($string) method, giving me back the height in pixel of the passed string

fpdf has the very useful getStringWidth method but, even looking into the source code, I couldn't find anything like getStringHeight.

The only possible idea that I have, is that I would render it somewhere, and then I would check in the bitmap itself when I see some pixels... With something like this (with some more ifs...):

y = yCoordinateOfTheString
while thisRowOfPixelsIsTotallyEmpty(y)
  ++y
start = y
while thereIsAtLeastOnePixelInThisRow(y)
  ++y
end = y
height = end - start + 1

Of course, this would be crazy inefficient, so... Is there another way? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜中书 2025-01-28 05:00:33

GetStringWidth()为您提供输出的宽度,就好像它都在一条线上一样,但是实际的输出自然会分为右键截距的多条线。因此,请拿到getPageWidth(),从左右边缘减去,然后将您的getTringWidth()除以此图,以确定所需的行数。将其ceil()乘以您将要传递的任何线高(),这应该等于输出高度。

如果您没有从页面的左侧输出,那么您需要将缩进添加到getTringWidth()中,以说明所需的潜在额外行。

http://www.fpdf.org/ 有关这些功能的详细信息。

$pdf->SetFont('Arial', '', 14);

$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean non risus porta tortor sodales aliquet. Cras efficitur, purus ut molestie ultrices, eros urna egestas lacus, vel porttitor ante neque in est. Aliquam gravida metus enim, ut dignissim risus mattis eget. Aliquam accumsan lobortis elit molestie scelerisque.';
$singleLineWidth = $pdf->GetStringWidth($string);
$numberLines = ceil(($pdf->GetX() + $singleLineWidth) / ($pdf->GetPageWidth() - 20)); // 20 accounts for the 10mm default margins
$lineHeight = 8;
$heightOfOutput = $numberLines * $lineHeight;

// Now use $heightOfOutput to, for example, determine if a page break is needed before output
if($pdf->GetY() > $pdf->GetPageHeight() - $heightOfOutput)
    $pdf->AddPage();

$pdf->Write($lineHeight, $string);

GetStringWidth() gives you the width of the output as if it were all on one line, but the actual output will naturally split into multiiple lines where the right margin intercepts. So take GetPageWidth(), minus off the left and right margins, and then divide your GetStringWidth() by this figure to determine the number of lines needed. Multiply the ceil() of this by whatever line height you're about to pass to Write() and this should equal the output height.

If you're not not outputting from the left of the page though then you'll want to add that indentation on to GetStringWidth() to account for the potential extra line needed.

http://www.fpdf.org/ for details on each of these functions.

$pdf->SetFont('Arial', '', 14);

$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean non risus porta tortor sodales aliquet. Cras efficitur, purus ut molestie ultrices, eros urna egestas lacus, vel porttitor ante neque in est. Aliquam gravida metus enim, ut dignissim risus mattis eget. Aliquam accumsan lobortis elit molestie scelerisque.';
$singleLineWidth = $pdf->GetStringWidth($string);
$numberLines = ceil(($pdf->GetX() + $singleLineWidth) / ($pdf->GetPageWidth() - 20)); // 20 accounts for the 10mm default margins
$lineHeight = 8;
$heightOfOutput = $numberLines * $lineHeight;

// Now use $heightOfOutput to, for example, determine if a page break is needed before output
if($pdf->GetY() > $pdf->GetPageHeight() - $heightOfOutput)
    $pdf->AddPage();

$pdf->Write($lineHeight, $string);
夕色琉璃 2025-01-28 05:00:33

重新阅读您的问题,如果您实际上仅使用FPDF作为其getTringWidth()方法,而实际上并没有尝试输出PDF,那么您可能真的只需Imagettfbbox()。

https://wwwww.php.net/manual/manual/en/en/en/function.imagettfbboxboxboxboxboxbox。 php

Re-reading your question, if you're only actually using FPDF for its GetStringWidth() method and you're not actually trying to output a PDF, you probably just want imagettfbbox() really.

https://www.php.net/manual/en/function.imagettfbbox.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文