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
45 public function getPaginatedByType($type, $page = 0, $pageSize = 24)
47 $images = $this->image->where('type', '=', strtolower($type))
48 ->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
49 $hasMore = count($images) > $pageSize;
51 $returnImages = $images->take(24);
52 $returnImages->each(function ($image) {
53 $this->loadThumbs($image);
57 'images' => $returnImages,
63 * Save a new image into storage and return the new image.
64 * @param UploadedFile $uploadFile
68 public function saveNew(UploadedFile $uploadFile, $type)
70 $image = $this->imageService->saveNew($this->image, $uploadFile, $type);
71 $this->loadThumbs($image);
76 * Update the details of an image via an array of properties.
78 * @param array $updateDetails
81 public function updateImageDetails(Image $image, $updateDetails)
83 $image->fill($updateDetails);
85 $this->loadThumbs($image);
91 * Destroys an Image object along with its files and thumbnails.
95 public function destroyImage(Image $image)
97 $this->imageService->destroyImage($image);
103 * Load thumbnails onto an image object.
104 * @param Image $image
106 private function loadThumbs(Image $image)
109 'gallery' => $this->getThumbnail($image, 150, 150),
110 'display' => $this->getThumbnail($image, 840, 0, true)
115 * Get the thumbnail for an image.
116 * If $keepRatio is true only the width will be used.
117 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
119 * @param Image $image
122 * @param bool $keepRatio
125 public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
127 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);