From 18bceb367dec8d4419870f89cecf93b5de85934f Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Sun, 4 Sep 2016 21:59:32 +0100 Subject: [PATCH] Albums now display their photos in a grid --- app/Album.php | 3 +- app/AlbumSources/IAlbumSource.php | 4 +- app/AlbumSources/LocalFilesystemSource.php | 7 +++- app/Helpers/ImageHelper.php | 9 +++++ .../Controllers/Gallery/AlbumController.php | 37 +++++++++++++++++++ .../Controllers/Gallery/PhotoController.php | 2 +- app/Photo.php | 18 +++++++++ .../views/themes/base/gallery/album.blade.php | 14 +++++++ .../views/themes/base/gallery/index.blade.php | 2 +- routes/web.php | 4 +- 10 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 app/Http/Controllers/Gallery/AlbumController.php create mode 100644 resources/views/themes/base/gallery/album.blade.php diff --git a/app/Album.php b/app/Album.php index e11e798..21b66f6 100644 --- a/app/Album.php +++ b/app/Album.php @@ -7,6 +7,7 @@ use App\AlbumSources\LocalFilesystemSource; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\Route; class Album extends Model { @@ -71,6 +72,6 @@ class Album extends Model public function url() { - return sprintf('/%s', urlencode($this->url_alias)); + return route('viewAlbum', $this->url_alias); } } \ No newline at end of file diff --git a/app/AlbumSources/IAlbumSource.php b/app/AlbumSources/IAlbumSource.php index 44a4825..b8a4e4b 100644 --- a/app/AlbumSources/IAlbumSource.php +++ b/app/AlbumSources/IAlbumSource.php @@ -17,7 +17,7 @@ interface IAlbumSource * @param string $thumbnail Thumbnail to get the image to. * @return string */ - function getPathToPhoto(Album $album, Photo $photo, $thumbnail); + function getPathToPhoto(Album $album, Photo $photo, $thumbnail = null); /** * Gets the absolute URL to the given photo file. @@ -26,7 +26,7 @@ interface IAlbumSource * @param string $thumbnail Thumbnail to get the image to. * @return string */ - function getUrlToPhoto(Album $album, Photo $photo, $thumbnail); + function getUrlToPhoto(Album $album, Photo $photo, $thumbnail = null); /** * Saves a generated thumbnail to its permanent location. diff --git a/app/AlbumSources/LocalFilesystemSource.php b/app/AlbumSources/LocalFilesystemSource.php index 9984664..f0b4efd 100644 --- a/app/AlbumSources/LocalFilesystemSource.php +++ b/app/AlbumSources/LocalFilesystemSource.php @@ -37,14 +37,17 @@ class LocalFilesystemSource implements IAlbumSource public function getUrlToPhoto(Album $album, Photo $photo, $thumbnail = null) { - $photoUrl = sprintf('%s/%s', urlencode($album->url_alias), urlencode($photo->file_name)); + $photoUrl = route('downloadPhoto', [ + 'albumUrlAlias' => $album->url_alias, + 'photoFilename' => $photo->file_name + ]); if (!is_null($thumbnail)) { $photoUrl .= sprintf('?t=%s', urlencode($thumbnail)); } - return url($photoUrl); + return $photoUrl; } public function saveThumbnail(Album $album, Photo $photo, $thumbnailInfo, $tempFilename) diff --git a/app/Helpers/ImageHelper.php b/app/Helpers/ImageHelper.php index a860d5c..67f58d7 100644 --- a/app/Helpers/ImageHelper.php +++ b/app/Helpers/ImageHelper.php @@ -11,6 +11,15 @@ class ImageHelper { $thumbnailWidth = intval($thumbnailInfo['width']); $thumbnailHeight = intval($thumbnailInfo['height']); + + // If image is portrait, restrict the height by the relevant aspect ratio + if ($photo->height > $photo->width) + { + $aspectRatio = $photo->height / $photo->width; + $thumbnailWidth = intval($thumbnailInfo['height']) / $aspectRatio; + $thumbnailHeight = intval($thumbnailInfo['height']); + } + $thumbnailImageResource = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight); imagecopyresized( diff --git a/app/Http/Controllers/Gallery/AlbumController.php b/app/Http/Controllers/Gallery/AlbumController.php new file mode 100644 index 0000000..0d0ae58 --- /dev/null +++ b/app/Http/Controllers/Gallery/AlbumController.php @@ -0,0 +1,37 @@ + $album + ]); + } + + /** + * @param $id + * @return Album + */ + private static function loadAlbum($urlAlias) + { + $album = Album::where('url_alias', $urlAlias)->first(); + if (is_null($album)) + { + App::abort(404); + return null; + } + + return $album; + } +} diff --git a/app/Http/Controllers/Gallery/PhotoController.php b/app/Http/Controllers/Gallery/PhotoController.php index 93cab1a..5907e4c 100644 --- a/app/Http/Controllers/Gallery/PhotoController.php +++ b/app/Http/Controllers/Gallery/PhotoController.php @@ -11,7 +11,7 @@ use Symfony\Component\HttpFoundation\Request; class PhotoController extends Controller { - public function view(Request $request, $albumUrlAlias, $photoFilename) + public function download(Request $request, $albumUrlAlias, $photoFilename) { $album = PhotoController::loadAlbumByAlias($albumUrlAlias); $albumSource = $album->getAlbumSource(); diff --git a/app/Photo.php b/app/Photo.php index d9d62bd..51b72a1 100644 --- a/app/Photo.php +++ b/app/Photo.php @@ -42,4 +42,22 @@ class Photo extends Model { return $this->belongsTo(Album::class); } + + public function thumbnailUrl($thumbnailName) + { + /** @var Album $album */ + //$album = Album::where('id', $this->album_id)->first(); + $album = $this->album; + $albumSource = $album->getAlbumSource(); + + return $album->getAlbumSource()->getUrlToPhoto($album, $this, $thumbnailName); + } + + public function url() + { + return route('viewPhoto', [ + 'albumUrlAlias' => $this->album->url_alias, + 'photoFilename' => $this->file_name + ]); + } } diff --git a/resources/views/themes/base/gallery/album.blade.php b/resources/views/themes/base/gallery/album.blade.php new file mode 100644 index 0000000..3083caf --- /dev/null +++ b/resources/views/themes/base/gallery/album.blade.php @@ -0,0 +1,14 @@ +@extends('themes.base.layout') +@section('title', 'Welcome') + +@section('content') +
+
+ @foreach ($album->photos as $photo) +
+ +
+ @endforeach +
+
+@endsection \ No newline at end of file diff --git a/resources/views/themes/base/gallery/index.blade.php b/resources/views/themes/base/gallery/index.blade.php index 25e3540..925bacd 100644 --- a/resources/views/themes/base/gallery/index.blade.php +++ b/resources/views/themes/base/gallery/index.blade.php @@ -10,7 +10,7 @@
{{ $album->name }}

- +

{{ $album->description }}

diff --git a/routes/web.php b/routes/web.php index 28d8b95..bcec66a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -24,4 +24,6 @@ Route::group(['prefix' => 'admin'], function () { // Gallery Route::get('/', 'Gallery\DefaultController@index')->name('home'); -Route::get('/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@view')->name('viewPhoto'); \ No newline at end of file +Route::get('/{albumUrlAlias}', 'Gallery\AlbumController@index')->name('viewAlbum'); +Route::get('/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@view')->name('viewPhoto'); +Route::get('/photo/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@download')->name('downloadPhoto'); \ No newline at end of file