使用 PHP 和 FPDF 插入图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3471441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting an image with PHP and FPDF
提问by Carson
I'm trying to insert an image but do not want to specify the x and y coordinates. Is that possible?
我正在尝试插入图像但不想指定 x 和 y 坐标。那可能吗?
$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.
我希望能够指定大小 (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);
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?
以上是我使用的代码。如果$reportSubtitle 是两行或三行,它会将$prod1title 和$$prod1sub 向下推,不可避免地在固定的图像下。有没有办法让图像像产品标题和副标题一样被推下,同时仍然声明尺寸?
回答by Carson
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.
希望这可以帮助其他人在同一条船上。
回答by Mchl
You can use $pdf->GetX()
and $pdf->GetY()
to get current cooridnates and use them to insert image.
您可以使用$pdf->GetX()
和$pdf->GetY()
获取当前坐标并使用它们插入图像。
$pdf->Image($image1, 5, $pdf->GetY(), 33.78);
$pdf->Image($image1, 5, $pdf->GetY(), 33.78);
or even
甚至
$pdf->Image($image1, 5, null, 33.78);
$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);
$pdf->Image($image1, 5, $pdf->GetY() + 5, 33.78);
回答by Rahul Shaddy
$image="img_name.jpg";
$pdf =new FPDF();
$pdf-> AddPage();
$pdf-> SetFont("Arial","B",10);
$pdf-> Image('profileimage/'.$image,100,15,35,35);
回答by Humphrey
Please note that you should not use any png when you are testing this , first work with jpg .
请注意,在测试时不应使用任何 png,首先使用 jpg。
$myImage = "images/logos/mylogo.jpg"; // this is where you get your Image
$pdf->Image($myImage, 5, $pdf->GetY(), 33.78);
回答by Marc B
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.
您不能将 PDF 视为 HTML 文档。图像不能在文档中“浮动”并让事物围绕它们流动,或与周围的文本一起流动。FPDF可以让你嵌入HTML文本块,但只是因为它分析的标签,并取代<i>
和<b>
等与后记等效命令和。动态放置图像不够聪明。
In other words, you have to specify coordinates (and if you don't, the current location's coordinates will be used anyways).
换句话说,您必须指定坐标(如果不指定,则无论如何都将使用当前位置的坐标)。