]> BookStack Code Mirror - bookstack/commitdiff
Added mulit image-type compatability to manager & app and added scaled image selection
authorDan Brown <redacted>
Tue, 8 Dec 2015 22:04:59 +0000 (22:04 +0000)
committerDan Brown <redacted>
Tue, 8 Dec 2015 22:04:59 +0000 (22:04 +0000)
app/Http/Controllers/ImageController.php
app/Http/Controllers/UserController.php
app/Http/routes.php
app/Repos/ImageRepo.php
resources/assets/js/components/image-manager.vue
resources/assets/js/components/image-picker.vue
resources/views/pages/create.blade.php
resources/views/pages/edit.blade.php
resources/views/settings/index.blade.php

index 7e7fedfdecb4c3b9c6435bb3ea327d4278e0e8d9..23f5446d65982ff0acfc8b1b5c2b6039ed75b5b9 100644 (file)
@@ -33,23 +33,24 @@ class ImageController extends Controller
 
 
     /**
-     * Get all images, Paginated
+     * Get all gallery images, Paginated
      * @param int $page
      * @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);
     }
 
 
     /**
      * Handles image uploads for use on pages.
+     * @param string  $type
      * @param Request $request
      * @return \Illuminate\Http\JsonResponse
      */
-    public function uploadGallery(Request $request)
+    public function uploadByType($type, Request $request)
     {
         $this->checkPermission('image-create');
         $this->validate($request, [
@@ -57,10 +58,25 @@ class ImageController extends Controller
         ]);
 
         $imageUpload = $request->file('file');
-        $image = $this->imageRepo->saveNew($imageUpload, 'gallery');
+        $image = $this->imageRepo->saveNew($imageUpload, $type);
         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
index ed9deab2da824aecfe8228da5b29822cd8b47622..d1c328b86a9a01e03798d8d6c2d43bf12dea4444 100644 (file)
@@ -18,7 +18,8 @@ class UserController extends Controller
 
     /**
      * UserController constructor.
-     * @param $user
+     * @param User     $user
+     * @param UserRepo $userRepo
      */
     public function __construct(User $user, UserRepo $userRepo)
     {
index 5b1e245a15664acd41a738dafd88c91a711d097a..274fccbff7e55a64150c5e12719b656a301fcc53 100644 (file)
@@ -57,10 +57,12 @@ Route::group(['middleware' => 'auth'], function () {
 
     // Image routes
     Route::group(['prefix' => 'images'], function() {
-        Route::get('/gallery/all', 'ImageController@getAllGallery');
-        Route::get('/gallery/all/{page}', 'ImageController@getAllGallery');
-        Route::post('/gallery/upload', 'ImageController@uploadGallery');
+        // Standard get, update and deletion for all types
+        Route::get('/thumb/{id}/{width}/{height}/{crop}', 'ImageController@getThumbnail');
         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');
     });
 
index 2699b9e971cb7a0d3b9a21f97b9bed03fcb4b1c3..0da243f7c8e2d9a4b3eb18aba3b931276302f06e 100644 (file)
@@ -52,15 +52,15 @@ class ImageRepo
 
 
     /**
-     * Get all images for the standard gallery view that's used for
-     * adding images to shared content such as pages.
-     * @param int $page
-     * @param int $pageSize
+     * Gets a load images paginated, filtered by image type.
+     * @param string $type
+     * @param int    $page
+     * @param int    $pageSize
      * @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();
         $hasMore = count($images) > $pageSize;
 
@@ -191,7 +191,7 @@ class ImageRepo
      * @param bool  $keepRatio
      * @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 . '/';
         $thumbFilePath = dirname($image->path) . $thumbDirName . basename($image->path);
index bb691760b92ae654feec8caef1f62772bce873ee..c47fee184dac95ae79696dbe19e823a997d10528 100644 (file)
             }
         },
 
+        props: {
+            imageType: {
+                type: String,
+                required: true
+            },
+            resizeWidth: {
+                type: String
+            },
+            resizeHeight: {
+                type: String
+            },
+            resizeCrop: {
+                type: Boolean
+            }
+        },
+
         created: function () {
             window.ImageManager = this;
         },
         methods: {
             fetchData: function () {
                 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.hasMore = data.hasMore;
                     _this.page++;
             setupDropZone: function () {
                 var _this = this;
                 var dropZone = new Dropzone(_this.$els.dropZone, {
-                    url: '/images/gallery/upload',
+                    url: '/images/' + _this.imageType + '/upload',
                     init: function () {
                         var dz = this;
                         this.on("sending", function (file, xhr, data) {
                 });
             },
 
+            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) {
                 var dblClickTime = 380;
                 var cTime = (new Date()).getTime();
                 if (this.cClickTime !== 0 && timeDiff < dblClickTime && this.selectedImage === image) {
                     // DoubleClick
                     if (this.callback) {
-                        this.callback(image);
+                        this.returnCallback(image);
                     }
                     this.hide();
                 } else {
 
             selectButtonClick: function () {
                 if (this.callback) {
-                    this.callback(this.selectedImage);
+                    this.returnCallback(this.selectedImage);
                 }
                 this.hide();
             },
index a52cd36616cb221bbf377d723570a2cf4071fd3a..4318ea1f08269860f13487bcc0f374761ab12bdb 100644 (file)
@@ -24,7 +24,7 @@
             showImageManager: function(e) {
                 var _this = this;
                 ImageManager.show(function(image) {
-                    _this.image = image.url;
+                    _this.image = image.thumbs.custom || image.url;
                 });
             },
             reset: function() {
index 4399de854580fc4c623406263ef1646b4fbb4f93..6db60a0b914127352ecedc8decd2fc94baed17d4 100644 (file)
@@ -16,5 +16,5 @@
             @endif
         </form>
     </div>
-    <image-manager></image-manager>
+    <image-manager image-type="gallery"></image-manager>
 @stop
\ No newline at end of file
index 7261da8f5dd22834c121af115572d4c49b083d0d..d89daf250c6224ef7f21cce1678de4a89be7da56 100644 (file)
@@ -14,6 +14,6 @@
             @include('pages/form', ['model' => $page])
         </form>
     </div>
-    <image-manager></image-manager>
+    <image-manager image-type="gallery"></image-manager>
 
 @stop
\ No newline at end of file
index ebf5381983bb81a231ce6a34c3dbdb3735a6357d..759bb16786d7e727ae5c36e65f0aef5ce321b4a7 100644 (file)
@@ -32,7 +32,7 @@
             <div class="col-md-6">
                 <div class="form-group" id="logo-control">
                     <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>
                 </div>
             </div>
@@ -86,6 +86,6 @@
 
 </div>
 
-<image-manager></image-manager>
+<image-manager image-type="system" resize-height="43" resize-width="200"></image-manager>
 
 @stop
Morty Proxy This is a proxified and sanitized view of the page, visit original site.