javascript 将图像从 img 标签的 src 上传到 php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19047841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:10:12  来源:igfitidea点击:

Uploading Image from src of img tag to php

javascriptphp

提问by Jenerson Jose

I just want to ask if there is a way to upload an image from srcof <img>tag . The srcof the imgtag was not from the server and look like this one:

我只想问,如果有从上传图片的方式src<img>标签。在src对的img从服务器看起来就像这样一个标签是不是:

<img src="data:image/png;base64,iVBOR...52Eysdsadsa===" />

This was the product of the approach I learned from thissite.

这是我从这个网站学到的方法的产物。

Thank you in advance.

先感谢您。

Let me know if you want me to explaine this dipper. Thank you

如果你想让我解释这个北斗七星,请告诉我。谢谢

采纳答案by Sunil Manheri

You can upload base64 encoded string(src part) to the server and save it as it is or convert it into an image and save it as a file.

您可以将 base64 编码的字符串(src 部分)上传到服务器并按原样保存或将其转换为图像并保存为文件。

Uploading base64 encoded image:

上传base64编码的图像:

Get the src attribute of the image and post it to the server.

获取图像的 src 属性并将其发布到服务器。

var base64image = $('#blah').attr('src');

Converting base64 encoded string to image

将base64编码的字符串转换为图像

$img = $_POST['base64image'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = 'image.png';
$success = file_put_contents($file, $data);

Or simply save the uploaded content into your database and later use the below code to render the image on your page:

或者简单地将上传的内容保存到您的数据库中,然后使用以下代码在您的页面上呈现图像:

echo '<img src="'. $img .'" />';