我已经获得了从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);
}
答案 0 :(得分:1)
如评论中所述,如果您没有提供图像本身,则不需要header('Content-Type: bitmap; charset=utf-8');
。
关于调整脚本的大小调整或多或少可以使用Imagemagic或GD library。
关于调整大小的一些帖子:
php - resize and save an image?