从手机上传后,网络上的图像方向已更改?

时间:2015-07-23 08:54:17

标签: android image-uploading image-upload

我可以通过手机成功上传图片,但当我在笔记本电脑上看到个人资料时,方向会发生变化。

$upload_path = 'uploads/';
    $filename = $_FILES['userfile']['name'];
    $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
    if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
        header("Location: profileOne.php?error=PICTURE");
        //die('The file you attempted to upload is too large.');
    if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');
    if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path .$user_id.".jpg")) {   

        $check=@copy($upload_path .$user_id.'.jpg', '../backend/data/profile_image/'.$user_id.'.jpg');
        if($check){
            $postdata1 = http_build_query(
                array(
                    'user_id' => $user_id,
                    'img'=>'data/profile_image/'.$user_id.'.jpg',
                    'type'=>'web'

                )
            );
            $opts1 = array('http' =>
                array(
                    'method'  => 'POST',
                    'header'  => 'Content-type: application/x-www-form-urlencoded',
                    'content' => $postdata1
                )
            );
            $context1  = stream_context_create($opts1);
            $endPoint1='http://www.ridorama.com/backend/registration/update_profile/';
            $result1 = file_get_contents($endPoint1, false, $context1);

1 个答案:

答案 0 :(得分:0)

我认为问题来自设备而非来自服务器,因为谁拍摄照片是设备,谁应用旋转是设备...然后问题是你没有在图像统计中正确保存旋转和当你尝试获取字节或从位图类传递图像,例如,因为图像在stats =>中没有正确应用旋转。 rotation =0º。

要正确解决此问题,您需要在相机中正确应用旋转,并且当相机拍照时,这个会在照片统计中保存camara旋转,并始终将图像转换为字节或位图类或发送照片到你的服务器,这个尊重照片的旋转统计。请按照以下代码将此代码应用于相机预览类:

public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();

    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    }
    else {  // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }

    Camera.Parameters parameters = camera.getParameters();
    parameters.setRotation(result);
    camera.setParameters(parameters);

    camera.setDisplayOrientation(result);
}

将此代码正确应用于setCameraDisplayOrientation()方法和照片保存方向,当您将图像发送到服务器时,服务器也可以正确定位。

告诉我,如果我帮助你并做好编程!