Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resized images rotate #14

Open
benicelabs opened this issue Feb 5, 2019 · 4 comments
Open

Resized images rotate #14

benicelabs opened this issue Feb 5, 2019 · 4 comments

Comments

@benicelabs
Copy link

Resized images uploaded seem to rotate when they are resized. It seems to rotate image taken with the camera directly uploaded from a WebView 90 CCW. Images uploaded from the camera roll seem to rotate 180.

return try req.content.decode(UserFile.self).map(to: Response.self) { data in
            // create an array of the file types we're willing to accept
            let acceptableTypes = [MediaType.png, MediaType.jpeg]

            for file in data.upload {
                // ensure this image is one of the valid types
                guard let mimeType = file.contentType else { continue }
                guard acceptableTypes.contains(mimeType) else { continue }
                
                // replace any spaces in filenames with a dash
               /let cleanedFilename = file.filename.replacingOccurrences(of: " ", with: "-")
                
                // convert that into a URL we can write to
                let newURL = originalsDirectory.appendingPathComponent(cleanedFilename)
                
                // write the full-size original image
                _ = try? file.data.write(to: newURL)
                
                // create a matching URL in the thumbnails directory
                let thumbURL = thumbsDirectory.appendingPathComponent(cleanedFilename)
                
                // attempt to load the original into a SwiftGD image
                if let image = Image(url: newURL) {
                    // attempt to resize that down to a thumbnail
                    if let resized = image.resizedTo(width: 300) {
                        
                        // it worked – save it!
                        resized.write(to: thumbURL)
                        
                        var tags = [String]()
                        if data.tag_line != nil {
                            tags.append(data.tag_line!)
                        }
                        
                        let p = //Create a Photo object to save
                        
                        p.save(on: req)
                     
                    }
                }
            }
            
            return req.redirect(to: "upload")
        }
@benicelabs
Copy link
Author

Follow up. It appears that my issue was with using a WKWebView. Seems the flag for orientation gets messed up some how. Just switched to using Alamofire for the multipart upload and things seem to be just fine.

@adirburke
Copy link

I have the same exact issue, the full size image is the correct direction, but resized one are not

@tomieq
Copy link

tomieq commented Oct 4, 2024

In 2024 I'm having exactly the same issue

@adirburke
Copy link

In 2024 I'm having exactly the same issue

I fixed this by using another Package along side to get the image data and rotate is again after resizing

SwiftExif

enum ImageOrientation : Int {
    case normal = 1 //= 0 degrees: the correct orientation, no adjustment is required.
    case normalMirrored = 2 //= 0 degrees, mirrored: image has been flipped back-to-front.
    case upSideDown = 3 //= 180 degrees: image is upside down.
    case upSideDownMirrored = 4 //= 180 degrees, mirrored: image has been flipped back-to-front and is upside down.
    case leftRotatedMirrored = 5 //= 90 degrees: image has been flipped back-to-front and is on its side.
    case leftRotated = 6 //= 90 degrees, mirrored: image is on its side.
    case rightRotatedMirrored = 7 //= 270 degrees: image has been flipped back-to-front and is on its far side.
    case rightRotated = 8 //= 270 degrees, mirrored: image is on its far side.
    
    func degrees() -> Float {
        switch self {
        case .normal, .normalMirrored:
            return 0
        case .upSideDown,  .upSideDownMirrored:
            return 180
        case .leftRotatedMirrored, .leftRotated:
            return -90
        case .rightRotatedMirrored, .rightRotated:
            return 90
        }
    }
    func flip() -> Bool {
        switch self {
        case .normal, .upSideDown, .leftRotated, .rightRotated:
            return false
        case .normalMirrored, .upSideDownMirrored, .leftRotatedMirrored, .rightRotatedMirrored:
            return true
        }
    }
}


// MARK :- After Re-Sizing
 let exifImage = SwiftExif.Image(imagePath: image).ExifRaw()

if let rawOrientation = exifImage["0"]?["Orientation"], let intOrientation = Int(rawOrientation), let orientation = ImageOrientation(rawValue: intOrientation) {
            modifiedImage?.rotate(angle: orientation.degrees())
            if orientation.flip() {
                modifiedImage?.flip(.horizontal)
            }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants