Created
April 24, 2017 15:39
-
-
Save woraperth/eea4b5e7d07b40cb46a13103603e2053 to your computer and use it in GitHub Desktop.
Add Inline Image in FPDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PDF extends FPDF | |
{ | |
// Inline Image | |
function InlineImage($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='') | |
{ | |
// ----- Code from FPDF->Image() ----- | |
// Put an image on the page | |
if($file=='') | |
$this->Error('Image file name is empty'); | |
if(!isset($this->images[$file])) | |
{ | |
// First use of this image, get info | |
if($type=='') | |
{ | |
$pos = strrpos($file,'.'); | |
if(!$pos) | |
$this->Error('Image file has no extension and no type was specified: '.$file); | |
$type = substr($file,$pos+1); | |
} | |
$type = strtolower($type); | |
if($type=='jpeg') | |
$type = 'jpg'; | |
$mtd = '_parse'.$type; | |
if(!method_exists($this,$mtd)) | |
$this->Error('Unsupported image type: '.$type); | |
$info = $this->$mtd($file); | |
$info['i'] = count($this->images)+1; | |
$this->images[$file] = $info; | |
} | |
else | |
$info = $this->images[$file]; | |
// Automatic width and height calculation if needed | |
if($w==0 && $h==0) | |
{ | |
// Put image at 96 dpi | |
$w = -96; | |
$h = -96; | |
} | |
if($w<0) | |
$w = -$info['w']*72/$w/$this->k; | |
if($h<0) | |
$h = -$info['h']*72/$h/$this->k; | |
if($w==0) | |
$w = $h*$info['w']/$info['h']; | |
if($h==0) | |
$h = $w*$info['h']/$info['w']; | |
// Flowing mode | |
if($y===null) | |
{ | |
if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) | |
{ | |
// Automatic page break | |
$x2 = $this->x; | |
$this->AddPage($this->CurOrientation,$this->CurPageSize,$this->CurRotation); | |
$this->x = $x2; | |
} | |
$y = $this->y; | |
$this->y += $h; | |
} | |
if($x===null) | |
$x = $this->x; | |
$this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i'])); | |
if($link) | |
$this->Link($x,$y,$w,$h,$link); | |
# ----------------------- | |
// Update Y | |
$this->y += $h; | |
} | |
} | |
// Example of adding image | |
$pdf = new PDF(); | |
$pdf->AliasNbPages(); | |
$pdf->AddPage(); | |
$pdf->SetFont('Times','',12); | |
// Add image to document's content (Change 100 to the image's width) | |
$pdf->Cell( 40, 40, $pdf->InlineImage("ds.jpg", $pdf->GetX(), $pdf->GetY(), 100), 0, 0, 'L', false ); | |
// Always add new line after image (Change 20 to any height you want) | |
$pdf->Ln(20); | |
// Add more images | |
$pdf->Cell( 40, 40, $pdf->InlineImage("ds.jpg", $pdf->GetX(), $pdf->GetY(), 100), 0, 0, 'L', false ); | |
$pdf->Ln(20); | |
$pdf->Cell( 40, 40, $pdf->InlineImage("ds.jpg", $pdf->GetX(), $pdf->GetY(), 100), 0, 0, 'L', false ); | |
// Print output | |
$pdf->Output(); |
What if I need to put the inline-image in a cell along with other text?
https://gist.github.com/woraperth/eea4b5e7d07b40cb46a13103603e2053#file-inlineimg-php-L82
@shuklaalok7 Hi! I haven't touched fpdf for some times now. For inline-image next to text, I believe fpdf do not natively have this functionality.
There is a hack here though: https://stackoverflow.com/questions/22358578/aligning-image-next-to-text-in-fpdf
Thank you, @woraperth. It was easier than I thought.
@shuklaalok7 glad to see you solved it 👯
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget to require fpdf like so:
require('fpdf.php');
before adding this code