EXIF orientation is now respected and the original rotation angle stored in the database

This commit is contained in:
Andy Heathershaw 2016-09-05 09:58:37 +01:00
parent 18bceb367d
commit 7209e21e5a
3 changed files with 59 additions and 2 deletions

View File

@ -106,15 +106,43 @@ class ProcessUploadCommand extends Command
// Read the Exif data
$exifData = @exif_read_data($photoFile);
$angleToRotate = 0;
// For debugging:
//dump($exifData);
if (isset($exifData['Orientation']))
{
switch ($exifData['Orientation'])
{
case 3:
$angleToRotate = 180;
break;
case 6:
$angleToRotate = 270;
break;
case 8:
$angleToRotate = 90;
break;
}
if ($angleToRotate > 0)
{
$originalPhotoResource = $this->imageHelper->rotateImage($originalPhotoResource, $angleToRotate);
if ($angleToRotate == 90 || $angleToRotate == 270)
{
$photo->width = $imageInfo[1];
$photo->height = $imageInfo[0];
}
}
}
$photo->metadata_version = ProcessUploadCommand::METADATA_VERSION;
$photo->taken_at = $this->metadataDateTime($exifData);
$photo->camera_make = $this->metadataCameraMake($exifData);
$photo->camera_model = $this->metadataCameraModel($exifData);
$photo->camera_software = $this->metadataCameraSoftware($exifData);
$photo->rotation = $angleToRotate;
$photo->save();
// Generate and save thumbnails

View File

@ -85,4 +85,31 @@ class ImageHelper
return $im;
}
public function rotateImage($imageResource, $angle)
{
imagesetinterpolation($imageResource, IMG_SINC);
return imagerotate($imageResource, $angle, 0);
/*switch ($angle)
{
case 90:
case 270:
// Need a new image canvas
$maxDimension = ($photo->width > $photo->height ? $photo->width : $photo->height);
$newImageResource = imagecreatetruecolor($maxDimension, $maxDimension);
imagecopy($newImageResource, $imageResource, 0, 0, 0, 0, $photo->width, $photo->height);
$newImageResource = imagerotate($newImageResource, $angle, 0);
$imageResource = $newImageResource;
break;
case 180:
// We can just rotate this in-place
$imageResource = imagerotate($imageResource, $angle, 0);
break;
}
return $imageResource;*/
}
}

View File

@ -21,6 +21,7 @@ class AddPhotoMetadataColumns extends Migration
$table->string('camera_software')->nullable();
$table->integer('width')->nullable();
$table->integer('height')->nullable();
$table->integer('rotation')->nullable();
});
}
@ -39,6 +40,7 @@ class AddPhotoMetadataColumns extends Migration
$table->dropColumn('camera_software');
$table->dropColumn('width');
$table->dropColumn('height');
$table->dropColumn('rotation');
});
}
}