1 | <?php namespace Modules\Media\Image; |
||
7 | class Imagy |
||
8 | { |
||
9 | /** |
||
10 | * @var \Intervention\Image\Image |
||
11 | */ |
||
12 | private $image; |
||
13 | /** |
||
14 | * @var \Illuminate\Filesystem\Filesystem |
||
15 | */ |
||
16 | private $finder; |
||
17 | /** |
||
18 | * @var ImageFactoryInterface |
||
19 | */ |
||
20 | private $imageFactory; |
||
21 | /** |
||
22 | * @var ThumbnailsManager |
||
23 | */ |
||
24 | private $manager; |
||
25 | |||
26 | /** |
||
27 | * All the different images types where thumbnails should be created |
||
28 | * @var array |
||
29 | */ |
||
30 | private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif']; |
||
31 | /** |
||
32 | * @var Repository |
||
33 | */ |
||
34 | private $config; |
||
35 | |||
36 | /** |
||
37 | * @param ImageFactoryInterface $imageFactory |
||
38 | * @param ThumbnailsManager $manager |
||
39 | * @param Repository $config |
||
40 | */ |
||
41 | public function __construct(ImageFactoryInterface $imageFactory, ThumbnailsManager $manager, Repository $config) |
||
49 | |||
50 | /** |
||
51 | * Get an image in the given thumbnail options |
||
52 | * @param string $path |
||
53 | * @param string $thumbnail |
||
54 | * @param bool $forceCreate |
||
55 | * @return string |
||
56 | */ |
||
57 | public function get($path, $thumbnail, $forceCreate = false) |
||
73 | |||
74 | /** |
||
75 | * Return the thumbnail path |
||
76 | * @param string $originalImage |
||
77 | * @param string $thumbnail |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getThumbnail($originalImage, $thumbnail) |
||
88 | |||
89 | /** |
||
90 | * Create all thumbnails for the given image path |
||
91 | * @param string $path |
||
92 | */ |
||
93 | public function createAll($path) |
||
94 | { |
||
95 | if (!$this->isImage($path)) { |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | foreach ($this->manager->all() as $thumbnail) { |
||
100 | $image = $this->image->make(public_path() . $path); |
||
101 | $filename = $this->config->get('asgard.media.config.files-path') . $this->newFilename($path, $thumbnail->name()); |
||
102 | foreach ($thumbnail->filters() as $manipulation => $options) { |
||
103 | $image = $this->imageFactory->make($manipulation)->handle($image, $options); |
||
104 | } |
||
105 | $image = $image->encode(pathinfo($path, PATHINFO_EXTENSION)); |
||
106 | $this->writeImage($filename, $image); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Prepend the thumbnail name to filename |
||
112 | * @param $path |
||
113 | * @param $thumbnail |
||
114 | * @return mixed|string |
||
115 | */ |
||
116 | private function newFilename($path, $thumbnail) |
||
122 | |||
123 | /** |
||
124 | * Return the already created file if it exists and force create is false |
||
125 | * @param string $filename |
||
126 | * @param bool $forceCreate |
||
127 | * @return bool |
||
128 | */ |
||
129 | private function returnCreatedFile($filename, $forceCreate) |
||
133 | |||
134 | /** |
||
135 | * Write the given image |
||
136 | * @param string $filename |
||
137 | * @param string $image |
||
138 | */ |
||
139 | private function writeImage($filename, $image) |
||
144 | |||
145 | /** |
||
146 | * Make a new image |
||
147 | * @param string $path |
||
148 | * @param string $filename |
||
149 | * @param string null $thumbnail |
||
150 | */ |
||
151 | private function makeNew($path, $filename, $thumbnail) |
||
163 | |||
164 | /** |
||
165 | * Check if the given path is en image |
||
166 | * @param string $path |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function isImage($path) |
||
173 | |||
174 | /** |
||
175 | * Delete all files on disk for the given file in storage |
||
176 | * This means the original and the thumbnails |
||
177 | * @param $file |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function deleteAllFor(File $file) |
||
197 | } |
||
198 |
If you suppress an error, we recommend checking for the error condition explicitly: