简体   繁体   English

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

[英]Uploading Image from src of img tag to php

I just want to ask if there is a way to upload an image from src of <img> tag . 我只是想问一下是否有办法从<img>标签的src上传图像。 The src of the img tag was not from the server and look like this one: img标签的src不是来自服务器,看起来像这样:

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

This was the product of the approach I learned from this site. 这是我从这个网站上学到的方法的产物。

Thank you in advance. 先感谢您。

Let me know if you want me to explaine this dipper. 如果你想让我解释这个北斗星,请告诉我。 Thank you 谢谢

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 .'" />';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM