Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php中base64图像字符串的解码_Php_Android_Json_Image - Fatal编程技术网

Php中base64图像字符串的解码

Php中base64图像字符串的解码,php,android,json,image,Php,Android,Json,Image,我正在构建一个Android应用程序,当注册用户时,必须提供他们的信息,其中包括他们的个人资料图片,然后应用程序将图像编码为Base64字符串,并将其与提供的所有其他信息一起添加到数组列表中,我的问题是将服务器端的Base64字符串解码为图像,并将图像名称保存到数据库中,如果用户没有上传图像,则应设置默认图像,我尝试了几种方法,但都失败了,如果我的JSON帖子中没有包含Base64图像字符串,那么应用程序运行平稳,这意味着我在解码图像和设置图像时弄糟了,如果我在浏览器中运行整个Php代码,它工作

我正在构建一个Android应用程序,当注册用户时,必须提供他们的信息,其中包括他们的个人资料图片,然后应用程序将图像编码为Base64字符串,并将其与提供的所有其他信息一起添加到数组列表中,我的问题是将服务器端的Base64字符串解码为图像,并将图像名称保存到数据库中,如果用户没有上传图像,则应设置默认图像,我尝试了几种方法,但都失败了,如果我的JSON帖子中没有包含Base64图像字符串,那么应用程序运行平稳,这意味着我在解码图像和设置图像时弄糟了,如果我在浏览器中运行整个Php代码,它工作得很好,那么我就卡住了,手机哪里出了问题

如果提供了图像,它将执行此代码

  if (imgPath !=null)

        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            final Bitmap bitmapOrg = BitmapFactory.decodeFile(imgPath, options);
           ByteArrayOutputStream bao = new ByteArrayOutputStream();
            bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
            byte [] ba = bao.toByteArray();
            String profile_pic= Base64.encodeToString(ba, 0);


            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));
                params.add(new BasicNameValuePair("fname", fname));
                params.add(new BasicNameValuePair("lname", lname));
                params.add(new BasicNameValuePair("email", email));
                params.add(new BasicNameValuePair("city", city));
                params.add(new BasicNameValuePair("nrc", nrc));
                params.add(new BasicNameValuePair("dob", dob));
                params.add(new BasicNameValuePair("cell", cell));
                params.add(new BasicNameValuePair("profile", profile_pic));

我想说的是,问题是,您使用的功能,如is_upload_file和move_upload_file,只有在使用标准文件上载机制时才相关。但是,您只是将图像作为base64编码的变量传递给普通的POST字段名和变量机制。所以$_POST['profile']只是一个普通字段,您需要使用base64decode将其转换回图像。然后,您应该像普通文件一样将其写入磁盘。谢谢,看看我当前的方法$binary=base64\u decode$image;,我如何实现这一点$二进制文件包含图像字节。所以,只要将它们保存到文件中,您就完成了。使用文件内容。
  try {

                String profile_pic = null;
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));
                params.add(new BasicNameValuePair("fname", fname));
                params.add(new BasicNameValuePair("lname", lname));
                params.add(new BasicNameValuePair("email", email));
                params.add(new BasicNameValuePair("city", city));
                params.add(new BasicNameValuePair("nrc", nrc));
                params.add(new BasicNameValuePair("dob", dob));
                params.add(new BasicNameValuePair("cell", cell));
                params.add(new BasicNameValuePair("profile", profile_pic));
if (!empty($_POST)) {

    if(!empty($_POST['profile']) || $_POST['profile'] == null)
    {
       $image = $_POST['profile'];
        $binary=base64_decode($image);
        $source = imagecreatefromstring($binary);       
    }

    if (empty($_POST['username']) || empty($_POST['password']))   
    {

        // Create some data that will be the JSON response 
        $response["success"] = 0;
        $response["message"] = "Please Make Sure All Fields Are filled";
        die(json_encode($response));
    }

    $query        = " SELECT 1 FROM users WHERE username = :user";
    //now lets update what :user should be
    $query_params = array(
        ':user' => $_POST['username']
    );


    try {
        // These two statements run the query against your database table. 
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {

        $response["success"] = 0;
        $response["message"] = "Database Error1. Please Try Again!";
        die(json_encode($response));
    }


    $row = $stmt->fetch();
    if ($row) {

        $response["success"] = 0;
        $response["message"] = "I'm sorry, this username is already in use";
        die(json_encode($response));
    }

    $Destination = 'avatars';
    if(!isset($_POST['profile']) || 
       !is_uploaded_file($_POST['profile']['tmp_name']) || 
       $_POST['profile'] == null )
    {
        $NewImageName= 'default.jpg';
        move_uploaded_file($_POST['profile']['tmp_name'], "$Destination/$NewImageName");
    }else{
        $RandomNum   = rand(0, 9999999999);
        $ImageName = str_replace(' ','-',strtolower($_POST['profile']['name']));
        $ImageType = $_POST['profile']['type'];
        $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
        $ImageExt = str_replace('.','',$ImageExt);
        $ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
        $NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
        move_uploaded_file($_POST['profile']['tmp_name'],   "$Destination/$NewImageName");
    }

    $sql5="UPDATE users SET propic='$NewImageName' WHERE username = ':username'";
    $sql6="INSERT INTO users (propic) VALUES ('$NewImageName') WHERE username = ':username'";

    $query = "INSERT INTO users ( username, 
                                  fname, 
                                  lname,
                                password,
                                  email,
                                  city,
                                  dob,
                                  nrc,
                                  cell,
                                  propic) 
                          VALUES ( :user,
                                   :fnam,
                                   :lnam,
                                   :pass, 
                                   :emai,
                                   :cit,
                                   :do, 
                                   :nr,  
                                   :cel,
                                   :NewImageName )";

    //Again, we need to update our tokens with the actual data:
    $query_params = array(
        ':user' => $_POST['username'],
        ':fnam' => $_POST['fname'],
        ':lnam' => $_POST['lname'] ,
        ':pass' => md5($_POST['password']),
        ':emai' => $_POST['email'],
        ':cit'  => $_POST['city'],
        ':do'   => $_POST['dob'],
        ':nr'   => $_POST['nrc'],
        ':cel' => $_POST['cell'],
        ':NewImageName' =>$NewImageName);



    //time to run our query, and create the user
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Database Error2. Please Try Again!";
        die(json_encode($response));
    }


    $response["success"] = 1;
    $response["message"] = "Username Successfully Added!";
    echo json_encode($response);