1

我在向服务器发送 base64 图像字符串(嵌入在 json 对象中)时遇到问题。

这是JS代码:

var sMimeType;
var id_g;
var tipo_g;
var lang_g;

function fileLoaded(frEvnt){
    var sFBody = frEvnt.target.result;
    var sBodyBase64 = btoa(sFBody);
    var myobj = {my_id: id_g, my_tipo: tipo_g, my_lang:lang_g, img_mime_type: sMimeType, img_base64 :sBodyBase64};
    //$("#error").html(JSON.stringify(myobj));
    $.ajax({
      url: "up_img.php",
      data: JSON.stringify(myobj),
      type: 'POST',
      success:function(a) {/*window.location = tipo_g+".php?l="+lang_g*/alert(a);},
      error:function() {$("#error").html("error")}
    });

}
function Upload(id,tipo){
    lang = document[id].l.value;
    var oFile = document[id].img.files[0];
    id_g = id;
    tipo_g = tipo;
    lang_g = lang;
    if(oFile){
        var oFReader = new FileReader();
        oFReader.onload = fileLoaded;
        sMimeType = oFile.type;
          oFReader.readAsBinaryString(oFile);
    }

}

这是up_img.php

require_once('dbconn.php');
require_once('../utility/sec.php');


$json = json_decode(file_get_contents("php://input"),true);

$id = "";
$id = $json['my_id'];
$lang = "";
$lang = $json['my_lang'];
$tipo = "";
$tipo = $json['my_tipo'];
$img_mime = "";
$img_mime = $json['img_mime_type'];
$img_base64 = "";
$img_base64 = base64_decode($json['img_base64']);
$event = new event($tipo,$lang);


$event->upload_img($img_base64,$img_mime,$id);

这里是$event->upload_img($img_base64,$img_mime,$id);

public function upload_img($img,$mime,$id){
        if($this->check_img($id)){
            //$img = str_replace('data:image/png;base64,', '', $img);
            switch($mime){
                case "image/png": case "image/jpeg": case "image/jpg": case "image/gif": case "image/tiff":{
                    $img = str_replace(' ', '+', $img);
                    $data = $img;
                    $ext = explode('/',$mime);
                    $file = $this->get_folder_picture() . md5($id) . '.' . $ext[1];
                    $success = file_put_contents($file, $data);
                    if($success)
                    {
                         $newconn = new event($this->getTipo(),$this->getLang());
                         $newconn->open_conn();
                         $query = sprintf("insert into images (`Id`,`Ext`) values (%d,'%s')",
                            $id,$ext[1]);
                         $q = mysql_query($query,$newconn->getLink());
                         echo mysql_error($newconn->getLink());
                         $newconn->close_conn();

                    }else
                        echo 'Unable to save the file.';
                    break;
                }
                default:{
                    echo "not a valid image. image type accepted are jpge,jpg,png,tiff and gif: u gave ".$mime; 
                    break;
                }

            }

        }else{
            print "image error";    
        }
    }

问题是,一旦我可视化存储在服务器中的图像(图像的路径<img src=\"$img\" alt='' title='' class='box_img' name='img_view' />在哪里$img),我就会看到类似这样的东西 在此处输入图像描述

但我的原始图像看起来像这样 在此处输入图像描述

任何人都可以帮助我吗?

4

1 回答 1

4

找到了解决方案!刚刚删除了该行

$img = str_replace(' ', '+', $img);

现在图像看起来一样!!!!

于 2012-10-08T16:48:59.287 回答