|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniSharp\LaravelFilemanager\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Intervention\Image\Facades\Image; |
|
6
|
|
|
use UniSharp\LaravelFilemanager\Events\ImageIsResizing; |
|
7
|
|
|
use UniSharp\LaravelFilemanager\Events\ImageWasResized; |
|
8
|
|
|
|
|
9
|
|
|
class ResizeController extends LfmController |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Dipsplay image for resizing. |
|
13
|
|
|
* |
|
14
|
|
|
* @return mixed |
|
15
|
|
|
*/ |
|
16
|
|
|
public function getResize() |
|
17
|
|
|
{ |
|
18
|
|
|
$ratio = 1.0; |
|
19
|
|
|
$image = request('img'); |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
$original_image = Image::make($this->lfm->setName($image)->path('absolute')); |
|
|
|
|
|
|
22
|
|
|
$original_width = $original_image->width(); |
|
23
|
|
|
$original_height = $original_image->height(); |
|
24
|
|
|
|
|
25
|
|
|
$scaled = false; |
|
26
|
|
|
|
|
27
|
|
|
// FIXME size should be configurable |
|
28
|
|
|
if ($original_width > 600) { |
|
29
|
|
|
$ratio = 600 / $original_width; |
|
30
|
|
|
$width = $original_width * $ratio; |
|
31
|
|
|
$height = $original_height * $ratio; |
|
32
|
|
|
$scaled = true; |
|
33
|
|
|
} else { |
|
34
|
|
|
$width = $original_width; |
|
35
|
|
|
$height = $original_height; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($height > 400) { |
|
39
|
|
|
$ratio = 400 / $original_height; |
|
40
|
|
|
$width = $original_width * $ratio; |
|
41
|
|
|
$height = $original_height * $ratio; |
|
42
|
|
|
$scaled = true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return view('laravel-filemanager::resize') |
|
|
|
|
|
|
46
|
|
|
->with('img', $this->lfm->pretty($image)) |
|
47
|
|
|
->with('height', number_format($height, 0)) |
|
48
|
|
|
->with('width', $width) |
|
49
|
|
|
->with('original_height', $original_height) |
|
50
|
|
|
->with('original_width', $original_width) |
|
51
|
|
|
->with('scaled', $scaled) |
|
52
|
|
|
->with('ratio', $ratio); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function performResize($overWrite = true) |
|
56
|
|
|
{ |
|
57
|
|
|
$image_name = request('img'); |
|
|
|
|
|
|
58
|
|
|
$image_path = $this->lfm->setName(request('img'))->path('absolute'); |
|
|
|
|
|
|
59
|
|
|
$resize_path = $image_path; |
|
60
|
|
|
|
|
61
|
|
|
if (! $overWrite) { |
|
62
|
|
|
$fileParts = explode('.', $image_name); |
|
63
|
|
|
$fileParts[count($fileParts) - 2] = $fileParts[count($fileParts) - 2] . '_resized_' . time(); |
|
64
|
|
|
$resize_path = $this->lfm->setName(implode('.', $fileParts))->path('absolute'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
event(new ImageIsResizing($image_path)); |
|
|
|
|
|
|
68
|
|
|
Image::make($image_path)->resize(request('dataWidth'), request('dataHeight'))->save($resize_path); |
|
69
|
|
|
event(new ImageWasResized($image_path)); |
|
70
|
|
|
|
|
71
|
|
|
return parent::$success_response; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function performResizeNew() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->performResize(false); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|