PHP convert Dataurl to image Image method

Source: Internet
Author: User
This article mainly introduces the PHP will dataurl to the image image method of the relevant data, here provides two methods and implementation methods, the need for friends can refer to the following

PHP convert Dataurl to image Image method

Using canvas generated pictures, is using Dataurl, PHP can not be saved directly through the File_put_contents method to the local computer, need to do a transcoding.

Picture Dataurl as follows

$imgstr = ' data:image/png;base64,ivborw0kggoaaaansuheugaaaauaaaafcayaaacnbyblaaaaheleqvqi12p4//8/ w38giaxdibke0dhxgljnbaao9txl0y4ohwaaaabjru5erkjggg== ';

Method One:

Extract the data needed for the Dataurl store via regular, then show directly 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;d ie;

Method Two:

If you just want to save the image locally, you can use the substr and Strpos methods

$imgdata = substr ($imgstr, Strpos ($imgstr, ",") + 1); $decodedData = Base64_decode ($imgdata); File_put_contents (' 11.png ') , $decodedData);

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.