print BASE64 coded image into a FPDF document

前端 未结 7 1854
我寻月下人不归
我寻月下人不归 2021-01-04 13:03

I\'ve some as base64 stored images in a database. Is it possible to print those data directly in a PDF document, using FPDF?

Data sctructure of the image

<         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 13:43

    I'know this is an old topic, but there is a easier way of solving this problem. Just try this simple steps:

    1. Strip data:image/png;base64 from URI using explode
    2. Concatenate data://text/plain;base64, with what is left from the stripped URI
    3. Use the result string(base64) as argument of FPDF Image()

    Example:

    $dataURI= "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAMAAADarb8dAAAABlBMVEUAAADtHCTeKUOwAAAAF0lEQVR4AWOgAWBE4zISkMbDZQRyaQkABl4ADHmgWUYAAAAASUVORK5CYII=";
    
    $img = explode(',',$logo,2);
    $pic = 'data://text/plain;base64,'. $img;
    
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->Image($pic, 10,30,0,0,'png');
    $pdf->Output();
    

    Remember of choose the correct image type on Image() function.

提交回复
热议问题