From b437b627802fcba7800e22569b47a20d645a21d2 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 2 Jun 2018 15:29:33 -0600 Subject: [PATCH] Update Image class, remove PNG -> JPEG conversion and use orientate() method to detect proper orientation in Exif --- app/Util/Media/Image.php | 41 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/app/Util/Media/Image.php b/app/Util/Media/Image.php index a7d53d34..db7641fd 100644 --- a/app/Util/Media/Image.php +++ b/app/Util/Media/Image.php @@ -105,42 +105,25 @@ class Image { $orientation = $ratio['orientation']; try { - $img = Intervention::make($file); + $img = Intervention::make($file)->orientate(); $img->resize($aspect['width'], $aspect['height'], function ($constraint) { $constraint->aspectRatio(); }); - $converted = $this->convertPngToJpeg($path, $thumbnail, $img->extension); + $converted = $this->setBaseName($path, $thumbnail, $img->extension); $newPath = storage_path('app/'.$converted['path']); - $is_png = false; - - 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); - } + + $img->save($newPath, 75); if(!$thumbnail) { $media->orientation = $orientation; } - if($is_png == true) { - if($thumbnail == false) { + if($thumbnail == true) { + $media->thumbnail_path = $converted['path']; + $media->thumbnail_url = url(Storage::url($converted['path'])); + } else { $media->media_path = $converted['path']; $media->mime = $img->mime; - } - } - - if($thumbnail == true) { - $media->thumbnail_path = $converted['path']; - $media->thumbnail_url = url(Storage::url($converted['path'])); } $media->save(); @@ -150,18 +133,14 @@ class Image { } } - public function convertPngToJpeg($basePath, $thumbnail = false, $extension) + public function setBaseName($basePath, $thumbnail = false, $extension) { $png = false; $path = explode('.', $basePath); $name = ($thumbnail == true) ? $path[0] . '_thumb' : $path[0]; $ext = last($path); $basePath = "{$name}.{$ext}"; - if($extension == 'png' || $ext == 'png') { - $ext = 'jpeg'; - $basePath = "{$name}.{$ext}"; - $png = true; - } + return ['path' => $basePath, 'png' => $png]; }