From 93b76d19543bcef531387e27cbb05fd066f9f30e Mon Sep 17 00:00:00 2001 From: Peter Deltchev Date: Sun, 27 Dec 2015 01:43:43 -0800 Subject: [PATCH] Added support for JPEG cover art. --- app/Commands/EditTrackCommand.php | 2 +- app/Console/Commands/ImportMLPMA.php | 1 - app/Http/Controllers/ImagesController.php | 6 ++--- app/Http/routes.php | 2 +- app/Image.php | 27 ++++++++++++++----- app/Track.php | 2 +- public/templates/directives/image-upload.html | 2 +- .../app/directives/image-upload.coffee | 4 +-- 8 files changed, 30 insertions(+), 16 deletions(-) diff --git a/app/Commands/EditTrackCommand.php b/app/Commands/EditTrackCommand.php index a8b97c13..420e4cfe 100644 --- a/app/Commands/EditTrackCommand.php +++ b/app/Commands/EditTrackCommand.php @@ -65,7 +65,7 @@ class EditTrackCommand extends CommandBase time() + (86400 * 2))) . (isset($this->_input['released_at']) && $this->_input['released_at'] != "" ? '|date' : ''), 'license_id' => 'required|exists:licenses,id', 'genre_id' => 'required|exists:genres,id', - 'cover' => 'image|mimes:png|min_width:350|min_height:350', + 'cover' => 'image|mimes:png,jpeg|min_width:350|min_height:350', 'track_type_id' => 'required|exists:track_types,id|not_in:'.TrackType::UNCLASSIFIED_TRACK, 'songs' => 'required_when:track_type,2|exists:songs,id', 'cover_id' => 'exists:images,id', diff --git a/app/Console/Commands/ImportMLPMA.php b/app/Console/Commands/ImportMLPMA.php index d38cf454..d6907962 100644 --- a/app/Console/Commands/ImportMLPMA.php +++ b/app/Console/Commands/ImportMLPMA.php @@ -516,5 +516,4 @@ class ImportMLPMA extends Command $tags ]; } - } diff --git a/app/Http/Controllers/ImagesController.php b/app/Http/Controllers/ImagesController.php index f6cbc515..15f8fc3f 100644 --- a/app/Http/Controllers/ImagesController.php +++ b/app/Http/Controllers/ImagesController.php @@ -28,7 +28,7 @@ use Response; class ImagesController extends Controller { - public function getImage($id, $type) + public function getImage($id, $type, $extension) { $coverType = Image::getImageTypeFromName($type); @@ -56,8 +56,8 @@ class ImagesController extends Controller $response->header('X-Accel-Redirect', $filename); } - $response->header('Content-Disposition', "filename=\"ponyfm-i${id}-${type}.png\""); - $response->header('Content-Type', 'image/png'); + $response->header('Content-Disposition', "filename=\"ponyfm-i${id}-${type}.{$image->extension}\""); + $response->header('Content-Type', $image->mime); $lastModified = filemtime($filename); diff --git a/app/Http/routes.php b/app/Http/routes.php index 165ec426..9289f759 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -60,7 +60,7 @@ Route::get('/about', function() { return View::make('pages.about'); }); Route::get('/faq', function() { return View::make('pages.faq'); }); Route::get('/mlpforums-advertising-program', function() { return View::make('pages.mlpforums-advertising-program'); }); -Route::get('i{id}/{type}.png', 'ImagesController@getImage')->where('id', '\d+'); +Route::get('i{id}/{type}.{extension}', 'ImagesController@getImage')->where('id', '\d+'); Route::get('playlist/{id}-{slug}', 'PlaylistsController@getPlaylist'); Route::get('p{id}', 'PlaylistsController@getShortlink')->where('id', '\d+'); diff --git a/app/Image.php b/app/Image.php index c888ff79..cc0d4d1e 100644 --- a/app/Image.php +++ b/app/Image.php @@ -78,12 +78,27 @@ class Image extends Model $image->ensureDirectoryExists(); foreach (self::$ImageTypes as $coverType) { - $command = 'convert 2>&1 "' . $file->getPathname() . '" -background transparent -flatten +matte -strip -quality 95 -format png '; - if (isset($coverType['width']) && isset($coverType['height'])) { - $command .= '-thumbnail ' . $coverType['width'] . 'x' . $coverType['height'] . '^ -gravity center -extent ' . $coverType['width'] . 'x' . $coverType['height'] . ' '; + if ($coverType['id'] === self::ORIGINAL && $image->mime === 'image/jpeg') { + $command = 'cp '.$file->getPathname().' '.$image->getFile($coverType['id']); + + } else { + // ImageMagick options reference: http://www.imagemagick.org/script/command-line-options.php + $command = 'convert 2>&1 "' . $file->getPathname() . '" -background white -alpha remove -alpha off -strip'; + + if ($image->mime === 'image/jpeg') { + $command .= ' -quality 100 -format jpeg'; + + } else { + $command .= ' -quality 95 -format png'; + } + + if (isset($coverType['width']) && isset($coverType['height'])) { + $command .= " -thumbnail ${coverType['width']}x${coverType['height']}^ -gravity center -extent ${coverType['width']}x${coverType['height']}"; + } + + $command .= ' "' . $image->getFile($coverType['id']) . '"'; } - $command .= '"' . $image->getFile($coverType['id']) . '"'; External::execute($command); } @@ -100,7 +115,7 @@ class Image extends Model { $type = self::$ImageTypes[$type]; - return action('ImagesController@getImage', ['id' => $this->id, 'type' => $type['name']]); + return action('ImagesController@getImage', ['id' => $this->id, 'type' => $type['name'], 'extension' => $this->extension]); } public function getFile($type = self::NORMAL) @@ -112,7 +127,7 @@ class Image extends Model { $typeInfo = self::$ImageTypes[$type]; - return $this->id . '_' . $typeInfo['name'] . '.png'; + return $this->id . '_' . $typeInfo['name'] . '.'.$this->extension; } public function getDirectory() diff --git a/app/Track.php b/app/Track.php index 621d4ea0..a23bda86 100644 --- a/app/Track.php +++ b/app/Track.php @@ -741,7 +741,7 @@ class Track extends Model 'data' => file_get_contents($this->cover->getFile()), 'picturetypeid' => 2, 'description' => 'cover', - 'mime' => 'image/png' + 'mime' => $this->cover->mime ]; } diff --git a/public/templates/directives/image-upload.html b/public/templates/directives/image-upload.html index e9cec93e..00e5f2e5 100644 --- a/public/templates/directives/image-upload.html +++ b/public/templates/directives/image-upload.html @@ -1,7 +1,7 @@

- Image must be a PNG that is at least 350x350.
+ Image must be a PNG or JPEG that is at least 350x350.

diff --git a/resources/assets/scripts/app/directives/image-upload.coffee b/resources/assets/scripts/app/directives/image-upload.coffee index 964bc21d..69410209 100644 --- a/resources/assets/scripts/app/directives/image-upload.coffee +++ b/resources/assets/scripts/app/directives/image-upload.coffee @@ -84,8 +84,8 @@ angular.module('ponyfm').directive 'pfmImageUpload', () -> $scope.imageObject = null $scope.imageFile = file - if file.type != 'image/png' - $scope.error = 'Image must be a png!' + if file.type not in ['image/png', 'image/jpeg'] + $scope.error = 'Image must be a PNG or JPEG!' $scope.isImageLoaded = false $scope.imageObject = $scope.imageFile = $scope.imageUrl = null return