Home > Article > Backend Development > Detailed explanation of PHP method of converting dataurl into image image
This article mainly introduces the relevant information about the method of converting dataurl into image image in PHP. Here are two methods and implementation methods. Friends in need can refer to it
PHP Convert dataurl to image Image method
The image generated using canvas uses dataurl. PHP cannot directly save it to the local computer through the file_put_contents method and needs to be transcoded.
The picture dataurl is as follows
$imgstr = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
Method one:
Extract the data required for dataurl storage through regular expressions, and then display it 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; die;
Method 2:
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 it will be helpful to everyone's study.
Related recommendations:
phpAlipay Series Computer Website Payment
##php Performance Analysisphp-Usage of fpm slow execution log slow log
phpRealize cross-domain form submission Methods
The above is the detailed content of Detailed explanation of PHP method of converting dataurl into image image. For more information, please follow other related articles on the PHP Chinese website!