php image orientation correction
Sep 9, 2020
PHP
image oriented
```php: image.php
$ filename = “path”
if (mime_content_type ($ filename) =='image / jpeg’) { $ image = ImageCreateFromJPEG ($ filename); } else { $ image = ImageCreateFromPNG ($ filename); }
$ exif_datas = @exif_read_data ($ filename);
if (isset ($ exif_datas ['Orientation'])) {
$ orientation = $ exif_datas ['Orientation'];
//rotation angle
$ degrees = 0;
switch ($ orientation) {
case 1: // No rotation (↑)
break;
case 8: // 90 degrees to the right (→)
$ degrees = 90;
break;
case 3: // Rotate 180 degrees (↓)
$ degrees = 180;
break;
case 6: // Rotate 270 degrees to the right (←)
$ degrees = 270;
break;
case 2: // Reverse (↑)
$ mode = IMG_FLIP_HORIZONTAL;
break;
case 7: // Invert and right 90 degrees (→)
$ degrees = 90;
$ mode = IMG_FLIP_HORIZONTAL;
break;
case 4: // Inverted to 180 degrees, but same as vertical inversion (↓)
$ mode = IMG_FLIP_VERTICAL;
break;
case 5: // Invert and 270 degrees (←)
$ degrees = 270;
$ mode = IMG_FLIP_HORIZONTAL;
break;
}
// Invert (2,7,4,5)
if (isset ($ mode)) {
$ image = imageflip ($ image, $ mode);
}
// Rotate (8,3,6,7,5)
if ($ degrees> 0) {
$ image = imagerotate ($ image, $ degrees, 0);
}
// Save
if (mime_content_type ($ filename) =='image / jpeg') {
ImageJPEG ($ image, $ filename);
} else {
ImagePNG ($ image, $ filename);
}
// // Memory release
imagedestroy ($ image);
}