[英]How to resize an image from android via php before saving to server
我已經獲得了從android獲取圖像並將其保存到服務器的代碼,但是我需要將圖像調整為標准尺寸。 我環顧四周,無法使任何東西正常工作。 下面是到目前為止的代碼。
<?php
// Get image string
$base = $_REQUEST['image'];
// Get file name
$filename = $_REQUEST['filename'];
// Decode Image
$binary = base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
// Images will be saved
$file = fopen('/home1/skyrealm/public_html/img/'.$filename.'.jpg', 'wb');
if($file != true)
{
echo'Error uploading image';
}
else
{
// Create/Svae File
fwrite($file, $binary);
fclose($file);
echo 'Image upload successful';
}
?>
編輯:
這是android端的代碼:
public void encodeImagetoString() {
new AsyncTask<Void, Void, String>() {
protected void onPreExecute() {
};
@Override
protected String doInBackground(Void... params) {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
bitmap = BitmapFactory.decodeFile(imgPath, options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
return "";
}
@Override
protected void onPostExecute(String msg) {
// Put converted Image string into Async Http Post param
params.put("image", encodedString);
// Trigger Image upload
triggerImageUpload();
}
}.execute(null, null, null);
}
如評論中所述,您不需要header('Content-Type: bitmap; charset=utf-8');
如果您不了解圖像本身。
關於調整大小,腳本差不多可以使用Imagemagic或GD庫 。
有關調整大小的一些文章:
PHP-調整大小並保存圖像?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.