1 <?php namespace BookStack\Repos;
5 use BookStack\Services\ImageService;
6 use BookStack\Services\RestrictionService;
8 use Symfony\Component\HttpFoundation\File\UploadedFile;
14 protected $imageService;
15 protected $restictionService;
18 * ImageRepo constructor.
20 * @param ImageService $imageService
21 * @param RestrictionService $restrictionService
23 public function __construct(Image $image, ImageService $imageService, RestrictionService $restrictionService)
25 $this->image = $image;
26 $this->imageService = $imageService;
27 $this->restictionService = $restrictionService;
32 * Get an image with the given id.
36 public function getById($id)
38 return $this->image->findOrFail($id);
42 * Gets a load images paginated, filtered by image type.
45 * @param int $pageSize
46 * @param bool|int $userFilter
49 public function getPaginatedByType($type, $page = 0, $pageSize = 24, $userFilter = false)
51 $images = $this->image->where('type', '=', strtolower($type));
53 if ($userFilter !== false) {
54 $images = $images->where('created_by', '=', $userFilter);
57 $images = $this->restictionService->filterRelatedPages($images, 'images', 'uploaded_to');
58 $images = $images->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
59 $hasMore = count($images) > $pageSize;
61 $returnImages = $images->take(24);
62 $returnImages->each(function ($image) {
63 $this->loadThumbs($image);
67 'images' => $returnImages,
73 * Save a new image into storage and return the new image.
74 * @param UploadedFile $uploadFile
76 * @param int $uploadedTo
79 public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0)
81 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo);
82 $this->loadThumbs($image);
87 * Update the details of an image via an array of properties.
89 * @param array $updateDetails
92 public function updateImageDetails(Image $image, $updateDetails)
94 $image->fill($updateDetails);
96 $this->loadThumbs($image);
102 * Destroys an Image object along with its files and thumbnails.
103 * @param Image $image
106 public function destroyImage(Image $image)
108 $this->imageService->destroyImage($image);
114 * Load thumbnails onto an image object.
115 * @param Image $image
117 private function loadThumbs(Image $image)
120 'gallery' => $this->getThumbnail($image, 150, 150),
121 'display' => $this->getThumbnail($image, 840, 0, true)
126 * Get the thumbnail for an image.
127 * If $keepRatio is true only the width will be used.
128 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
130 * @param Image $image
133 * @param bool $keepRatio
136 public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
138 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);