php tcpdf image string filter

孤街醉人 提交于 2019-12-13 20:24:30

问题


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

which results:

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

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.


回答1:


Finally got it! explode() worked... Then instead of pdf->Image, I just put a temp. image for img tag's src attribute.

  • 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");
    


来源:https://stackoverflow.com/questions/24115215/php-tcpdf-image-string-filter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!