Added mulit image-type compatability to manager & app and added scaled image selection

This commit is contained in:
Dan Brown 2015-12-08 22:04:59 +00:00
parent 32d5b12d27
commit a3188d349c
9 changed files with 78 additions and 25 deletions

View File

@ -33,23 +33,24 @@ class ImageController extends Controller
/** /**
* Get all images, Paginated * Get all gallery images, Paginated
* @param int $page * @param int $page
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function getAllGallery($page = 0) public function getAllByType($type, $page = 0)
{ {
$imgData = $this->imageRepo->getAllGallery($page); $imgData = $this->imageRepo->getPaginatedByType($type, $page);
return response()->json($imgData); return response()->json($imgData);
} }
/** /**
* Handles image uploads for use on pages. * Handles image uploads for use on pages.
* @param string $type
* @param Request $request * @param Request $request
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function uploadGallery(Request $request) public function uploadByType($type, Request $request)
{ {
$this->checkPermission('image-create'); $this->checkPermission('image-create');
$this->validate($request, [ $this->validate($request, [
@ -57,10 +58,25 @@ class ImageController extends Controller
]); ]);
$imageUpload = $request->file('file'); $imageUpload = $request->file('file');
$image = $this->imageRepo->saveNew($imageUpload, 'gallery'); $image = $this->imageRepo->saveNew($imageUpload, $type);
return response()->json($image); return response()->json($image);
} }
/**
* Generate a sized thumbnail for an image.
* @param $id
* @param $width
* @param $height
* @param $crop
* @return \Illuminate\Http\JsonResponse
*/
public function getThumbnail($id, $width, $height, $crop)
{
$this->checkPermission('image-create');
$image = $this->imageRepo->getById($id);
$thumbnailUrl = $this->imageRepo->getThumbnail($image, $width, $height, $crop == 'false');
return response()->json(['url' => $thumbnailUrl]);
}
/** /**
* Update image details * Update image details

View File

@ -18,7 +18,8 @@ class UserController extends Controller
/** /**
* UserController constructor. * UserController constructor.
* @param $user * @param User $user
* @param UserRepo $userRepo
*/ */
public function __construct(User $user, UserRepo $userRepo) public function __construct(User $user, UserRepo $userRepo)
{ {

View File

@ -57,10 +57,12 @@ Route::group(['middleware' => 'auth'], function () {
// Image routes // Image routes
Route::group(['prefix' => 'images'], function() { Route::group(['prefix' => 'images'], function() {
Route::get('/gallery/all', 'ImageController@getAllGallery'); // Standard get, update and deletion for all types
Route::get('/gallery/all/{page}', 'ImageController@getAllGallery'); Route::get('/thumb/{id}/{width}/{height}/{crop}', 'ImageController@getThumbnail');
Route::post('/gallery/upload', 'ImageController@uploadGallery');
Route::put('/update/{imageId}', 'ImageController@update'); Route::put('/update/{imageId}', 'ImageController@update');
Route::post('/{type}/upload', 'ImageController@uploadByType');
Route::get('/{type}/all', 'ImageController@getAllByType');
Route::get('/{type}/all/{page}', 'ImageController@getAllByType');
Route::delete('/{imageId}', 'ImageController@destroy'); Route::delete('/{imageId}', 'ImageController@destroy');
}); });

View File

@ -52,15 +52,15 @@ class ImageRepo
/** /**
* Get all images for the standard gallery view that's used for * Gets a load images paginated, filtered by image type.
* adding images to shared content such as pages. * @param string $type
* @param int $page * @param int $page
* @param int $pageSize * @param int $pageSize
* @return array * @return array
*/ */
public function getAllGallery($page = 0, $pageSize = 24) public function getPaginatedByType($type, $page = 0, $pageSize = 24)
{ {
$images = $this->image->where('type', '=', 'gallery') $images = $this->image->where('type', '=', strtolower($type))
->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get(); ->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
$hasMore = count($images) > $pageSize; $hasMore = count($images) > $pageSize;
@ -191,7 +191,7 @@ class ImageRepo
* @param bool $keepRatio * @param bool $keepRatio
* @return string * @return string
*/ */
private function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false) public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
{ {
$thumbDirName = '/' . ($keepRatio ? 'scaled-' : 'thumbs-') . $width . '-' . $height . '/'; $thumbDirName = '/' . ($keepRatio ? 'scaled-' : 'thumbs-') . $width . '-' . $height . '/';
$thumbFilePath = dirname($image->path) . $thumbDirName . basename($image->path); $thumbFilePath = dirname($image->path) . $thumbDirName . basename($image->path);

View File

@ -76,6 +76,22 @@
} }
}, },
props: {
imageType: {
type: String,
required: true
},
resizeWidth: {
type: String
},
resizeHeight: {
type: String
},
resizeCrop: {
type: Boolean
}
},
created: function () { created: function () {
window.ImageManager = this; window.ImageManager = this;
}, },
@ -88,7 +104,7 @@
methods: { methods: {
fetchData: function () { fetchData: function () {
var _this = this; var _this = this;
this.$http.get('/images/gallery/all/' + _this.page, function (data) { this.$http.get('/images/' + _this.imageType + '/all/' + _this.page, function (data) {
_this.images = _this.images.concat(data.images); _this.images = _this.images.concat(data.images);
_this.hasMore = data.hasMore; _this.hasMore = data.hasMore;
_this.page++; _this.page++;
@ -98,7 +114,7 @@
setupDropZone: function () { setupDropZone: function () {
var _this = this; var _this = this;
var dropZone = new Dropzone(_this.$els.dropZone, { var dropZone = new Dropzone(_this.$els.dropZone, {
url: '/images/gallery/upload', url: '/images/' + _this.imageType + '/upload',
init: function () { init: function () {
var dz = this; var dz = this;
this.on("sending", function (file, xhr, data) { this.on("sending", function (file, xhr, data) {
@ -120,6 +136,24 @@
}); });
}, },
returnCallback: function (image) {
var _this = this;
var isResized = _this.resizeWidth && _this.resizeHeight;
if (!isResized) {
_this.callback(image);
return;
}
var cropped = _this.resizeCrop ? 'true' : 'false';
var requestString = '/images/thumb/' + image.id + '/' + _this.resizeWidth + '/' + _this.resizeHeight + '/' + cropped;
_this.$http.get(requestString, function(data) {
image.thumbs.custom = data.url;
_this.callback(image);
});
},
imageClick: function (image) { imageClick: function (image) {
var dblClickTime = 380; var dblClickTime = 380;
var cTime = (new Date()).getTime(); var cTime = (new Date()).getTime();
@ -127,7 +161,7 @@
if (this.cClickTime !== 0 && timeDiff < dblClickTime && this.selectedImage === image) { if (this.cClickTime !== 0 && timeDiff < dblClickTime && this.selectedImage === image) {
// DoubleClick // DoubleClick
if (this.callback) { if (this.callback) {
this.callback(image); this.returnCallback(image);
} }
this.hide(); this.hide();
} else { } else {
@ -139,7 +173,7 @@
selectButtonClick: function () { selectButtonClick: function () {
if (this.callback) { if (this.callback) {
this.callback(this.selectedImage); this.returnCallback(this.selectedImage);
} }
this.hide(); this.hide();
}, },

View File

@ -24,7 +24,7 @@
showImageManager: function(e) { showImageManager: function(e) {
var _this = this; var _this = this;
ImageManager.show(function(image) { ImageManager.show(function(image) {
_this.image = image.url; _this.image = image.thumbs.custom || image.url;
}); });
}, },
reset: function() { reset: function() {

View File

@ -16,5 +16,5 @@
@endif @endif
</form> </form>
</div> </div>
<image-manager></image-manager> <image-manager image-type="gallery"></image-manager>
@stop @stop

View File

@ -14,6 +14,6 @@
@include('pages/form', ['model' => $page]) @include('pages/form', ['model' => $page])
</form> </form>
</div> </div>
<image-manager></image-manager> <image-manager image-type="gallery"></image-manager>
@stop @stop

View File

@ -32,7 +32,7 @@
<div class="col-md-6"> <div class="col-md-6">
<div class="form-group" id="logo-control"> <div class="form-group" id="logo-control">
<label for="setting-app-logo">Application Logo</label> <label for="setting-app-logo">Application Logo</label>
<p class="small">This image should be 43px in height. </p> <p class="small">This image should be 43px in height. <br>Large images will be scaled down.</p>
<image-picker current-image="{{ Setting::get('app-logo', '') }}" default-image="/logo.png" name="setting-app-logo" image-class="logo-image"></image-picker> <image-picker current-image="{{ Setting::get('app-logo', '') }}" default-image="/logo.png" name="setting-app-logo" image-class="logo-image"></image-picker>
</div> </div>
</div> </div>
@ -86,6 +86,6 @@
</div> </div>
<image-manager></image-manager> <image-manager image-type="system" resize-height="43" resize-width="200"></image-manager>
@stop @stop