| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- class Image {
- // Takes an image and makes the largest dimension 1920 pixels large and rotates it according to EXIF data.
- public static function normalise($path, $size = 1920) {
- $size = getimagesize($path);
- list($width, $height, $type) = $size;
- $exif = exif_read_data($path);
- $orientation = 0;
- $rotate_angle = 0;
- $is_flipped = false;
- if($exif !== false) {
- $orientation = $exif["Orientation"] ?? 0;
- if(in_array($orientation, [6, 5])) $rotate_angle = 270;
- if(in_array($orientation, [3, 4])) $rotate_angle = 180;
- if(in_array($orientation, [8, 7])) $rotate_angle = 90;
- if(in_array($orientation, [5, 4, 7])) $is_flipped = true;
- }
- $original_image = imagecreatefromjpeg($path);
- if($width > $height) {
- if($width > 1920) {
- $width = 1920;
- }
- }
- else {
- if($height > 1920) {
- $ratio = $height / 1920;
- $width = floor($width / $ratio);
- }
- }
- // Resize
- if($width != $size[0]) {
- $output_image = imagescale($original_image, $width, -1, IMG_BICUBIC);
- imagedestroy($original_image);
- }
- else {
- $output_image = $original_image;
- }
- // Rotate
- if($rotate_angle) {
- $output_image = imagerotate($output_image, $rotate_angle, 0);
- }
- // Flip
- if($is_flipped) {
- $output_image = imageflip($output_image, IMG_FLIP_HORIZONTAL);
- }
- return $output_image;
- }
- }
|