[英]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.