janosdupai
janosdupai

Reputation: 597

Swift capture photo in portrait mode and upload to server - The uploaded image appears in wrong orientation

If I take a picture with my iPhone in portrait mode, and send this picture with alamofire to server -> the image will stored perfectly. If I try to use this picture in html, the orientation is wrong. The image is rotated -90 degrees. I tried to save the image in jpg, and png format, but I have the same issue both.

Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(imageData, withName: "image", fileName: "image.png", mimeType: "image/png")
    },
        to: SERVER_URL,
        encodingCompletion: { encodingResult in
            switch encodingResult {.....

On the server side:

<?php
.....
$response['error'] = "NULL";
$filename = uniqid() . ".png";
move_uploaded_file($_FILES['image']['tmp_name'], "image/" . $filename) ...... 
 ?>

I think there will be 2 different solution, but I could not find the correct way. 1) I have to change the orientation on my iPhone device somehow 2) I have to change the orientation on the server side before I upload to the server.

(I don't want to make the solution on the client side)

Upvotes: 1

Views: 493

Answers (1)

fewlinesofcode
fewlinesofcode

Reputation: 3082

You can get UIImage orientation using imageOrientation property and pass it to the server side as a parameter, and then to decide what to do on the backend.

Also you can change image orientation using the code below:

let pngImage = #imageLiteral(resourceName: "yourImageFileName")
let image = UIImage(cgImage: pngImage.cgImage, scale: pngImage.scale,   orientation: UIImage.Orientation.up)

Upvotes: 1

Related Questions