]> BookStack Code Mirror - bookstack/blob - app/Repos/ImageRepo.php
Added basic system tests for markdown editor, Added extra test helpers
[bookstack] / app / Repos / ImageRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use BookStack\Image;
5 use BookStack\Services\ImageService;
6 use BookStack\Services\RestrictionService;
7 use Setting;
8 use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10 class ImageRepo
11 {
12
13     protected $image;
14     protected $imageService;
15     protected $restictionService;
16
17     /**
18      * ImageRepo constructor.
19      * @param Image $image
20      * @param ImageService $imageService
21      * @param RestrictionService $restrictionService
22      */
23     public function __construct(Image $image, ImageService $imageService, RestrictionService $restrictionService)
24     {
25         $this->image = $image;
26         $this->imageService = $imageService;
27         $this->restictionService = $restrictionService;
28     }
29
30
31     /**
32      * Get an image with the given id.
33      * @param $id
34      * @return mixed
35      */
36     public function getById($id)
37     {
38         return $this->image->findOrFail($id);
39     }
40
41     /**
42      * Gets a load images paginated, filtered by image type.
43      * @param string $type
44      * @param int $page
45      * @param int $pageSize
46      * @param bool|int $userFilter
47      * @return array
48      */
49     public function getPaginatedByType($type, $page = 0, $pageSize = 24, $userFilter = false)
50     {
51         $images = $this->image->where('type', '=', strtolower($type));
52
53         if ($userFilter !== false) {
54             $images = $images->where('created_by', '=', $userFilter);
55         }
56
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;
60
61         $returnImages = $images->take(24);
62         $returnImages->each(function ($image) {
63             $this->loadThumbs($image);
64         });
65
66         return [
67             'images'  => $returnImages,
68             'hasMore' => $hasMore
69         ];
70     }
71
72     /**
73      * Save a new image into storage and return the new image.
74      * @param UploadedFile $uploadFile
75      * @param  string $type
76      * @param int $uploadedTo
77      * @return Image
78      */
79     public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0)
80     {
81         $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo);
82         $this->loadThumbs($image);
83         return $image;
84     }
85
86     /**
87      * Update the details of an image via an array of properties.
88      * @param Image $image
89      * @param array $updateDetails
90      * @return Image
91      */
92     public function updateImageDetails(Image $image, $updateDetails)
93     {
94         $image->fill($updateDetails);
95         $image->save();
96         $this->loadThumbs($image);
97         return $image;
98     }
99
100
101     /**
102      * Destroys an Image object along with its files and thumbnails.
103      * @param Image $image
104      * @return bool
105      */
106     public function destroyImage(Image $image)
107     {
108         $this->imageService->destroyImage($image);
109         return true;
110     }
111
112
113     /**
114      * Load thumbnails onto an image object.
115      * @param Image $image
116      */
117     private function loadThumbs(Image $image)
118     {
119         $image->thumbs = [
120             'gallery' => $this->getThumbnail($image, 150, 150),
121             'display' => $this->getThumbnail($image, 840, 0, true)
122         ];
123     }
124
125     /**
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.
129      *
130      * @param Image $image
131      * @param int $width
132      * @param int $height
133      * @param bool $keepRatio
134      * @return string
135      */
136     public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
137     {
138         return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
139     }
140
141
142 }