Skip to content

Instantly share code, notes, and snippets.

@timneutkens
Created April 14, 2016 14:36
Show Gist options
  • Save timneutkens/e7b1b20e976dff3d4e8408f4979cac6d to your computer and use it in GitHub Desktop.
Save timneutkens/e7b1b20e976dff3d4e8408f4979cac6d to your computer and use it in GitHub Desktop.
Insert base64 image data into fpdf
<?php
// load the 'fpdf' extension
require('fpdf.php');
// just for demonstration purpose, the OP gets the content from a database instead
$h_img = fopen('img.jpg', "rb");
$img = fread($h_img, filesize('img.jpg'));
fclose($h_img);
// prepare a base64 encoded "data url"
$pic = 'data://text/plain;base64,' . base64_encode($img);
// extract dimensions from image
$info = getimagesize($pic);
// create a simple pdf document to prove this is very well possible:
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello Image!');
$pdf->Image($pic, 10, 30, $info[0], $info[1], 'jpg');
$pdf->Output();
@thirdwheel
Copy link

Shouldn't the data type be the same as the image coming in, or does FPDF genuinely not care?

@thirdwheel
Copy link

Also, the image width and height are optional, so you could get away with leaving those off - YMMV though.

@yusufheri
Copy link

yusufheri commented Sep 28, 2021

Thank you so much, it helped me. May God bless you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment