Images: Updated to create thumbnails at specific events
This commit is contained in:
		
							parent
							
								
									59da7666b5
								
							
						
					
					
						commit
						cc0827ff28
					
				| 
						 | 
					@ -6,6 +6,8 @@ use BookStack\Exceptions\ImageUploadException;
 | 
				
			||||||
use BookStack\Http\Controller;
 | 
					use BookStack\Http\Controller;
 | 
				
			||||||
use BookStack\Uploads\ImageRepo;
 | 
					use BookStack\Uploads\ImageRepo;
 | 
				
			||||||
use Illuminate\Http\Request;
 | 
					use Illuminate\Http\Request;
 | 
				
			||||||
 | 
					use Illuminate\Support\Facades\App;
 | 
				
			||||||
 | 
					use Illuminate\Support\Facades\Log;
 | 
				
			||||||
use Illuminate\Validation\ValidationException;
 | 
					use Illuminate\Validation\ValidationException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class GalleryImageController extends Controller
 | 
					class GalleryImageController extends Controller
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -44,7 +44,7 @@ class ImageController extends Controller
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function update(Request $request, string $id)
 | 
					    public function update(Request $request, string $id)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $this->validate($request, [
 | 
					        $data = $this->validate($request, [
 | 
				
			||||||
            'name' => ['required', 'min:2', 'string'],
 | 
					            'name' => ['required', 'min:2', 'string'],
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -52,9 +52,7 @@ class ImageController extends Controller
 | 
				
			||||||
        $this->checkImagePermission($image);
 | 
					        $this->checkImagePermission($image);
 | 
				
			||||||
        $this->checkOwnablePermission('image-update', $image);
 | 
					        $this->checkOwnablePermission('image-update', $image);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $image = $this->imageRepo->updateImageDetails($image, $request->all());
 | 
					        $image = $this->imageRepo->updateImageDetails($image, $data);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->imageRepo->loadThumbs($image);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return view('pages.parts.image-manager-form', [
 | 
					        return view('pages.parts.image-manager-form', [
 | 
				
			||||||
            'image'          => $image,
 | 
					            'image'          => $image,
 | 
				
			||||||
| 
						 | 
					@ -99,7 +97,7 @@ class ImageController extends Controller
 | 
				
			||||||
            $dependantPages = $this->imageRepo->getPagesUsingImage($image);
 | 
					            $dependantPages = $this->imageRepo->getPagesUsingImage($image);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->imageRepo->loadThumbs($image);
 | 
					        $this->imageRepo->loadThumbs($image, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return view('pages.parts.image-manager-form', [
 | 
					        return view('pages.parts.image-manager-form', [
 | 
				
			||||||
            'image'          => $image,
 | 
					            'image'          => $image,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -130,7 +130,7 @@ class ImageGalleryApiController extends ApiController
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    protected function formatForSingleResponse(Image $image): array
 | 
					    protected function formatForSingleResponse(Image $image): array
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $this->imageRepo->loadThumbs($image);
 | 
					        $this->imageRepo->loadThumbs($image, false);
 | 
				
			||||||
        $data = $image->toArray();
 | 
					        $data = $image->toArray();
 | 
				
			||||||
        $data['created_by'] = $image->createdBy;
 | 
					        $data['created_by'] = $image->createdBy;
 | 
				
			||||||
        $data['updated_by'] = $image->updatedBy;
 | 
					        $data['updated_by'] = $image->updatedBy;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,11 +45,11 @@ class Image extends Model
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Get a thumbnail for this image.
 | 
					     * Get an (already existing) thumbnail for this image.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @throws \Exception
 | 
					     * @throws \Exception
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function getThumb(?int $width, ?int $height, bool $keepRatio = false): string
 | 
					    public function getThumb(?int $width, ?int $height, bool $keepRatio = false): ?string
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return app()->make(ImageService::class)->getThumbnail($this, $width, $height, $keepRatio);
 | 
					        return app()->make(ImageService::class)->getThumbnail($this, $width, $height, $keepRatio);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -29,14 +29,14 @@ class ImageRepo
 | 
				
			||||||
     * Execute a paginated query, returning in a standard format.
 | 
					     * Execute a paginated query, returning in a standard format.
 | 
				
			||||||
     * Also runs the query through the restriction system.
 | 
					     * Also runs the query through the restriction system.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private function returnPaginated($query, $page = 1, $pageSize = 24): array
 | 
					    private function returnPaginated(Builder $query, int $page = 1, int $pageSize = 24): array
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
 | 
					        $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
 | 
				
			||||||
        $hasMore = count($images) > $pageSize;
 | 
					        $hasMore = count($images) > $pageSize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $returnImages = $images->take($pageSize);
 | 
					        $returnImages = $images->take($pageSize);
 | 
				
			||||||
        $returnImages->each(function (Image $image) {
 | 
					        $returnImages->each(function (Image $image) {
 | 
				
			||||||
            $this->loadThumbs($image);
 | 
					            $this->loadThumbs($image, false);
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return [
 | 
					        return [
 | 
				
			||||||
| 
						 | 
					@ -119,7 +119,7 @@ class ImageRepo
 | 
				
			||||||
        $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
 | 
					        $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($type !== 'system') {
 | 
					        if ($type !== 'system') {
 | 
				
			||||||
            $this->loadThumbs($image);
 | 
					            $this->loadThumbs($image, true);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $image;
 | 
					        return $image;
 | 
				
			||||||
| 
						 | 
					@ -133,7 +133,7 @@ class ImageRepo
 | 
				
			||||||
    public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
 | 
					    public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
 | 
					        $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
 | 
				
			||||||
        $this->loadThumbs($image);
 | 
					        $this->loadThumbs($image, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $image;
 | 
					        return $image;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -160,7 +160,7 @@ class ImageRepo
 | 
				
			||||||
        $image->fill($updateDetails);
 | 
					        $image->fill($updateDetails);
 | 
				
			||||||
        $image->updated_by = user()->id;
 | 
					        $image->updated_by = user()->id;
 | 
				
			||||||
        $image->save();
 | 
					        $image->save();
 | 
				
			||||||
        $this->loadThumbs($image);
 | 
					        $this->loadThumbs($image, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $image;
 | 
					        return $image;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -179,6 +179,7 @@ class ImageRepo
 | 
				
			||||||
        $image->updated_by = user()->id;
 | 
					        $image->updated_by = user()->id;
 | 
				
			||||||
        $image->touch();
 | 
					        $image->touch();
 | 
				
			||||||
        $image->save();
 | 
					        $image->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->imageService->replaceExistingFromUpload($image->path, $image->type, $file);
 | 
					        $this->imageService->replaceExistingFromUpload($image->path, $image->type, $file);
 | 
				
			||||||
        $this->loadThumbs($image, true);
 | 
					        $this->loadThumbs($image, true);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -215,11 +216,11 @@ class ImageRepo
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Load thumbnails onto an image object.
 | 
					     * Load thumbnails onto an image object.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function loadThumbs(Image $image, bool $forceCreate = false): void
 | 
					    public function loadThumbs(Image $image, bool $shouldCreate): void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $image->setAttribute('thumbs', [
 | 
					        $image->setAttribute('thumbs', [
 | 
				
			||||||
            'gallery' => $this->getThumbnail($image, 150, 150, false, $forceCreate),
 | 
					            'gallery' => $this->getThumbnail($image, 150, 150, false, $shouldCreate),
 | 
				
			||||||
            'display' => $this->getThumbnail($image, 1680, null, true, $forceCreate),
 | 
					            'display' => $this->getThumbnail($image, 1680, null, true, $shouldCreate),
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -228,10 +229,10 @@ class ImageRepo
 | 
				
			||||||
     * If $keepRatio is true only the width will be used.
 | 
					     * If $keepRatio is true only the width will be used.
 | 
				
			||||||
     * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
 | 
					     * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio, bool $forceCreate): ?string
 | 
					    protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio, bool $shouldCreate): ?string
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            return $this->imageService->getThumbnail($image, $width, $height, $keepRatio, $forceCreate);
 | 
					            return $this->imageService->getThumbnail($image, $width, $height, $keepRatio, $shouldCreate);
 | 
				
			||||||
        } catch (Exception $exception) {
 | 
					        } catch (Exception $exception) {
 | 
				
			||||||
            return null;
 | 
					            return null;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -259,7 +259,7 @@ class ImageService
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $initialHeader = substr($imageData, 0, strpos($imageData, 'IDAT'));
 | 
					        $initialHeader = substr($imageData, 0, strpos($imageData, 'IDAT'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return strpos($initialHeader, 'acTL') !== false;
 | 
					        return str_contains($initialHeader, 'acTL');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
| 
						 | 
					@ -268,9 +268,8 @@ class ImageService
 | 
				
			||||||
     * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
 | 
					     * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @throws Exception
 | 
					     * @throws Exception
 | 
				
			||||||
     * @throws InvalidArgumentException
 | 
					 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio = false, bool $forceCreate = false): string
 | 
					    public function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio = false, bool $shouldCreate = false): ?string
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // Do not resize GIF images where we're not cropping
 | 
					        // Do not resize GIF images where we're not cropping
 | 
				
			||||||
        if ($keepRatio && $this->isGif($image)) {
 | 
					        if ($keepRatio && $this->isGif($image)) {
 | 
				
			||||||
| 
						 | 
					@ -285,13 +284,13 @@ class ImageService
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Return path if in cache
 | 
					        // Return path if in cache
 | 
				
			||||||
        $cachedThumbPath = $this->cache->get($thumbCacheKey);
 | 
					        $cachedThumbPath = $this->cache->get($thumbCacheKey);
 | 
				
			||||||
        if ($cachedThumbPath && !$forceCreate) {
 | 
					        if ($cachedThumbPath && !$shouldCreate) {
 | 
				
			||||||
            return $this->getPublicUrl($cachedThumbPath);
 | 
					            return $this->getPublicUrl($cachedThumbPath);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // If thumbnail has already been generated, serve that and cache path
 | 
					        // If thumbnail has already been generated, serve that and cache path
 | 
				
			||||||
        $storage = $this->getStorageDisk($image->type);
 | 
					        $storage = $this->getStorageDisk($image->type);
 | 
				
			||||||
        if (!$forceCreate && $storage->exists($this->adjustPathForStorageDisk($thumbFilePath, $image->type))) {
 | 
					        if (!$shouldCreate && $storage->exists($this->adjustPathForStorageDisk($thumbFilePath, $image->type))) {
 | 
				
			||||||
            $this->cache->put($thumbCacheKey, $thumbFilePath, 60 * 60 * 72);
 | 
					            $this->cache->put($thumbCacheKey, $thumbFilePath, 60 * 60 * 72);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return $this->getPublicUrl($thumbFilePath);
 | 
					            return $this->getPublicUrl($thumbFilePath);
 | 
				
			||||||
| 
						 | 
					@ -306,6 +305,10 @@ class ImageService
 | 
				
			||||||
            return $this->getPublicUrl($image->path);
 | 
					            return $this->getPublicUrl($image->path);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!$shouldCreate) {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // If not in cache and thumbnail does not exist, generate thumb and cache path
 | 
					        // If not in cache and thumbnail does not exist, generate thumb and cache path
 | 
				
			||||||
        $thumbData = $this->resizeImage($imageData, $width, $height, $keepRatio);
 | 
					        $thumbData = $this->resizeImage($imageData, $width, $height, $keepRatio);
 | 
				
			||||||
        $this->saveImageDataInPublicSpace($storage, $this->adjustPathForStorageDisk($thumbFilePath, $image->type), $thumbData);
 | 
					        $this->saveImageDataInPublicSpace($storage, $this->adjustPathForStorageDisk($thumbFilePath, $image->type), $thumbData);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue