From fe0b4c21083ce026c4383539dc1736d9f6282488 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Mon, 3 Oct 2016 15:57:58 +0100 Subject: [PATCH] refs #6: added the ability to flip photos horizontally, vertically and both --- app/Helpers/ImageHelper.php | 20 +++++++ .../Controllers/Admin/AlbumController.php | 4 ++ .../Controllers/Admin/PhotoController.php | 17 ++++++ .../Middleware/CheckMaxPostSizeExceeded.php | 1 + app/Services/PhotoService.php | 15 +++++ resources/lang/en/admin.php | 5 +- .../themes/base/admin/show_album.blade.php | 58 +++++++++++++++++++ .../partials/single_photo_admin.blade.php | 12 ++-- routes/web.php | 1 + 9 files changed, 128 insertions(+), 5 deletions(-) diff --git a/app/Helpers/ImageHelper.php b/app/Helpers/ImageHelper.php index 3f18d5b..c3f8207 100644 --- a/app/Helpers/ImageHelper.php +++ b/app/Helpers/ImageHelper.php @@ -7,6 +7,26 @@ use App\Photo; class ImageHelper { + public function flipImage($imageResource, $flipHorizontal, $flipVertical) + { + imagesetinterpolation($imageResource, $this->getInterpolationMethod()); + + if ($flipHorizontal && $flipVertical) + { + return imageflip($imageResource, IMG_FLIP_BOTH); + } + else if ($flipHorizontal) + { + return imageflip($imageResource, IMG_FLIP_HORIZONTAL); + } + else if ($flipVertical) + { + return imageflip($imageResource, IMG_FLIP_VERTICAL); + } + + return false; + } + public function generateThumbnail($gdImageResource, Photo $photo, $thumbnailInfo) { $thumbnailWidth = intval($thumbnailInfo['width']); diff --git a/app/Http/Controllers/Admin/AlbumController.php b/app/Http/Controllers/Admin/AlbumController.php index d5af032..d9faff0 100644 --- a/app/Http/Controllers/Admin/AlbumController.php +++ b/app/Http/Controllers/Admin/AlbumController.php @@ -161,6 +161,10 @@ class AlbumController extends Controller 'rotate_left' => trans('admin.photo_actions.rotate_left'), 'rotate_right' => trans('admin.photo_actions.rotate_right'), '-' => '-----', + 'flip_horizontal' => trans('admin.photo_actions.flip_horizontal'), + 'flip_vertical' => trans('admin.photo_actions.flip_vertical'), + 'flip_both' => trans('admin.photo_actions.flip_both'), + '--' => '-----', 'refresh_thumbnails' => trans('admin.photo_actions.refresh_thumbnails'), 'delete' => trans('admin.photo_actions.delete') ], diff --git a/app/Http/Controllers/Admin/PhotoController.php b/app/Http/Controllers/Admin/PhotoController.php index 4820c3a..2ca2779 100644 --- a/app/Http/Controllers/Admin/PhotoController.php +++ b/app/Http/Controllers/Admin/PhotoController.php @@ -105,6 +105,23 @@ class PhotoController extends Controller return back(); } + public function flip($photoId, $horizontal, $vertical) + { + $this->authorize('admin-access'); + + settype($direction, 'boolean'); + + $photo = Photo::where('id', intval($photoId))->first(); + if (is_null($photo)) + { + App::abort(404); + return null; + } + + $photoService = new PhotoService($photo); + $photoService->flip($horizontal, $vertical); + } + public function regenerateThumbnails($photoId) { $this->authorize('admin-access'); diff --git a/app/Http/Middleware/CheckMaxPostSizeExceeded.php b/app/Http/Middleware/CheckMaxPostSizeExceeded.php index ef824d8..efad4ea 100644 --- a/app/Http/Middleware/CheckMaxPostSizeExceeded.php +++ b/app/Http/Middleware/CheckMaxPostSizeExceeded.php @@ -18,6 +18,7 @@ class CheckMaxPostSizeExceeded protected $exclude = [ '/admin/photos/analyse/*', + '/admin/photos/flip/*', '/admin/photos/regenerate-thumbnails/*', '/admin/photos/rotate/*' ]; diff --git a/app/Services/PhotoService.php b/app/Services/PhotoService.php index 096269f..a0f9b70 100644 --- a/app/Services/PhotoService.php +++ b/app/Services/PhotoService.php @@ -137,6 +137,21 @@ class PhotoService $this->photo->delete(); } + public function flip($horizontal, $vertical) + { + $imageInfo = array(); + $photoPath = $this->albumSource->getPathToPhoto($this->photo); + $originalPhotoImage = $this->imageHelper->openImage($photoPath, $imageInfo); + if ($this->imageHelper->flipImage($originalPhotoImage, boolval($horizontal), boolval($vertical))) + { + $this->imageHelper->saveImage($originalPhotoImage, $photoPath, $imageInfo); + + $this->regenerateThumbnails($originalPhotoImage); + + $this->photo->save(); + } + } + public function regenerateThumbnails($originalPhotoResource = null) { if (is_null($originalPhotoResource)) diff --git a/resources/lang/en/admin.php b/resources/lang/en/admin.php index a7ed9b3..991a193 100644 --- a/resources/lang/en/admin.php +++ b/resources/lang/en/admin.php @@ -50,7 +50,10 @@ return [ 'no_storages_title' => 'No storage locations defined', 'open_album' => 'Open album', 'photo_actions' => [ - 'delete' => 'Delete', + 'delete' => 'Delete permanently', + 'flip_both' => 'Flip both', + 'flip_horizontal' => 'Flip horizontally', + 'flip_vertical' => 'Flip vertically', 'refresh_thumbnails' => 'Refresh thumbnails', 'rotate_left' => 'Rotate left', 'rotate_right' => 'Rotate right' diff --git a/resources/views/themes/base/admin/show_album.blade.php b/resources/views/themes/base/admin/show_album.blade.php index c5007e0..748af58 100644 --- a/resources/views/themes/base/admin/show_album.blade.php +++ b/resources/views/themes/base/admin/show_album.blade.php @@ -191,6 +191,40 @@ }); } + function flipPhoto(photo_id, horizontal, vertical, parent) + { + var url = '{{ route('photos.flip', ['id' => 0, 'horizontal' => -1, 'vertical' => -2]) }}'; + url = url.replace('/0/', '/' + photo_id + '/'); + + if (horizontal) + { + url = url.replace(/\/-1\//, '/1/'); + } + else + { + url = url.replace(/\/-1\//, '/0/'); + } + + if (vertical) + { + url = url.replace(/\/-2$/, '/1'); + } + else + { + url = url.replace(/\/-2$/, '/0'); + } + + $('.loading', parent).show(); + $.post(url, function() + { + var image = $('img.photo-thumbnail', parent); + var originalUrl = image.data('original-src'); + image.attr('src', originalUrl + "&_=" + new Date().getTime()); + + $('.loading', parent).hide(); + }); + } + function regenerateThumbnails(photo_id, parent) { var url = '{{ route('photos.regenerateThumbnails', ['id' => 0]) }}'; @@ -256,6 +290,30 @@ return false; }); + $('a.flip-photo-both').click(function() { + var parent = $(this).parents('.photo'); + var photo_id = $(parent).data('photo-id'); + + flipPhoto(photo_id, true, true, parent); + $(this).dropdown('toggle'); + return false; + }); + $('a.flip-photo-horizontal').click(function() { + var parent = $(this).parents('.photo'); + var photo_id = $(parent).data('photo-id'); + + flipPhoto(photo_id, true, false, parent); + $(this).dropdown('toggle'); + return false; + }); + $('a.flip-photo-vertical').click(function() { + var parent = $(this).parents('.photo'); + var photo_id = $(parent).data('photo-id'); + + flipPhoto(photo_id, false, true, parent); + $(this).dropdown('toggle'); + return false; + }); $('a.regenerate-thumbnails').click(function() { var parent = $(this).parents('.photo'); var photo_id = $(parent).data('photo-id'); diff --git a/resources/views/themes/base/partials/single_photo_admin.blade.php b/resources/views/themes/base/partials/single_photo_admin.blade.php index 01a476b..b69dc25 100644 --- a/resources/views/themes/base/partials/single_photo_admin.blade.php +++ b/resources/views/themes/base/partials/single_photo_admin.blade.php @@ -15,8 +15,12 @@ @@ -25,8 +29,8 @@ diff --git a/routes/web.php b/routes/web.php index 3ae1436..891f2b4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -27,6 +27,7 @@ Route::group(['prefix' => 'admin'], function () { // Photo management Route::post('photos/analyse/{id}', 'Admin\PhotoController@analyse')->name('photos.analyse'); + Route::post('photos/flip/{photoId}/{horizontal}/{vertical}', 'Admin\PhotoController@flip')->name('photos.flip'); Route::post('photos/regenerate-thumbnails/{id}', 'Admin\PhotoController@regenerateThumbnails')->name('photos.regenerateThumbnails'); Route::post('photos/rotate/{photoId}/{angle}', 'Admin\PhotoController@rotate')->name('photos.rotate'); Route::post('photos/store-bulk', 'Admin\PhotoController@storeBulk')->name('photos.storeBulk');