1

我有一个我想通过 AJAX 上传的画布图像,但是当我 base64_decode

$dataUrl = $data['dataUrl'];
$parts = explode(',', $dataUrl);
$img_data = $parts[1];
$img_data = str_replace(' ','+',$img_data);
$img_data = base64_decode($img_data);
// Save data as an image
$fp = fopen('image.png', 'w');  
fwrite($fp, $img_data);  
fclose($fp); 
die();

如果我回显 img 数据,它是空的,但我确实得到了一个无效的图像,图像的大小为 7-8kB,但它已损坏。

发送到服务器的数据:

 data[dataUrl]:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkYAAAJECAYAAADzFetnAAAbA0lEQVR4Xu3awQnEQBADQTv/pA13+gjERFAOoejHMvL7+AgQIECAAAECBH4CLwcCBAgQIECAAIG/gIeREggQIECAAAECEfAwkgIBAgQIECBAwMNIAwQIECBAgACBFnAxUgQBAgQIECBAwMVIAwQIECBAgAABFyMNECBAgAABAgSmgClNGAQIECBAgAABU5oGCBAgQIAAAQKmNA0QIECAAAECBExpGiBAgAABAgQIXAL+MdIHAQIECBAgQCACHkZSIECAAAECBAh4GGmAAAECBAgQINACLkaKIECAAAECBAi4GGmAAAECBAgQIOBipAECBAgQIECAwBQwpQmDAAECBAgQIGBK0wABAgQIECBAwJSmAQIECBAgQICAKU0DBAgQIECAAIFLwD9G+iBAgAABAgQIRMDDSAoECBAgQIAAAQ8jDRAgQIAAAQIEWsDFSBEECBAgQIAAARcjDRAgQIAAAQIEXIw0QIAAAQIECBCYAqY0YRAgQIAAAQIETGkaIECAAAECBAiY0jRAgAABAgQIEDClaYAAAQIECBAgcAn4x0gfBAgQIECAAIEIeBhJgQABAgQIECDgYaQBAgQIECBAgEALuBgpggABAgQIECDgYqQBAgQIECBAgICLkQYIECBAgAABAlPAlCYMAgQIECBAgIApTQMECBAgQIAAAVOaBggQIECAAAECpjQNECBAgAABAgQuAf8Y6YMAAQIECBAgEAEPIykQIECAAAECBDyMNECAAAECBAgQaAEXI0UQIECAAAECBFyMNECAAAECBAgQcDHSAAECBAgQIEBgCpjShEGAAAECBAgQMKVpgAABAgQIECBgStMAAQIECBAgQMCUpgECBAgQIECAwCXgHyN9ECBAgAABAgQi4GEkBQIECBAgQICAh5EGCBAgQIAAAQIt4GKkCAIECBAgQICAi5EGCBAgQIAAAQIuRhogQIAAAQIECEwBU5owCBAgQIAAAQKmNA0QIECAAAECBExpGiBAgAABAgQImNI0QIAAAQIECBC4BPxjpA8CBAgQIECAQAQ8jKRAgAABAgQIEPAw0gABAgQIECBAoAVcjBRBgAABAgQIEHAx0gABAgQIECBAwMVIAwQIECBAgACBKWBKEwYBAgQIECBAwJSmAQIECBAgQICAKU0DBAgQIECAAAFTmgYIECBAgAABApeAf4z0QYAAAQIECBCIgIeRFAgQIECAAAECHkYaIECAAAECBAi0gIuRIggQIECAAAECLkYaIECAAAECBAi4GGmAAAECBAgQIDAFTGnCIECAAAECBAiY0jRAgAABAgQIEDClaYAAAQIECBAgYErTAAECBAgQIEDgEvCPkT4IECBAgAABAhHwMJICAQIECBAgQMDDSAMECBAgQIAAgRZwMVIEAQIECBAgQMDFSAMECBAgQIAAARcjDRAgQIAAAQIEpoApTRgECBAgQIAAAVOaBggQIECAAAECpjQNECBAgAABAgRMaRogQIAAAQIECFwC/jHSBwECBAgQIEAgAh5GUiBAgAABAgQIeBhpgAABAgQIECDQAi5GiiBAgAABAgQIuBhpgAABAgQIECDgYqQBAgQIECBAgMAUMKUJgwABAgQIECBgStMAAQIECBAgQMCUpgECBAgQIECAgClNAwQIECBAgACBS8A/RvogQIAAAQIECETAw0gKBAgQIECAAAEPIw0QIECAAAECBFrAxUgRBAgQIECAAAEXIw0QIECAAAECBFyMNECAAAECBAgQmAKmNGEQIECAAAECBExpGiBAgAABAgQImNI0QIAAAQIECBAwpWmAAAECBAgQIHAJ+MdIHwQIECBAgACBCHgYSYEAAQIECBAg4GGkAQIECBAgQIBAC7gYKYIAAQIECBAg4GKkAQIECBAgQICAi5EGCBAgQIAAAQJTwJQmDAIECBAgQICAKU0DBAgQIECAAAFTmgYIECBAgAABAqY0DRAgQIAAAQIELgH/GOmDAAECBAgQIBABDyMpECBAgAABAgQ8jDRAgAABAgQIEGgBFyNFECBAgAABAgRcjDRAgAABAgQIEHAx0gABAgQIECBAYAqY0oRBgAABAgQIEDClaYAAAQIECBAgYErTAAECBAgQIEDAlKYBAgQIECBAgMAl4B8jfRAgQIAAAQIEIuBhJAUCBAgQIECAgIeRBggQIECAAAECLeBipAgCBAgQIECAgIuRBggQIECAAAECLkYaIECAAAECBAhMAVOaMAgQIECAAAECpjQNECBAgAABAgRMaRogQIAAAQIECJjSNECAAAECBAgQuAT8Y6QPAgQIECBAgEAEPIykQIAAAQIECBDwMNIAAQIECBAgQKAFXIwUQYAAAQIECBBwMdIAAQIECBAgQMDFSAMECBAgQIAAgSlgShMGAQIECBAgQMCUpgECBAgQIECAgClNAwQIECBAgAABU5oGCBAgQIAAAQKXgH+M9EGAAAECBAgQiICHkRQIECBAgAABAh5GGiBAgAABAgQItICLkSIIECBAgAABAi5GGiBAgAABAgQIuBhpgAABAgQIECAwBUxpwiBAgAABAgQImNI0QIAAAQIECBAwpWmAAAECBAgQIGBK0wABAgQIECBA4BLwj5E+CBAgQIAAAQIR8DCSAgECBAgQIEDAw0gDBAgQIECAAIEWcDFSBAECBAgQIEDAxUgDBAgQIECAAAEXIw0QIECAAAECBKaAKU0YBAgQIECAAAFTmgYIECBAgAABAqY0DRAgQIAAAQIETGkaIECAAAECBAhcAv4x0gcBAgQIECBAIAIeRlIgQIAAAQIECHgYaYAAAQIECBAg0AIuRoogQIAAAQIECLgYaYAAAQIECBAg4GKkAQIECBAgQIDAFDClCYMAAQIECBAgYErTAAECBAgQIEDAlKYBAgQIECBAgIApTQMECBAgQIAAgUvAP0b6IECAAAECBAhEwMNICgQIECBAgAABDyMNECBAgAABAgRawMVIEQQIECBAgAABFyMNECBAgAABAgRcjDRAgAABAgQIEJgCpjRhECBAgAABAgRMaRogQIAAAQIECJjSNECAAAECBAgQMKVpgAABAgQIECBwCfjHSB8ECBAgQIAAgQh4GEmBAAECBAgQIOBhpAECBAgQIECAQAu4GCmCAAECBAgQIOBipAECBAgQIECAgIuRBggQIECAAAECU8CUJgwCBAgQIECAgClNAwQIECBAgAABU5oGCBAgQIAAAQKmNA0QIECAAAECBC4B/xjpgwABAgQIECAQAQ8jKRAgQIAAAQIEPIw0QIAAAQIECBBoARcjRRAgQIAAAQIEXIw0QIAAAQIECBBwMdIAAQIECBAgQGAKmNKEQYAAAQIECBAwpWmAAAECBAgQIGBK0wABAgQIECBAwJSmAQIECBAgQIDAJeAfI30QIECAAAECBCLgYSQFAgQIECBAgICHkQYIECBAgAABAi3gYqQIAgQIECBAgICLkQYIECBAgAABAi5GGiBAgAABAgQITAFTmjAIECBAgAABAqY0DRAgQIAAAQIETGkaIECAAAECBAiY0jRAgAABAgQIELgE/GOkDwIECBAgQIBABDyMpECAAAECBAgQ8DDSAAECBAgQIECgBVyMFEGAAAECBAgQcDHSAAECBAgQIEDAxUgDBAgQIECAAIEpYEoTBgECBAgQIEDAlKYBAgQIECBAgIApTQMECBAgQIAAAVOaBggQIECAAAECl4B/jPRBgAABAgQIEIiAh5EUCBAgQIAAAQIeRhogQIAAAQIECLSAi5EiCBAgQIAAAQIuRhogQIAAAQIECLgYaYAAAQIECBAgMAVMacIgQIAAAQIECJjSNECAAAECBAgQMKVpgAABAgQIECBgStMAAQIECBAgQOAS8I+RPggQIECAAAECEfAwkgIBAgQIECBAwMNIAwQIECBAgACBFnAxUgQBAgQIECBAwMVIAwQIECBAgAABFyMNECBAgAABAgSmgClNGAQIECBAgAABU5oGCBAgQIAAAQKmNA0QIECAAAECBExpGiBAgAABAgQIXAL+MdIHAQIECBAgQCACHkZSIECAAAECBAh4GGmAAAECBAgQINACLkaKIECAAAECBAi4GGmAAAECBAgQIOBipAECBAgQIECAwBQwpQmDAAECBAgQIGBK0wABAgQIECBAwJSmAQIECBAgQICAKU0DBAgQIECAAIFLwD9G+iBAgAABAgQIRMDDSAoECBAgQIAAAQ8jDRAgQIAAAQIEWsDFSBEECBAgQIAAARcjDRAgQIAAAQIEXIw0QIAAAQIECBCYAqY0YRAgQIAAAQIETGkaIECAAAECBAiY0jRAgAABAgQIEDClaYAAAQIECBAgcAn4x0gfBAgQIECAAIEIeBhJgQABAgQIECDgYaQBAgQIECBAgEALuBgpggABAgQIECDgYqQBAgQIECBAgICLkQYIECBAgAABAlPAlCYMAgQIECBAgIApTQMECBAgQIAAAVOaBggQIECAAAECpjQNECBAgAABAgQuAf8Y6YMAAQIECBAgEAEPIykQIECAAAECBDyMNECAAAECBAgQaAEXI0UQIECAAAECBFyMNECAAAECBAgQcDHSAAECBAgQIEBgCpjShEGAAAECBAgQMKVpgAABAgQIECBgStMAAQIECBAgQMCUpgECBAgQIECAwCXgHyN9ECBAgAABAgQi4GEkBQIECBAgQICAh5EGCBAgQIAAAQIt4GKkCAIECBAgQICAi5EGCBAgQIAAAQIuRhogQIAAAQIECEwBU5owCBAgQIAAAQKmNA0QIECAAAECBExpGiBAgAABAgQImNI0QIAAAQIECBC4BPxjpA8CBAgQIECAQAQ8jKRAgAABAgQIEPAw0gABAgQIECBAoAVcjBRBgAABAgQIEHAx0gABAgQIECBAwMVIAwQIECBAgACBKWBKEwYBAgQIECBAwJSmAQIECBAgQICAKU0DBAgQIECAAAFTmgYIECBAgAABApeAf4z0QYAAAQIECBCIgIeRFAgQIECAAAECHkYaIECAAAECBAi0gIuRIggQIECAAAECLkYaIECAAAECBAi4GGmAAAECBAgQIDAFTGnCIECAAAECBAiY0jRAgAABAgQIEDClaYAAAQIECBAgYErTAAECBAgQIEDgEvCPkT4IECBAgAABAhHwMJICAQIECBAgQMDDSAMECBAgQIAAgRZwMVIEAQIECBAgQMDFSAMECBAgQIAAARcjDRAgQIAAAQIEpoApTRgECBAgQIAAAVOaBggQIECAAAECpjQNECBAgAABAgRMaRogQIAAAQIECFwC/jHSBwECBAgQIEAgAh5GUiBAgAABAgQIeBhpgAABAgQIECDQAi5GiiBAgAABAgQIuBhpgAABAgQIECDgYqQBAgQIECBAgMAUMKUJgwABAgQIECBgStMAAQIECBAgQMCUpgECBAgQIECAgClNAwQIECBAgACBS8A/RvogQIAAAQIECETAw0gKBAgQIECAAAEPIw0QIECAAAECBFrAxUgRBAgQIECAAAEXIw0QIECAAAECBFyMNECAAAECBAgQmAKmNGEQIECAAAECBExpGiBAgAABAgQImNI0QIAAAQIECBAwpWmAAAECBAgQIHAJ+MdIHwQIECBAgACBCHgYSYEAAQIECBAg4GGkAQIECBAgQIBAC7gYKYIAAQIECBAg4GKkAQIECBAgQICAi5EGCBAgQIAAAQJTwJQmDAIECBAgQICAKU0DBAgQIECAAAFTmgYIECBAgAABAqY0DRAgQIAAAQIELgH/GOmDAAECBAgQIBABDyMpECBAgAABAgQ8jDRAgAABAgQIEGgBFyNFECBAgAABAgRcjDRAgAABAgQIEHAx0gABAgQIECBAYAqY0oRBgAABAgQIEDClaYAAAQIECBAgYErTAAECBAgQIEDAlKYBAgQIECBAgMAl4B8jfRAgQIAAAQIEIuBhJAUCBAgQIECAgIeRBggQIECAAAECLeBipAgCBAgQIECAgIuRBggQIECAAAECLkYaIECAAAECBAhMAVOaMAgQIECAAAECpjQNECBAgAABAgRMaRogQIAAAQIECJjSNECAAAECBAgQuAT8Y6QPAgQIECBAgEAEPIykQIAAAQIECBDwMNIAAQIECBAgQKAFXIwUQYAAAQIECBBwMdIAAQIECBAgQMDFSAMECBAgQIAAgSlgShMGAQIECBAgQMCUpgECBAgQIECAgClNAwQIECBAgAABU5oGCBAgQIAAAQKXgH+M9EGAAAECBAgQiICHkRQIECBAgAABAh5GGiBAgAABAgQItICLkSIIECBAgAABAi5GGiBAgAABAgQIuBhpgAABAgQIECAwBUxpwiBAgAABAgQImNI0QIAAAQIECBAwpWmAAAECBAgQIGBK0wABAgQIECBA4BLwj5E+CBAgQIAAAQIR8DCSAgECBAgQIEDAw0gDBAgQIECAAIEWcDFSBAECBAgQIEDAxUgDBAgQIECAAAEXIw0QIECAAAECBKaAKU0YBAgQIECAAAFTmgYIECBAgAABAqY0DRAgQIAAAQIETGkaIECAAAECBAhcAv4x0gcBAgQIECBAIAIeRlIgQIAAAQIECHgYaYAAAQIECBAg0AIuRoogQIAAAQIECLgYaYAAAQIECBAg4GKkAQIECBAgQIDAFDClCYMAAQIECBAgYErTAAECBAgQIEDAlKYBAgQIECBAgIApTQMECBAgQIAAgUvAP0b6IECAAAECBAhEwMNICgQIECBAgAABDyMNECBAgAABAgRawMVIEQQIECBAgAABFyMNECBAgAABAgRcjDRAgAABAgQIEJgCpjRhECBAgAABAgRMaRogQIAAAQIECJjSNECAAAECBAgQMKVpgAABAgQIECBwCfjHSB8ECBAgQIAAgQh4GEmBAAECBAgQIOBhpAECBAgQIECAQAu4GCmCAAECBAgQIOBipAECBAgQIECAgIuRBggQIECAAAECU8CUJgwCBAgQIECAgClNAwQIECBAgAABU5oGCBAgQIAAAQKmNA0QIECAAAECBC4B/xjpgwABAgQIECAQAQ8jKRAgQIAAAQIEPIw0QIAAAQIECBBoARcjRRAgQIAAAQIEXIw0QIAAAQIECBBwMdIAAQIECBAgQGAKmNKEQYAAAQIECBAwpWmAAAECBAgQIGBK0wABAgQIECBAwJSmAQIECBAgQIDAJeAfI30QIECAAAECBCLgYSQFAgQIECBAgICHkQYIECBAgAABAi3gYqQIAgQIECBAgICLkQYIECBAgAABAi5GGiBAgAABAgQITAFTmjAIECBAgAABAqY0DRAgQIAAAQIETGkaIECAAAECBAiY0jRAgAABAgQIELgE/GOkDwIECBAgQIBABDyMpECAAAECBAgQ8DDSAAECBAgQIECgBVyMFEGAAAECBAgQcDHSAAECBAgQIEDAxUgDBAgQIECAAIEpYEoTBgECBAgQIEDAlKYBAgQIECBAgIApTQMECBAgQIAAAVOaBggQIECAAAECl4B/jPRBgAABAgQIEIiAh5EUCBAgQIAAAQIeRhogQIAAAQIECLSAi5EiCBAgQIAAAQIuRhogQIAAAQIECLgYaYAAAQIECBAgMAVMacIgQIAAAQIECJjSNECAAAECBAgQMKVpgAABAgQIECBgStMAAQIECBAgQOAS8I+RPggQIECAAAECEfAwkgIBAgQIECBAwMNIAwQIECBAgACBFnAxUgQBAgQIECBAwMVIAwQIECBAgAABFyMNECBAgAABAgSmgClNGAQIECBAgAABU5oGCBAgQIAAAQKmNA0QIECAAAECBExpGiBAgAABAgQIXAL+MdIHAQIECBAgQCACHkZSIECAAAECBAh4GGmAAAECBAgQINACLkaKIECAAAECBAi4GGmAAAECBAgQIOBipAECBAgQIECAwBQwpQmDAAECBAgQIGBK0wABAgQIECBAwJSmAQIECBAgQICAKU0DBAgQIECAAIFLwD9G+iBAgAABAgQIRMDDSAoECBAgQIAAAQ8jDRAgQIAAAQIEWsDFSBEECBAgQIAAARcjDRAgQIAAAQIEXIw0QIAAAQIECBCYAqY0YRAgQIAAAQIETGkaIECAAAECBAiY0jRAgAABAgQIEDClaYAAAQIECBAgcAn4x0gfBAgQIECAAIEIeBhJgQABAgQIECDgYaQBAgQIECBAgEALuBgpggABAgQIECDgYqQBAgQIECBAgICLkQYIECBAgAABAlPAlCYMAgQIECBAgIApTQMECBAgQIAAAVOaBggQIECAAAECpjQNECBAgAABAgQuAf8Y6YMAAQIECBAgEAEPIykQIECAAAECBDyMNECAAAECBAgQaAEXI0UQIECAAAECBFyMNECAAAECBAgQcDHSAAECBAgQIEBgCpjShEGAAAECBAgQMKVpgAABAgQIECBgStMAAQIECBAgQMCUpgECBAgQIECAwCXgHyN9ECBAgAABAgQi4GEkBQIECBAgQICAh5EGCBAgQIAAAQIt4GKkCAIECBAgQICAi5EGCBAgQIAAAQIuRhogQIAAAQIECEwBU5owCBAgQIAAAQKmNA0QIECAAAECBExpGiBAgAABAgQImNI0QIAAAQIECBC4BPxjpA8CBAgQIECAQAQ8jKRAgAABAgQIEPAw0gABAgQIECBAoAVcjBRBgAABAgQIEHAx0gABAgQIECBAwMVIAwQIECBAgACBKWBKEwYBAgQIECBAwJSmAQIECBAgQICAKU0DBAgQIECAAAFTmgYIECBAgAABApeAf4z0QYAAAQIECBCIgIeRFAgQIECAAAECHkYaIECAAAECBAi0gIuRIggQIECAAAECLkYaIECAAAECBAi4GGmAAAECBAgQIDAFTGnCIECAAAECBAiY0jRAgAABAgQIEDClaYAAAQIECBAgYErTAAECBAgQIEDgEvCPkT4IECBAgAABAhHwMJICAQIECBAgQMDDSAMECBAgQIAAgRZwMVIEAQIECBAgQMDFSAMECBAgQIAAARcjDRAgQIAAAQIEpoApTRgECBAgQIAAAVOaBggQIECAAAECpjQNECBAgAABAgRMaRogQIAAAQIECFwC/jHSBwECBAgQIEAgAh5GUiBAgAABAgQIeBhpgAABAgQIECDQAi5GiiBAgAABAgQIuBhpgAABAgQIECDgYqQBAgQIECBAgMAUMKUJgwABAgQIECBgStMAAQIECBAgQKAFPrzVAkUViKyYAAAAAElFTkSuQmCC

问题是什么?

4

1 回答 1

0

我很确定您不允许用“+”替换所有空格!而且我还认为(取决于您的配置)您可能会在“获取”或“发布”数据上使用 stripslashes() 。此外,explode() 是将字符串分成两部分的错误方法:-)

我的示例代码将是这样的:

$dataUrl = $data['dataUrl'];

$dataUrl = stripslashes($dataUrl); //-- not sure!

$pos = strpos($dataUrl, ',');
if ($pos<0) return;
$base_data = substr($dataUrl, $pos+1);

$img_data = base64_decode($base_data);
于 2013-10-22T11:50:10.190 回答