php - print BASE64 coded image into a FPDF document -
i've base64 stored images in database. possible print data directly in pdf document, using fpdf?
data sctructure of image
data:image/png;base64,ivborw0kggoaaaansuheug7aaafqaaacwcayaaaaonxpvaaafguleqvr4ae2dc9buvvnhusn3qabena9h5a2icjxmj8hukymoqdfclikmrqzr1eydmqapnkrmc8jmhlbbhadjjav0lkqdzcmcqiqq2kerufardhcfdpx /33w tznpe/ 3ttaa9/ a2z9/32xvt5nvvbl2evtdfee9dts27bt4macjmacjm8acjtbvarv123xbbwimyaimyaimiaj26k4hjmacjmacjjaaanboayhez8eetmaetmae7nbdb0zabezabexgaats0adqim6ccziacziacdihuw6ygamygamywaai2kepobcdbrmwarmwarowq3cdmaetmaetmiebelbdh0ahogsmyaimyaimyifuomacjm
i think $pdf->imagepng()
should right function in fpdf.
while comments suggested tcpdf solution, did want state there way entirely fpdf. thought process this:
- strip
data:image/png;base64,
uri usingexplode
- decode uri
base64_decode
- save decoded data png file
file_put_contents
- generate pdf , add png file
image
- delete png file
unlink
always remember error check. if image fails save server not want continue execution. make sure strictly check if functions return false!
here solution. used tiny image small uri in example.
const tempimgloc = 'tempimg.png'; $datauri = "data:image/png;base64,ivborw0kggoaaaansuheugaaabaaaaapcamaaadarb8daaaablbmveuaaadthctekuowaaaaf0leqvr4awogawbe4ziskmbdzqryaqkabl4adhmgwuyaaaaasuvork5cyii="; $datapieces = explode(',',$datauri); $encodedimg = $datapieces[1]; $decodedimg = base64_decode($encodedimg); // check if image decoded if( $decodedimg!==false ) { // save image temporary location if( file_put_contents(tempimgloc,$decodedimg)!==false ) { // open new pdf document , print image $pdf = new fpdf(); $pdf->addpage(); $pdf->image(tempimgloc); $pdf->output(); // delete image server unlink(tempimgloc); } }
Comments
Post a Comment