在Php

时间:2015-04-25 18:30:51

标签: php android json image

我正在构建一个Android应用程序,当注册用户必须提供他们的信息,其中包括他们的个人资料图片时,应用程序然后将图像编码为Base64字符串并将其与其他所有其他信息一起添加到数组列表中提供,我的问题是将服务器端的Base64字符串解码为Image并将图像名称保存到数据库中,如果用户没有上传图像应该设置默认图像,我尝试了几种方法但是都失败了,如果我在我的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));

如果没有提供图像,则执行以下代码:

  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);

0 个答案:

没有答案