TCPDF 并插入一个 base64 编码的图像

TCPDF and insert an image base64 encoded

我从 Mysql 数据库中得到了这个字符串:

$img_base64_encoded = 

'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMzgiIGhlaWdodD0iNTIiPjxwYXRoIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2U9InJnYigxMjEsIDEyMSwgMTIxKSIgZmlsbD0ibm9uZSIgZD0iTSA5IDggYyAtMC4wMyAwLjM1IC0xLjE5IDEzLjIzIC0yIDIwIGMgLTAuMjEgMS43MiAtMC40MSAzLjQ4IC0xIDUgYyAtMS4wMyAyLjY3IC0zLjI2IDUuMzMgLTQgOCBjIC0wLjgzIDMgLTEgMTAgLTEgMTAiLz48cGF0aCBzdHJva2UtbGluZWpvaW49InJvdW5kIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlPSJyZ2IoMTIxLCAxMjEsIDEyMSkiIGZpbGw9Im5vbmUiIGQ9Ik0gOSAyOSBsIDExIC01Ii8+PHBhdGggc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZT0icmdiKDEyMSwgMTIxLCAxMjEpIiBmaWxsPSJub25lIiBkPSJNIDIxIDYgYyAwIDAuNjEgMC41MiAyNS4wNiAwIDM1IGMgLTAuMDUgMS4wMiAtMiAzIC0yIDMiLz48cGF0aCBzdHJva2UtbGluZWpvaW49InJvdW5kIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlPSJyZ2IoMTIxLCAxMjEsIDEyMSkiIGZpbGw9Im5vbmUiIGQ9Ik0gMzUgMSBjIDAuMDQgMC4wNSAxLjg5IDEuOTggMiAzIGMgMC40OCA0LjUzIDAuNzUgMTEuMTQgMCAxNiBjIC0wLjUgMy4yNyAtMy4zOCA2Ljc1IC00IDEwIGMgLTAuNjMgMy4zMiAwIDExIDAgMTEiLz48L3N2Zz4='

它在图像中显示 "HI"。 现在我想将这个直接编码的图像嵌入到使用 TCPDF 插件生成的 PDF 中,但总是出现错误:

TCPDF ERROR: [Image] Unable to get the size of the image: 

这是插入方式:

$img = '<img src="' . $img_base64_encoded . '">';
$pdf->writeHTML($img, true, false, true, false, '');

我也尝试过:

  $pdf->Image('@' . $img_base64_encoded);

同样的错误。

您不能在 src 中使用 base64 流,而是先将流保存到文件然后再使用它

$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, '');

嗯,你实际上可以,看看你只需要在 base64 编码字符串前添加一个“@”的代码:

$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, '');

已使用最新版本的 TCPDF 进行测试

不需要写成HTML

    $img = base64_decode(preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded));
    $this->tcpdf->Image("@".$img, 68, 208, 46, 46);

对于那些为输出生成 HTML 变量的人,我是这样工作的: 请注意,我正在使用找到的 tc-lib-barcode 库生成条形码 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">';

这段代码对我有用。唯一需要注意的是删除“data:image/;base64”,这是我在数据库中的图片值的开头:

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

注意:我使用setImageScale缩小图片尺寸

使用此代码成功地让 TCPDF 显示保存在 mysql 数据库中的图像。从数据库读取后无需保存。 礼貌一点,从 header 数据中提取 imagetype 以将其提供给 Image API 方法。

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