8000 refactor thumbnails related functions · pacebale/laravel-filemanager@7bedc5a · GitHub
[go: up one dir, main page]

Skip to content

Commit 7bedc5a

Browse files
committed
refactor thumbnails related functions
1 parent b0bc46a commit 7bedc5a

File tree

4 files changed

+47
-35
lines changed

4 files changed

+47
-35
lines changed

src/LfmItem.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,35 @@ public function hasThumb()
153153
return false;
154154
}
155155

156-
if (in_array($this->mimeType(), ['image/gif', 'image/svg+xml'])) {
156+
if (!$this->lfm->thumb()->exists()) {
157157
return false;
158158
}
159159

160-
if (!$this->lfm->thumb()->exists()) {
160+
return true;
161+
}
162+
163+
public function shouldCreateThumb()
164+
{
165+
if (!$this->helper->config('should_create_thumbnails')) {
166+
return false;
167+
}
168+
169+
if (!$this->isImage()) {
170+
return false;
171+
}
172+
173+
if (in_array($this->mimeType(), ['image/gif', 'image/svg+xml'])) {
161174
return false;
162175
}
163176

164177
return true;
165178
}
166179

180+
public function get()
181+
{
182+
return $this->lfm->get();
183+
}
184+
167185
/**
168186
* Make file size readable.
169187
*

src/LfmPath.php

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -201,20 +201,20 @@ public function error($error_type, $variables = [])
201201
public function upload($file)
202202
{
203203
$this->uploadValidator($file);
204-
$new_filename = $this->getNewName($file);
205-
$new_file_path = $this->setName($new_filename)->path('absolute');
204+
$new_file_name = $this->getNewName($file);
205+
$new_file_path = $this->setName($new_file_name)->path('absolute');
206206

207207
event(new ImageIsUploading($new_file_path));
208208
try {
209-
$new_filename = $this->saveFile($file, $new_filename);
209+
$new_file_name = $this->saveFile($file, $new_file_name);
210210
} catch (\Exception $e) {
211211
\Log::info($e);
212212
return $this->error('invalid');
213213
}
214214
// TODO should be "FileWasUploaded"
215215
event(new ImageWasUploaded($new_file_path));
216216

217-
return $new_filename;
217+
return $new_file_name;
218218
}
219219

220220
private function uploadValidator($file)
@@ -229,9 +229,9 @@ private function uploadValidator($file)
229229
throw new \Exception('File failed to upload. Error code: ' . $file->getError());
230230
}
231231

232-
$new_filename = $this->getNewName($file) . '.' . $file->getClientOriginalExtension();
232+
$new_file_name = $this->getNewName($file) . '.' . $file->getClientOriginalExtension();
233233

234-
if ($this->setName($new_filename)->exists()) {
234+
if ($this->setName($new_file_name)->exists()) {
235235
return $this->error('file-exist');
236236
}
237237

@@ -255,54 +255,48 @@ private function uploadValidator($file)
255255

256256
private function getNewName($file)
257257
{
258-
$new_filename = $this->helper->translateFromUtf8(trim(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)));
258+
$new_file_name = $this->helper->translateFromUtf8(trim(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)));
259259

260260
if (config(' 10000 lfm.rename_file') === true) {
261-
$new_filename = uniqid();
261+
$new_file_name = uniqid();
262262
} elseif (config('lfm.alphanumeric_filename') === true) {
263-
$new_filename = preg_replace('/[^A-Za-z0-9\-\']/', '_', $new_filename);
263+
$new_file_name = preg_replace('/[^A-Za-z0-9\-\']/', '_', $new_file_name);
264264
}
265265

266266
$extension = $file->getClientOriginalExtension();
267267

268268
if ($extension) {
269-
$new_filename .= '.' . $extension;
269+
$new_file_name .= '.' . $extension;
270270
}
271271

272-
return $new_filename;
272+
return $new_file_name;
273273
}
274274

275-
private function saveFile($file, $new_filename)
275+
private function saveFile($file, $new_file_name)
276276
{
277-
$should_create_thumbnail = $this->shouldCreateThumb($file);
277+
$this->setName($new_file_name)->storage->save(file_get_contents($file));
278278

279-
$this->setName($new_filename)->thumb(false)->storage->save(file_get_contents($file));
279+
$this->makeThumbnail($new_file_name);
280280

281-
if ($should_create_thumbnail) {
282-
$this->makeThumbnail($new_filename);
283-
}
284-
285-
return $new_filename;
281+
return $new_file_name;
286282
}
287283

288-
public function makeThumbnail($filename)
284+
public function makeThumbnail($file_name)
289285
{
286+
$original_image = $this->pretty($file_name);
287+
288+
if (!$original_image->shouldCreateThumb()) {
289+
return;
290+
}
291+
290292
// create folder for thumbnails
291293
$this->setName(null)->thumb(true)->createFolder();
292294

293-
$image_content = $this->thumb(false)->setName($filename)->get();
294-
295-
// generate cropped thumbnail
296-
$image = Image::make($image_content)
295+
// generate cropped image content
296+
$image_content = Image::make($original_image->get())
297297
->fit(config('lfm.thumb_img_width', 200), config('lfm.thumb_img_height', 200))
298298
->encode();
299299

300-
$this->setName($filename)->thumb(true)->storage->save($image);
301-
}
302-
303-
private function shouldCreateThumb($file)
304-
{
305-
return starts_with($file->getMimeType(), 'image')
306-
&& !in_array($file->getMimeType(), ['image/gif', 'image/svg+xml']);
300+
$this->setName($file_name)->thumb(true)->storage->save($image_content);
307301
}
308302
}

src/LfmStorageRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function url($path)
5454

5555
public function makeDirectory()
5656
{
57-
$this->disk->makeDirectory($this->path, ...$arguments);
57+
$this->disk->makeDirectory($this->path, ...func_get_args());
5858

5959
$this->disk->setVisibility($this->path, 'public');
6060
}

src/views/crop.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="row">
22
<div class="col-md-8">
33
<div class="crop-container">
4-
<img src="{{ $img->path . '?timestamp=' . $img->time }}" class="img img-responsive">
4+
<img src="{{ $img->url . '?timestamp=' . $img->time }}" class="img img-responsive">
55
</div>
66
</div>
77
<div class="col-md-4">

0 commit comments

Comments
 (0)
0