|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Approached\LaravelImageOptimizer\ImageOptimizer; |
|
6
|
|
|
use File; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Support\Facades\Storage; |
|
10
|
|
|
use Intervention\Image\Facades\Image; |
|
11
|
|
|
|
|
12
|
|
|
class Optimizer extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
const EXTENSION_DOT_LENGTH = -4; |
|
16
|
|
|
|
|
17
|
|
|
public function resizepost(Request $request, ImageOptimizer $imageOptimizer) |
|
18
|
|
|
{ |
|
19
|
|
|
try { |
|
20
|
|
|
$pic = $request->file('picture'); |
|
21
|
|
|
$width = ($request->get('width')) ? $request->get('width') : 640; |
|
22
|
|
|
$height = ($request->get('height')) ? $request->get('height') : null; |
|
23
|
|
|
$imageOptimizer->optimizeUploadedImageFile($pic); |
|
|
|
|
|
|
24
|
|
|
$newName = substr_replace($pic->getClientOriginalName(), |
|
25
|
|
|
$this->getName($width, $height) . '.' . $pic->getClientOriginalExtension(), self::EXTENSION_DOT_LENGTH); |
|
26
|
|
|
Storage::put('compressed/' . $newName, File::get($pic)); |
|
|
|
|
|
|
27
|
|
|
$img = Image::make(storage_path('app/compressed/') . $newName); |
|
28
|
|
|
$resize = $img->resize($width, $height, function ($constraint) { |
|
29
|
|
|
$constraint->aspectRatio(); |
|
30
|
|
|
$constraint->upsize(); |
|
31
|
|
|
}); |
|
32
|
|
|
if ($resize) { |
|
33
|
|
|
$img->save(storage_path('app/resized/') . $newName); |
|
34
|
|
|
unlink(storage_path('app/compressed/') . $newName); |
|
35
|
|
|
return $img->response(); |
|
36
|
|
|
} |
|
37
|
|
|
} catch (\Exception $e) { |
|
38
|
|
|
return response()->json(['bad_input' => 'The input image is corrupted'], 204); |
|
39
|
|
|
} |
|
40
|
|
|
return response()->json(['bad_input' => 'The input image is corrupted'], 204); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function resizeremote(Request $request, ImageOptimizer $imageOptimizer) |
|
44
|
|
|
{ |
|
45
|
|
|
$pic = $request->get('image'); |
|
46
|
|
|
$width = ($request->get('width')) ? $request->get('width') : 640; |
|
47
|
|
|
$height = ($request->get('height')) ? $request->get('height') : null; |
|
48
|
|
|
$file = storage_path('app/remote/') . basename($pic); |
|
49
|
|
|
$client = new Client(); |
|
50
|
|
|
$client->request('GET', $pic, [ |
|
51
|
|
|
'sink' => storage_path('app/remote/') . basename($pic), |
|
52
|
|
|
]); |
|
53
|
|
|
$imageOptimizer->optimizeImage($file); |
|
54
|
|
|
$img = Image::make($file); |
|
55
|
|
|
$resize = $img->resize($width, $height, function ($constraint) { |
|
56
|
|
|
$constraint->aspectRatio(); |
|
57
|
|
|
$constraint->upsize(); |
|
58
|
|
|
}); |
|
59
|
|
|
if ($resize) { |
|
60
|
|
|
return $img->response(); |
|
61
|
|
|
} |
|
62
|
|
|
return response()->json(['bad_input' => 'The input image is corrupted']); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function getName($w, $h) |
|
66
|
|
|
{ |
|
67
|
|
|
$postfix = '_'; |
|
68
|
|
|
if ($w) { |
|
69
|
|
|
$postfix .= 'w' . $w; |
|
70
|
|
|
} |
|
71
|
|
|
if ($h) { |
|
72
|
|
|
$postfix .= 'h' . $h; |
|
73
|
|
|
} |
|
74
|
|
|
return $postfix; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.