1 <?php namespace BookStack\Repos;
5 use BookStack\Services\ImageService;
7 use Symfony\Component\HttpFoundation\File\UploadedFile;
13 protected $imageService;
16 * ImageRepo constructor.
18 * @param ImageService $imageService
20 public function __construct(Image $image, ImageService $imageService)
22 $this->image = $image;
23 $this->imageService = $imageService;
28 * Get an image with the given id.
32 public function getById($id)
34 return $this->image->findOrFail($id);
39 * Gets a load images paginated, filtered by image type.
42 * @param int $pageSize
43 * @param bool|int $userFilter
46 public function getPaginatedByType($type, $page = 0, $pageSize = 24, $userFilter = false)
48 $images = $this->image->where('type', '=', strtolower($type));
50 if ($userFilter !== false) {
51 $images = $images->where('created_by', '=', $userFilter);
54 $images = $images->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
55 $hasMore = count($images) > $pageSize;
57 $returnImages = $images->take(24);
58 $returnImages->each(function ($image) {
59 $this->loadThumbs($image);
63 'images' => $returnImages,
69 * Save a new image into storage and return the new image.
70 * @param UploadedFile $uploadFile
74 public function saveNew(UploadedFile $uploadFile, $type)
76 $image = $this->imageService->saveNewFromUpload($uploadFile, $type);
77 $this->loadThumbs($image);
82 * Update the details of an image via an array of properties.
84 * @param array $updateDetails
87 public function updateImageDetails(Image $image, $updateDetails)
89 $image->fill($updateDetails);
91 $this->loadThumbs($image);
97 * Destroys an Image object along with its files and thumbnails.
101 public function destroyImage(Image $image)
103 $this->imageService->destroyImage($image);
109 * Load thumbnails onto an image object.
110 * @param Image $image
112 private function loadThumbs(Image $image)
115 'gallery' => $this->getThumbnail($image, 150, 150),
116 'display' => $this->getThumbnail($image, 840, 0, true)
121 * Get the thumbnail for an image.
122 * If $keepRatio is true only the width will be used.
123 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
125 * @param Image $image
128 * @param bool $keepRatio
131 public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
133 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);