douqi2571 2012-10-01 10:31 采纳率: 0%
浏览 72
已采纳

通过JSON将base64'd映像发送到PHP GD库

So I send the base64 image text to the php file with jquery/javascript like this:

//Upload picture
$('#uploadPictureBtnHtml').click(function(){
    OpenLoader();
   var baseEncPicData = $('#chosenPictureData').val();
    $.ajax({
        type:'POST', url:'upload_picture.php',  dataType:"json",  data:{ PicFile: baseEncPicData },
        success:function (upload_pic_data) {
            if (upload_pic_data[0] == 'true') {
                    alert("it worked!");
                    CloseLoader();
            }
            else{
                    //upload_pic_data[1] will return PHP errors
                    alert("Why it didnt work: "+upload_pic_data[1]+" ");
                    CloseLoader();
            }
        }
        });
});

I know this works fine because if I take the baseEncPicData variable and output it like this:

$('#some_div').html('<img src="data:image/jpeg;base64,'+baseEncPicData+'" />');

It works just great and the image shows without an issue.

When I send this to the PHP file, I decode and "reassemble" it like this:

$dataUno = htmlspecialchars(trim(urldecode($_POST['PicFile'])));
$dataDos = base64_decode($dataUno);
$data = imagecreatefromstring($dataDos);
$the_new_png = imagepng($data, $the_directory);

The error I receive is only in PHP which is:

 imagecreatefromstring() [function.imagecreatefromstring]: Empty string or invalid image
 imagepng() expects parameter 1 to be resource

Which leads me to believe that it's not decoding the JSON correctly because there are characters being stripped that are part of the base64'd image. This is just an assumption. Not exactly sure what I'm doing wrong. Any help would be awesome. Thanks for taking your time to read this.

  • 写回答

1条回答 默认 最新

  • douguai4653 2012-10-01 10:55
    关注

    Remove this string:

    htmlspecialchars(trim(urldecode($_POST['PicFile'])));

    ... and process just $_POST['PicFile'] instead. With that preprocessing you turn all + characters (allowed in Base64) into spaces (), effectively broking the binary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建