PHP 数据-URI 到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6735414/
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
PHP Data-URI to file
提问by GAgnew
I have a data URI I am getting from javascript and trying to save via php. I use the following code which gives a apparently corrupt image file:
我有一个从 javascript 获取并尝试通过 php 保存的数据 URI。我使用以下代码提供了一个明显损坏的图像文件:
$data = $_POST['logoImage'];
$uri = substr($data,strpos($data,",")+1);
file_put_contents($_POST['logoFilename'], base64_decode($uri));
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs 9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAxklEQVQYlYWQMW7CUBBE33yITYUUmwbOkBtEcgUlTa7COXIVV5RUkXKC5AxU EdyZVD4kyKxkwIrr9vd0c7Oih aopinLNsF6Qkg2XW4XJ7LGFsAAcTV6lF5/jLdbALA9XDAXYfthFQVx OrmqKYK88/7rbbMFksALieTnzu9wDYTj6f70PKsp2kwAiSvjXNcvkWpAfNZkzWa/5a9yT7fdoX7rrB7hYh2fXo9HdjPYQZu3MIU8bYIlW20y0RUlXG2Kpv/vfwLxhTaSQwWqwhAAAAAElFTkSuQmCC
Below the code is the actual image as a Data-URI. 'logoImage' is the string above, and $uri is the string minus 'image/jpeg;base64,'.
代码下方是作为数据 URI 的实际图像。'logoImage' 是上面的字符串,$uri 是减去 'image/jpeg;base64,' 的字符串。
回答by Paul S.
A quick look at the PHP manualyields the following:
快速浏览 PHP 手册会得到以下结果:
If you want to save data that is derived from a Javascript canvas.toDataURL() function, you have to convert blanks into plusses. If you do not do that, the decoded data is corrupted:
<?php $encodedData = str_replace(' ','+',$encodedData); $decodedData = base64_decode($encodedData); ?>
如果要保存从 Javascript canvas.toDataURL() 函数派生的数据,则必须将空格转换为加号。如果你不这样做,解码的数据就会损坏:
<?php $encodedData = str_replace(' ','+',$encodedData); $decodedData = base64_decode($encodedData); ?>
回答by hakre
The data URI you have in your example is not a valid PNG image. This will never work and is unrelated to the code, it's related to the data.
您的示例中的数据 URI 不是有效的 PNG 图像。这永远不会工作并且与代码无关,它与数据有关。
Does not apply but might be of interest:
不适用但可能感兴趣:
file_put_contents($_POST['logoFilename'], file_get_contents($data));
The idea behind: PHP itself can read the contents of data URIs (data://
)so you don't need to decode it on your own.
背后的想法:PHP 本身可以读取数据 URI ( data://
)的内容,因此您不需要自己对其进行解码。
Note that the official data URI scheme (ref: The "data" URL scheme RFC 2397) does not include a double slash ("//
") after the colon (":
"). PHP supports with or without the two slashes.
请注意,官方数据 URI 方案(参考:“数据”URL 方案 RFC 2397)//
在冒号 (" :
")后不包含双斜杠(" ")。PHP 支持带或不带两个斜杠。
# RFC 2397 conform
$binary = file_get_contents($uri);
# with two slashes
$uriPhp = 'data://' . substr($uri, 5);
$binary = file_get_contents($uriPhp);
回答by Christophe
The all code that works :
所有有效的代码:
$imgData = str_replace(' ','+',$_POST['image']);
$imgData = substr($imgData,strpos($imgData,",")+1);
$imgData = base64_decode($imgData);
// Path where the image is going to be saved
$filePath = $_SERVER['DOCUMENT_ROOT']. '/ima/temp2.png';
// Write $imgData into the image file
$file = fopen($filePath, 'w');
fwrite($file, $imgData);
fclose($file);