個人中心模組-拍照剪裁上傳,模組拍照剪裁上傳

來源:互聯網
上載者:User

個人中心模組-拍照剪裁上傳,模組拍照剪裁上傳

現在多數的網路應用都有個人中心的模組,裡面少不了使用者上傳映像功能,正好最近項目裡用到了這個就從網上找了一個整合進來,寫得挺好的代碼給大家推廣一下。
廢話不多說了,代碼已經上傳到網盤,有需要的朋友可以去下載來看看,這裡處理一下上傳的問題。

使用AsyncTask非同步上傳

private class RegHeadAsyncTask extends AsyncTask<String, Void, String> {    protected void onPreExecute() {    }    protected BaseResponse doInBackground(String... params) {        List<NameValuePair> postParams = new ArrayList<NameValuePair>();        postParams.add(new BasicNameValuePair("uid", uid);        postParams.add(new BasicNameValuePair("image", image));        String response = null;        try {            String result = HttpClientUtil.uploadImg(HttpConfig.url_getHead(), postParams);            if (TextUtils.isEmpty(result))                return null;            Gson gson = new Gson();            // 解析上傳映像操作,伺服器返回的資訊            ...        } catch (Exception e) {            e.printStackTrace();        }        return response;    }    protected void onPostExecute(String result) {        if (null != result) {            // 伺服器返回的資訊            ...            if (flag) { // 上傳成功                ImageLoader.getInstance().displayImage(imageUrl, imageView); // 上傳成功更新ui            }        }    }}

使用HTTP的POST方式提交(HttpClientUtil類)

public static String uploadImg(String url, List<NameValuePair> nameValuePairs) {    BasicHttpParams httpParams = new BasicHttpParams();    HttpConnectionParams.setConnectionTimeout(httpParams, 10 * 1000);    HttpConnectionParams.setSoTimeout(httpParams, 10 * 1000);    HttpClient httpClient = new DefaultHttpClient(httpParams);    HttpPost httpPost = new HttpPost(url);    String result = "";    try {        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);        for (int index = 0; index < nameValuePairs.size(); index++) {            if (nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {                entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File(nameValuePairs.get(index).getValue()), "image/*"));            } else {                entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));            }        }        httpPost.setEntity(entity);        HttpResponse response = httpClient.execute(httpPost);        if (response.getStatusLine().getStatusCode() == 200) {            result = EntityUtils.toString(response.getEntity());            return result;        }    } catch (IOException e) {        e.printStackTrace();    }    return null;}

圖片上傳PHP服務端(ThinkPhp)

// 映像上傳public function upimg() {     $uptypes = array(    'image/jpg',    'image/jpeg',    'image/png',    'imagepeg',    'image/gif',    'image/bmp',    'image/x-png'    );     $user_id=$_POST['uid'];    $file = $_FILES["image"];    $fname = $_FILES["image"]["name"];    $fname_array = explode('.',$fname);    $extend = $fname_array[count($fname_array)-1];    $MAX_FILE_SIZE = 1024000;    $dest_folder = "./Uploads/avatar/";;    if($extend!=""){    if(!in_array($file["type"],$uptypes)){        jsonerror("只能上傳圖片檔案");    }    if($file["size"]>$MAX_FILE_SIZE){        jsonerror("頭像大小不能超過1M");    }    $uploadfile = $dest_folder.$user_id.'.'.jpg;    if(move_uploaded_file($_FILES["image"]["tmp_name"],$uploadfile)){        $user_info=M('Users');        $data['HeadImg']=$uploadfile;        $result=$user_info->where("Uid=$user_id")->save($data);        if($result === FALSE){            jsonerror("頭像修改失敗");        }else{            jsonsuccess("頭像修改成功");        }    }else{        jsonerror("圖片上傳失敗");    }} // 處理頭像上傳資料public function upimgs() {     // 判斷是否上傳會員卡    if(!empty($_FILES['image']['name'])){        $target_path  = "./Uploads/avatar/";//接收檔案目錄        $filename     = $_FILES['image']['name'];  // 檔案名稱        $exename      = $this->getExeName($filename); // 擷取副檔名        $filename     = $_POST['app_uid'].'.'.jpg;  // 將檔案重新命名        $target_path = $target_path.$filename;                if(move_uploaded_file($_FILES['image']['tmp_name'],$target_path)){            $user_id=$_POST['app_uid'];            $user_info=M('Users');            $data['HeadImg']=$filename;            $result=$user_info->where("Uid=$user_id")->save($data);            if($result === FALSE){                 jsonerror("頭像修改失敗");            }else{                jsonsuccess("頭像修改成功!");            }        }else{             jsonerror("上傳圖片有錯誤,請再試一次");        }    }}/*** 擷取副檔名*/public function getExeName($file){    $arrName = explode('.',$file);    return end($arrName);}

附上拍照剪裁demo http://pan.baidu.com/s/1hqIfdS4
右側帶字母的連絡人 http://pan.baidu.com/s/1tUKmM

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.