PHP converts dataurl to image summary,
PHP converts a dataurl to an image
Dataurl is used for images generated using canvas. php cannot directly Save the images to a local computer using the file_put_contents method. transcoding is required.
The dataurl of the image is as follows:
$ Imgstr = 'data: image/png; base64, bytes // 8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg = ';
Method 1:
Extract the data required for dataurl storage using regular expressions, and then directly display the data on the page.
If (! Preg_match ('/data :( [^;] *); base64 ,(. *)/', $ imgstr, $ matches) {die ("error");} $ content = base64_decode ($ matches [2]); header ('content-Type: '. $ matches [1]); header ('content-Length :'. strlen ($ content); echo $ content; die;
Method 2:
If you only want to save the image locally, use the substr and strpos methods.
$ Imgdata = substr ($ imgstr, strpos ($ imgstr, ",") + 1); $ decodedData = base64_decode(1_imgdata1_file_put_contents('11.png ', $ decodedData );
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!