Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the PostgreSQL bytea value into Data URL value using PHP

PostgreSQL has this datatype called bytea. It is their version of a blob.

In displaying images, png/jpg/gif, the current trend nowadays is to use Data URLs.

My question is how to convert a bytea value into a Base64 Data URL value?

References

  • bytea - Binary Data Types. PostgreSQL 9.6 Documentation
  • Data URL - Copy image as data URI by Umar Hansa. Google Developers. May 2015
like image 334
Abel Callejo Avatar asked Apr 13 '26 14:04

Abel Callejo


1 Answers

You may also fetch the complete image data URL from your database, e.g.:

SELECT 'data:image/gif;base64,' || encode(image_data, 'base64') AS image_url 
FROM ...  
like image 83
clemens Avatar answered Apr 15 '26 03:04

clemens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!