print BASE64 coded image into a FPDF document

前端 未结 7 1804
我寻月下人不归
我寻月下人不归 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:32

    To ensure the images work with FPDF and to avoid errors, here is a modified version of @pedro-soares' code:

    function getImage($dataURI){
      $img = explode(',',$dataURI,2);
      $pic = 'data://text/plain;base64,'.$img[1];
      $type = explode("/", explode(':', substr($dataURI, 0, strpos($dataURI, ';')))[1])[1]; // get the image type
      if ($type=="png"||$type=="jpeg"||$type=="gif") return array($pic, $type);
      return false;
    }
    

    Because FPDF only allows these three file types, it is important to check that it is valid. To use:

    $pic = getImage($Base64StringGoesHere);
    if ($pic!==false) $pdf->Image($pic[0], 10,30,0,0, $pic[1]);
    

提交回复
热议问题