private function getOrientation($filename) {
        $exif = @exif_read_data($filename);
        if($exif === null)
            return 0;
        $rotation = 0;
        if (!empty($exif['Orientation'])) {
            switch ($exif['Orientation']) {
                case 3:
                    $rotation = 180;
                    break;
                case 6:
                    $rotation = 90;
                    break;
                case 8:
                    $rotation = -90;
                    break;
            }
        }
        return $rotation;
    }