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AtMiAzIC0yIDMiLz48cGF0aCBzdHJva2UtbGluZWpvaW49InJvdW5kIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlPSJyZ2IoMTIxLCAxMjEsIDEyMSkiIGZpbGw9Im5vbmUiIGQ9Ik0gMzUgMSBjIDAuMDQgMC4wNSAxLjg5IDEuOTggMiAzIGMgMC40OCA0LjUzIDAuNzUgMTEuMTQgMCAxNiBjIC0wLjUgMy4yNyAtMy4zOCA2Ljc1IC00IDEwIGMgLTAuNjMgMy4zMiAwIDExIDAgMTEiLz48L3N2Zz4='
It show "HI" in a image. Now i want to embed this image encoded directly inside a PDF generated with TCPDF plugin but always get error:
TCPDF ERROR: [Image] Unable to get the size of the image:
And this is how to insert it:
$img = '<img src="' . $img_base64_encoded . '">';
$pdf->writeHTML($img, true, false, true, false, '');
I've also tried with:
$pdf->Image('@' . $img_base64_encoded);
with the same error.
Solution 1:[1]
You cannot use base64 stream in src rather first save the stream to a file then use it
$img_base64_encoded = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gA...';
$imageContent = file_get_contents($img_base64_encoded);
$path = tempnam(sys_get_temp_dir(), 'prefix');
file_put_contents ($path, $imageContent);
$img = '<img src="' . $path . '">';
$pdf->writeHTML($img, true, false, true, false, '');
Solution 2:[2]
Well, you can actually, looking at the code you just neet to add a '@' before the base64 encoded string:
$img_base64_encoded = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gA...';
$img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
$pdf->writeHTML($img, true, false, true, false, '');
Tested with the last version of TCPDF
Solution 3:[3]
No need to write as HTML
$img = base64_decode(preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded));
$this->tcpdf->Image("@".$img, 68, 208, 46, 46);
Solution 4:[4]
For those who generate an HTML variable for output, this is how I got it working: Note that I am generating a barcode using the tc-lib-barcode library found here
$img_base64_encoded = 'data:image/png;base64,' . base64_encode($bobj->getPngData());
$text.= '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '" width="200" height="30">';
Solution 5:[5]
This code works for me. The only point to focus is the remove of "data:image/;base64," which is the beginning of my picture value in the database:
$img_base64_encoded = str_replace("data:image/;base64,", "", $logo);
// Image from data stream ('PHP rules')
$imgLogo = base64_decode($img_base64_encoded);
$this->setImageScale(7);
$this->Image('@'.$imgLogo);
Note: I use setImageScale to reduce the picture size
Solution 6:[6]
Used this code with success for TCPDF to show image saved on mysql database. No need to save after reading from DB. A little bit more polite, extracting imagetype from header data to provide it to Image API method.
// check if there is a database image set
if ($f_photo==""){
$f_photoshow="/home/customer/www/mysite/public_html/images/default.png";
}else{
//define type (png/jpg) extracted from header - ex: data:image/jpeg;base64,/9j/4AAQS.....
$datasegments = explode(',',$f_photo);
$extension = explode('/', $datasegments[0]);
$imagetype = explode(';', $extension[1]);
$f_photoshow = '@' . base64_decode($datasegments[1]);
}
$mypositionY=$pdf->GetY()+7;
$mypositionX=$pdf->GetX()+7;
// Image([image], x, y, w, h, imagetype, link, align_after);
$pdf->Image($f_photoshow,$mypositionX,$mypositionY,11,null,$imagetype[0],null,'N');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vinay |
| Solution 2 | Gianluca |
| Solution 3 | sdespont |
| Solution 4 | Hendrik van der Merwe |
| Solution 5 | Dharman |
| Solution 6 | Orison Almeida |
