Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

php - 如何从base64数据字符串保存PNG图片服务器端(How to save a PNG image server-side, from a base64 data string)

I'm using Nihilogic's "Canvas2Image" JavaScript tool to convert canvas drawings to PNG images.(我正在使用Nihilogic的“ Canvas2Image” JavaScript工具将画布图形转换为PNG图像。)

What I need now is to turn those base64 strings that this tool generates, into actual PNG files on the server, using PHP.(现在,我需要使用PHP将此工具生成的base64字符串转换为服务器上的实际PNG文件。) In short, what I'm currently doing is to generate a file on the client side using Canvas2Image, then retrieve the base64-encoded data and send it to the server using AJAX:(简而言之,我当前正在做的是使用Canvas2Image在客户端生成一个文件,然后检索base64编码的数据,并使用AJAX将其发送到服务器:) // Generate the image file var image = Canvas2Image.saveAsPNG(canvas, true); image.id = "canvasimage"; canvas.parentNode.replaceChild(image, canvas); var url = 'hidden.php', data = $('#canvasimage').attr('src'); $.ajax({ type: "POST", url: url, dataType: 'text', data: { base64data : data } }); At this point, "hidden.php" receives a data block that looks like data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABE...(此时,“ hidden.php”收到的数据块看起来像data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAABE ...) From this point on, I'm pretty much stumped.(从这一点开始,我很困惑。) From what I've read, I believe that I'm supposed to use PHP's imagecreatefromstring function, but I'm not sure how to actually create an actual PNG image from the base64-encoded string and store it on my server.(通过阅读,我相信应该使用PHP的imagecreatefromstring函数,但是我不确定如何从base64编码的字符串实际创建实际的PNG图像并将其存储在服务器上。) Please aid!(请帮忙!)   ask by Andrei Oniga translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to extract the base64 image data from that string, decode it and then you can save it to disk, you don't need GD since it already is a png.(您需要从该字符串中提取base64图像数据,对其进行解码,然后可以将其保存到磁盘,因为它已经是png,所以不需要GD。)

$data = 'data:image/png;base64,AAAFBfj42Pj4'; list($type, $data) = explode(';', $data); list(, $data) = explode(',', $data); $data = base64_decode($data); file_put_contents('/tmp/image.png', $data); And as a one-liner:(作为单线:) $data = base64_decode(preg_replace('#^data:image/w+;base64,#i', '', $data)); An efficient method for extracting, decoding, and checking for errors is:(提取,解码和检查错误的有效方法是:) if (preg_match('/^data:image/(w+);base64,/', $data, $type)) { $data = substr($data, strpos($data, ',') + 1); $type = strtolower($type[1]); // jpg, png, gif if (!in_array($type, [ 'jpg', 'jpeg', 'gif', 'png' ])) { throw new Exception('invalid image type'); } $data = base64_decode($data); if ($data === false) { throw new Exception('base64_decode failed'); } } else { throw new Exception('did not match data URI with image data'); } file_put_contents("img.{$type}", $data);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...