What are common issues with image rotation when uploading images from iPhones in PHP forms?
When uploading images from iPhones in PHP forms, a common issue is that the images may have incorrect orientation due to EXIF data. To solve this problem, you can use PHP libraries like `exif_imagetype` and `imagecreatefromjpeg` to detect the orientation and rotate the image accordingly.
// Check if the uploaded file is an image
if (exif_imagetype($_FILES['file']['tmp_name']) == IMAGETYPE_JPEG) {
$image = imagecreatefromjpeg($_FILES['file']['tmp_name']);
// Rotate the image based on orientation
$exif = exif_read_data($_FILES['file']['tmp_name']);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
}
// Save the rotated image
imagejpeg($image, 'uploads/' . $_FILES['file']['name']);
// Free up memory
imagedestroy($image);
}