24

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?

Carson
  • 4,541
  • 9
  • 39
  • 45

6 Answers6

56

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.

Carson
  • 4,541
  • 9
  • 39
  • 45
  • This is not going to work as you believe. Cell position does not inherit XY from the image. You need to set XY to position Cell. Try showing fill and you will see what I mean. The image and the cell are in two different places. – Gregory Bologna Jun 13 '19 at 14:55
  • 1
    This code makes no sense. `Image()` returns nothing, so calling `Cell()` on it is invalid. See Mchl's answer for the correct way to do. – Olivier Sep 03 '20 at 07:07
13

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);

Mchl
  • 61,444
  • 9
  • 118
  • 120
5
$image="img_name.jpg";
$pdf =new FPDF();
$pdf-> AddPage();
$pdf-> SetFont("Arial","B",10);
$pdf-> Image('profileimage/'.$image,100,15,35,35);
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Rahul Shaddy
  • 51
  • 1
  • 1
  • Welcome to StackOverflow! While answers are always appreciated, this question was asked 6 years ago, and already had an accepted solution. Please try to avoid 'bumping' questions to the top by providing answers to them, unless the question was not already marked as resolved, or you found a dramatically better alternative approach to the problem. Also make sure to provide context to full explain how your code solves the problem at hand :) – Obsidian Age Feb 28 '17 at 23:40
  • It runs fine at FPDF 1.6 – 151291 May 27 '17 at 11:33
0

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);
Humphrey
  • 2,659
  • 3
  • 28
  • 38
0

// 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.

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
  • Well just like the other user that posted a variation of this answer, this isn't going to work. And this is really almost identical without and reasoning why it will work or is better than the other answer. So if you can add some more details it might have a fighting chance of not getting deleted. – treckstar Jun 22 '22 at 13:22
-1

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).

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • At most you could try to calculate the text size (maybe with `GetStringWidth()` and see if it'd require adjusting the image's coordinates. But again, you can NOT insert an image into a PDF without providing coordinates. Nothing says you can't adjust the coordinates based on other content, but you can't insert blindly. – Marc B Aug 12 '10 at 20:26