Update Image class, remove PNG -> JPEG conversion and use orientate() method to detect proper orientation in Exif

This commit is contained in:
Daniel Supernault 2018-06-02 15:29:33 -06:00
parent ae6a59e782
commit b437b62780

View file

@ -105,42 +105,25 @@ class Image {
$orientation = $ratio['orientation']; $orientation = $ratio['orientation'];
try { try {
$img = Intervention::make($file); $img = Intervention::make($file)->orientate();
$img->resize($aspect['width'], $aspect['height'], function ($constraint) { $img->resize($aspect['width'], $aspect['height'], function ($constraint) {
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
$converted = $this->convertPngToJpeg($path, $thumbnail, $img->extension); $converted = $this->setBaseName($path, $thumbnail, $img->extension);
$newPath = storage_path('app/'.$converted['path']); $newPath = storage_path('app/'.$converted['path']);
$is_png = false;
$img->save($newPath, 75);
if($img->extension == 'png' || $converted['png'] == true) {
\Log::info('PNG detected, ' . json_encode([$img, $converted]));
$is_png = true;
$newPath = str_replace('.png', '.jpeg', $newPath);
$img->encode('jpg', 80)->save($newPath);
if(!$thumbnail) {
@unlink($file);
}
\Log::info('PNG SAVED, ' . json_encode([$img, $newPath]));
} else {
\Log::info('PNG not detected, ' . json_encode([$img, $converted]));
$img->save($newPath, 75);
}
if(!$thumbnail) { if(!$thumbnail) {
$media->orientation = $orientation; $media->orientation = $orientation;
} }
if($is_png == true) { if($thumbnail == true) {
if($thumbnail == false) { $media->thumbnail_path = $converted['path'];
$media->thumbnail_url = url(Storage::url($converted['path']));
} else {
$media->media_path = $converted['path']; $media->media_path = $converted['path'];
$media->mime = $img->mime; $media->mime = $img->mime;
}
}
if($thumbnail == true) {
$media->thumbnail_path = $converted['path'];
$media->thumbnail_url = url(Storage::url($converted['path']));
} }
$media->save(); $media->save();
@ -150,18 +133,14 @@ class Image {
} }
} }
public function convertPngToJpeg($basePath, $thumbnail = false, $extension) public function setBaseName($basePath, $thumbnail = false, $extension)
{ {
$png = false; $png = false;
$path = explode('.', $basePath); $path = explode('.', $basePath);
$name = ($thumbnail == true) ? $path[0] . '_thumb' : $path[0]; $name = ($thumbnail == true) ? $path[0] . '_thumb' : $path[0];
$ext = last($path); $ext = last($path);
$basePath = "{$name}.{$ext}"; $basePath = "{$name}.{$ext}";
if($extension == 'png' || $ext == 'png') {
$ext = 'jpeg';
$basePath = "{$name}.{$ext}";
$png = true;
}
return ['path' => $basePath, 'png' => $png]; return ['path' => $basePath, 'png' => $png];
} }