使用 PHP 和 FPDF 插入图像

发布于 2024-09-14 06:53:20 字数 699 浏览 4 评论 0原文

我正在尝试插入图像,但不想指定 x 和 y 坐标。这可能吗?

$pdf->Image($image1, 5, 70, 33.78);

我希望能够指定大小 (33.78),但不能指定 x 和 y,以便它根据内容移动。

$pdf->Write( 70, $reportTitle );
$pdf->Ln( 45 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $reportSubtitle );

/**
  Create product 1
**/
$pdf->Ln( 10 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $prod1title );
$pdf->Ln( 30 );
$pdf->SetFont( 'Arial', '', 10 );
$pdf->Write( 5, $prod1sub );
$pdf->Ln( 30 );
$pdf->Image($image1, 5, 70, 33.78);

上面是我使用的代码。如果 $reportSubtitle 是两行或三行,它会将 $prod1title 和 $$prod1sub 向下推,并且不可避免地位于固定的图像下方。有没有办法让图像像产品标题和副标题一样被向下推,同时仍然声明尺寸?

I'm trying to insert an image but do not want to specify the x and y coordinates. Is that possible?

$pdf->Image($image1, 5, 70, 33.78);

I want to be able to specify the size (33.78) but not the x and y so that it moves based on the content.

$pdf->Write( 70, $reportTitle );
$pdf->Ln( 45 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $reportSubtitle );

/**
  Create product 1
**/
$pdf->Ln( 10 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $prod1title );
$pdf->Ln( 30 );
$pdf->SetFont( 'Arial', '', 10 );
$pdf->Write( 5, $prod1sub );
$pdf->Ln( 30 );
$pdf->Image($image1, 5, 70, 33.78);

The above is the code I use. If $reportSubtitle is two or three lines, it pushes $prod1title and $$prod1sub down, and inevitably under the image that is fixed. Is there no way to have the image act like the product title and subtitle and be pushed down too while still declaring the size?

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

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

发布评论

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

评论(6

夏夜暖风 2024-09-21 06:53:20

我明白了,而且实际上非常简单。

设置变量:

$image1 = "img/products/image1.jpg";

然后创建一个单元格,定位它,然后不要设置图像的位置,而是使用上面创建的变量,如下所示:

$this->Cell( 40, 40, $pdf->Image($image1, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false );

现在,如果单元格周围的其他单元格移动,单元格将随内容上下移动。

希望这可以帮助其他有同样想法的人。

I figured it out, and it's actually pretty straight forward.

Set your variable:

$image1 = "img/products/image1.jpg";

Then ceate a cell, position it, then rather than setting where the image is, use the variable you created above with the following:

$this->Cell( 40, 40, $pdf->Image($image1, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false );

Now the cell will move up and down with content if other cells around it move.

Hope this helps others in the same boat.

ι不睡觉的鱼゛ 2024-09-21 06:53:20

您可以使用 $pdf->GetX()$pdf->GetY() 获取当前坐标并使用它们插入图像。

$pdf->Image($image1, 5, $pdf->GetY(), 33.78);

甚至

$pdf->Image($image1, 5, null, 33.78);

(尽管在第一种情况下,您可以添加一个数字来创建一点空间)

$pdf->Image($image1, 5, $pdf->GetY() + 5 , 33.78);

You can use $pdf->GetX() and $pdf->GetY() to get current cooridnates and use them to insert image.

$pdf->Image($image1, 5, $pdf->GetY(), 33.78);

or even

$pdf->Image($image1, 5, null, 33.78);

(ALthough in first case you can add a number to create a bit of a space)

$pdf->Image($image1, 5, $pdf->GetY() + 5, 33.78);

贵在坚持 2024-09-21 06:53:20
$image="img_name.jpg";
$pdf =new FPDF();
$pdf-> AddPage();
$pdf-> SetFont("Arial","B",10);
$pdf-> Image('profileimage/'.$image,100,15,35,35);
$image="img_name.jpg";
$pdf =new FPDF();
$pdf-> AddPage();
$pdf-> SetFont("Arial","B",10);
$pdf-> Image('profileimage/'.$image,100,15,35,35);
栀子花开つ 2024-09-21 06:53:20

请注意,测试时不应使用任何 png,首先使用 jpg。

$myImage = "images/logos/mylogo.jpg";  // this is where you get your Image

$pdf->Image($myImage, 5, $pdf->GetY(), 33.78);

Please note that you should not use any png when you are testing this , first work with jpg .

$myImage = "images/logos/mylogo.jpg";  // this is where you get your Image

$pdf->Image($myImage, 5, $pdf->GetY(), 33.78);
扎心 2024-09-21 06:53:20

// 图像 URL

$url = 'img/img.png';

// 将图像放入 pdf 文档中

$pdf->Cell(30, 30, $pdf => Image($url, 5, $pdf => GetY(), 93.78), 0, 0, 'L', false);

93.78 是图像的大小。
5是从左侧算起的位置。

// Image URL

$url = 'img/img.png';

// Place the image in the pdf document

$pdf->Cell(30, 30, $pdf => Image($url, 5, $pdf => GetY(), 93.78), 0, 0, 'L', false);

The 93.78 is the size of the image.
5 is the position from the left side.

苯莒 2024-09-21 06:53:20

您不能像对待 HTML 文档那样对待 PDF。图像不能在文档中“浮动”,也不能让内容在其周围流动,或与周围的文本一起流动。 FPDF 允许您在文本块中嵌入 html,但这只是因为它解析标签并用 Postscript 等效命令替换 等。它不够智能,无法动态放置图像。

换句话说,您必须指定坐标(如果不指定,则无论如何都会使用当前位置的坐标)。

You can't treat a PDF like an HTML document. Images can't "float" within a document and have things flow around them, or flow with surrounding text. FPDF allows you to embed html in a text block, but only because it parses the tags and replaces <i> and <b> and so on with Postscript equivalent commands. It's not smart enough to dynamically place an image.

In other words, you have to specify coordinates (and if you don't, the current location's coordinates will be used anyways).

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