Albums now display their photos in a grid

This commit is contained in:
Andy Heathershaw 2016-09-04 21:59:32 +01:00
parent b08a0e4710
commit 18bceb367d
10 changed files with 92 additions and 8 deletions

View File

@ -7,6 +7,7 @@ use App\AlbumSources\LocalFilesystemSource;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Route;
class Album extends Model class Album extends Model
{ {
@ -71,6 +72,6 @@ class Album extends Model
public function url() public function url()
{ {
return sprintf('/%s', urlencode($this->url_alias)); return route('viewAlbum', $this->url_alias);
} }
} }

View File

@ -17,7 +17,7 @@ interface IAlbumSource
* @param string $thumbnail Thumbnail to get the image to. * @param string $thumbnail Thumbnail to get the image to.
* @return string * @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. * Gets the absolute URL to the given photo file.
@ -26,7 +26,7 @@ interface IAlbumSource
* @param string $thumbnail Thumbnail to get the image to. * @param string $thumbnail Thumbnail to get the image to.
* @return string * @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. * Saves a generated thumbnail to its permanent location.

View File

@ -37,14 +37,17 @@ class LocalFilesystemSource implements IAlbumSource
public function getUrlToPhoto(Album $album, Photo $photo, $thumbnail = null) 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)) if (!is_null($thumbnail))
{ {
$photoUrl .= sprintf('?t=%s', urlencode($thumbnail)); $photoUrl .= sprintf('?t=%s', urlencode($thumbnail));
} }
return url($photoUrl); return $photoUrl;
} }
public function saveThumbnail(Album $album, Photo $photo, $thumbnailInfo, $tempFilename) public function saveThumbnail(Album $album, Photo $photo, $thumbnailInfo, $tempFilename)

View File

@ -11,6 +11,15 @@ class ImageHelper
{ {
$thumbnailWidth = intval($thumbnailInfo['width']); $thumbnailWidth = intval($thumbnailInfo['width']);
$thumbnailHeight = intval($thumbnailInfo['height']); $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); $thumbnailImageResource = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
imagecopyresized( imagecopyresized(

View File

@ -0,0 +1,37 @@
<?php
namespace App\Http\Controllers\Gallery;
use App\Album;
use App\Facade\Theme;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Http\Request;
class AlbumController extends Controller
{
public function index(Request $request, $albumUrlAlias)
{
$album = AlbumController::loadAlbum($albumUrlAlias);
return Theme::render('gallery.album', [
'album' => $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;
}
}

View File

@ -11,7 +11,7 @@ use Symfony\Component\HttpFoundation\Request;
class PhotoController extends Controller class PhotoController extends Controller
{ {
public function view(Request $request, $albumUrlAlias, $photoFilename) public function download(Request $request, $albumUrlAlias, $photoFilename)
{ {
$album = PhotoController::loadAlbumByAlias($albumUrlAlias); $album = PhotoController::loadAlbumByAlias($albumUrlAlias);
$albumSource = $album->getAlbumSource(); $albumSource = $album->getAlbumSource();

View File

@ -42,4 +42,22 @@ class Photo extends Model
{ {
return $this->belongsTo(Album::class); 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
]);
}
} }

View File

@ -0,0 +1,14 @@
@extends('themes.base.layout')
@section('title', 'Welcome')
@section('content')
<div class="container">
<div class="row">
@foreach ($album->photos as $photo)
<div class="col-xs-12 col-sm-4">
<a href="{{ $photo->url() }}"><img src="{{ $photo->thumbnailUrl('preview') }}" alt="" class="img-thumbnail"/></a>
</div>
@endforeach
</div>
</div>
@endsection

View File

@ -10,7 +10,7 @@
<div class="panel-heading"><a href="{{ $album->url() }}">{{ $album->name }}</a></div> <div class="panel-heading"><a href="{{ $album->url() }}">{{ $album->name }}</a></div>
<div class="panel-body"> <div class="panel-body">
<p class="text-center"> <p class="text-center">
<img src="{{ $album->thumbnailUrl('preview') }}"/> <img class="img-responsive" src="{{ $album->thumbnailUrl('preview') }}"/>
</p> </p>
<p>{{ $album->description }}</p> <p>{{ $album->description }}</p>
</div> </div>

View File

@ -24,4 +24,6 @@ Route::group(['prefix' => 'admin'], function () {
// Gallery // Gallery
Route::get('/', 'Gallery\DefaultController@index')->name('home'); Route::get('/', 'Gallery\DefaultController@index')->name('home');
Route::get('/{albumUrlAlias}', 'Gallery\AlbumController@index')->name('viewAlbum');
Route::get('/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@view')->name('viewPhoto'); Route::get('/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@view')->name('viewPhoto');
Route::get('/photo/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@download')->name('downloadPhoto');