0
我想發佈一個base64圖像文件中,通過jQuery的AJAX PHP腳本..發佈的base64圖像到PHP
我已經與jQuery的encodeURIComponent方法()函數編碼,然後將其與rawurldecode解碼()函數PHP的數據發佈時。
問題是,Php無法將正確的圖像保存到PNG。
我比較了發佈的base64數據和原始數據,並且它們不匹配。這導致了我的結論,也許base64數據太大而無法發佈。
我的結論是否屬實?如果是這樣,是否有其他方式可以成功發佈base64數據? 如果我錯了,你可以請賜我嗎?
這是jQuery腳本
var whereSignatureis = $('.signatureView');
// i got the image from a plugin called jSignature, assign it to a variable data.
var data = signPaper.jSignature("getData","image");
// created an image ddata from what the data returned for displaying purposes.
var ddata = new Image();
// the ddata.src is now the png base64 image
ddata.src = "data:" + data[0] + "," + data[1];
// I encoded the image with uricomponent for safe posting.
var theImg = encodeURIComponent(ddata.src);
// and posted the image
$.ajax({
type: "POST",
url: "processes.php",
data: {img: theImg},
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
success: function(r){
whereSignatureis.append(r);
}
});
這是PHP
define('UPLOAD_DIR', '/images');
$img = rawurldecode($post->img);
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . 'signature' . '.png';
$success = file_put_contents($file, $data);
print $success ? "{$post->img}" : 'File not Saved.';
在此先感謝!
url編碼!= base64 – Maerlyn
請發表一些代碼,你如何處理javascript編碼,發帖和php解碼 – DevZer0
PHP在填寫'$ _GET'或'$ _POST'之前自動解碼查詢字符串,你不需要自己調用'rawurldecode()'。 – Barmar