1

I have written an app which takes a photo and uploads it to a remote database using a PHP file on that database. The problem is that when the photo is read from a website or just loaded from the link, the image is presented in landscape. There is a part of the app which displays this image, the orientation is correct when loaded in the app so I presume it is getting the exif data in order to do this? My question basically is how do I rotate either the UIImage object before its uploaded or how do I change the PHP data so it will place it in the database in the correct orientation?

Code from app:

// Display the activity indicator 
[myActivityIndicator startAnimating];
//Set up variables and copy image into NSData compressed
srandom(time(NULL));
NSURL *url = [NSURL URLWithString:@"http://www.mydomain/test-upload.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];  
NSData *compressedImageData = UIImageJPEGRepresentation(theImageView.image, 0.5f);

//set the filename to a random number

int randomNumber = (random() % 250000);
NSString* fileName = [NSString stringWithFormat:@"%d.jpg", randomNumber];

// Set the filename and upload

[request setFile:compressedImageData withFileName:fileName andContentType:@"image/jpeg" forKey:@"photo"];
[request setDelegate:self];
[self Update:fileName];

//Create a location manager and start it uploading (delegate : Location update)
[request startAsynchronous];

The code sample wouldnt work for the PHP:

$uploaddir = 'images/';
$file = basename($_FILES['photo']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile)) {
        echo "http://www.mysite.com/{$file}";
}
else {
$isfile = $_FILES['photo'];

if(file_exists($file)) {
echo 'file exists';
} else {
echo 'File not found!';
}

echo "fail";
echo "Name is" . $file; 
paruss
  • 31
  • 4

2 Answers2

2

Ok so to answer my question, this is what solved it: UIImageFix I was a bit reluctant to put in such a huge lump of code to solve a simple problem but it isn't as easy to solve as I first thought. I was worried if the rotated UIImage would cause problems for reading the file on the phone but this was not a problem as Exif data is kept in tact and the phone looks at this before presenting it in an image view.

This is all I had to write in the end as an addition to the scaleAndRotate function:

UIImage *imageToUpload = scaleAndRotateImage(self.theImageView.image);
paruss
  • 31
  • 4
1

How to Rotate a UIImage 90 degrees? might help you to rotate the image in any direction you want.

If you want to handle exif data on the iPhone, this might help: http://www.gotow.net/creative/wordpress/?p=64, otherwise: http://www.php.net/manual/en/function.exif-read-data.php#76964 is the equivalent of what I did in Python.

Community
  • 1
  • 1
Tudor
  • 4,137
  • 5
  • 38
  • 54
  • 1
    The first part of your answer is a little uncalled for and, at least, [condescending](http://meta.stackexchange.com/questions/15650/ban-lmgtfy-let-me-google-that-for-you-links/15652#15652). The second part doesn't seem to address the question at all, since I read the question as: 'how can I use/access the EXIF data to properly orient the image using php?' – David Thomas Apr 02 '11 at 22:33
  • Hi, that tutorial looks useful, ill try to get something working and post back my results. I dont really care whether its exif data being read or not just having it not in landscape would do. Gotta demo this for a project on thursday! Thanks for your help. – paruss Apr 04 '11 at 10:29