| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- class Image {
- // Takes an image and makes the largest dimension 1920 pixels large and rotates it according to EXIF data.
- public static function isJPEG($path) {
- $size = @getimagesize($path);
- if(!$size) return false;
- list(, , $type) = $size;
- if($type != IMAGETYPE_JPEG) return false;
- return true;
- }
- public static function normalise($path, $max_width = 1920) {
- $size = getimagesize($path);
- list($width, $height) = $size;
- $exif = exif_read_data($path);
- $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 > $max_width) {
- $width = $max_width;
- }
- }
- else {
- if($height > $max_width) {
- $ratio = $height / $max_width;
- $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;
- }
- }
|