php tcpdf 图像字符串过滤器 [英] php tcpdf image string filter
问题描述
我一直在尝试通过电子邮件发送包含字段名值列表的表格.当我达到图像值时,我会过滤掉图像字符串以用于 tcpdf 图像输出.我做了一个条件,如果找到 data:image/png ,它应该被删除.否则,删除 data:image/jpeg.
I've been trying to email a table containing a list of fieldname-value. When I reach an image value, I filter out the image string for use in tcpdf image output. I did a conditional that if data:image/png is found, it should be removed. Otherwise, remove data:image/jpeg.
$pdf->writeHTMLCell(50, 0, '', '', "{$fieldname}", 1, 0, 0, true, 'R', true);
$base64 = strpos("data:image/png", $col_value) ? str_replace("data:image/png;base64,", "", $field_value) : str_replace("data:image/jpeg;base64,", "", $field_value);
$imgdata = base64_decode($base64);
$pdf->Image('@'.$imgdata, '', '', 0,65, '', '', 'B', false, 300, 'R');
$pdf->writeHTMLCell(0, 10, '', '', "", 1, 1, 0, true, '', true);
结果:
TCPD 错误:[图像] 无法获取图像的大小:@
TCPDF ERROR: [Image] Unable to get the size of the image: @
如果没有条件/对 $base64 值执行 if/else,我不会收到错误消息.但是我的 base64 不会像两种类型那样灵活.
without the conditional / doing an if/else for the $base64 value, I dont get the error. But my base64 wont be as flexible as with two types.
推荐答案
终于搞定了!爆炸()工作......然后,而不是pdf->图像,我只是放了一个温度.img 标签的 src 属性的图像.
Finally got it! explode() worked... Then instead of pdf->Image, I just put a temp. image for img tag's src attribute.
- 使用 getimagefromstring 获取字符串
- 从字符串中保存临时图像
然后在 $pdf->writeHTMLCell 中使用 img 标签
- get the string with getimagefromstring
- save a temp image from string
then use img tag in $pdf->writeHTMLCell
$base64 = explode(',', $col_value);
$base64d = base64_decode($base64[1]);
$im = imagecreatefromstring($base64d);
if ($im !== false) {
echo 'image ok';
//header('Content-Type: image/png');
imagejpeg($im, $path . "test.jpg");
imagedestroy($im);
echo "<br /> image done";
}
else {
echo 'An error occurred.';
}
$pdf->writeHTMLCell(0, 30, '', '', '<img src="' .$path . 'test.jpg"/>', 1, 1, 0, true, 'C', true);
unlink($path . "test.jpg");
这篇关于php tcpdf 图像字符串过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!