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