1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larafolio\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Larafolio\Models\Image; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
use Larafolio\Http\Content\ContentImages; |
9
|
|
|
use Intervention\Image\Facades\Image as Intervention; |
10
|
|
|
|
11
|
|
|
class ImageController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Service class for content images. |
15
|
|
|
* |
16
|
|
|
* @var \Larafolio\Http\Content\ContentImages |
17
|
|
|
*/ |
18
|
|
|
protected $contentImages; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Construct. |
22
|
|
|
* |
23
|
|
|
* @param \Larafolio\Http\Content\ContentImages $contentImages Service class for content images. |
24
|
|
|
*/ |
25
|
|
|
public function __construct(ContentImages $contentImages) |
26
|
|
|
{ |
27
|
|
|
parent::__construct(); |
28
|
|
|
|
29
|
|
|
$this->contentImages = $contentImages; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Update image name and caption. |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Http\Request $request Request object. |
36
|
|
|
* @param int $imageID ID of image to be updated. |
37
|
|
|
* |
38
|
|
|
* @return \Illuminate\Http\Response|bool |
39
|
|
|
*/ |
40
|
|
|
public function update(Request $request, $imageID) |
41
|
|
|
{ |
42
|
|
|
$image = Image::findOrFail($imageID); |
43
|
|
|
|
44
|
|
|
if ($request->hasFile('file')) { |
45
|
|
|
$imageData = $this->updateImageInstance($request, $image); |
46
|
|
|
} else { |
47
|
|
|
$imageData = $request->only(['name', 'caption', 'alt']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->user->updateImageInfo($image, $imageData); |
51
|
|
|
|
52
|
|
|
if ($request->ajax()) { |
53
|
|
|
return response()->json(true); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return back(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Remove image from portfolio. |
61
|
|
|
* |
62
|
|
|
* @param \Illuminate\Http\Request $request Request object. |
63
|
|
|
* @param int $imageID ID of image to be removed. |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Http\Response|bool |
66
|
|
|
*/ |
67
|
|
|
public function destroy(Request $request, $imageID) |
68
|
|
|
{ |
69
|
|
|
$image = Image::findOrFail($imageID); |
70
|
|
|
|
71
|
|
|
$this->user->removeImage($image); |
72
|
|
|
|
73
|
|
|
if ($request->ajax()) { |
74
|
|
|
return response()->json(true); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return back(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Update an image path. |
82
|
|
|
* |
83
|
|
|
* @param \Illuminate\Http\Request $request Request from user. |
84
|
|
|
* @param \Larafolio\Models\Image $image Image to update. |
85
|
|
|
*/ |
86
|
|
|
protected function updateImageInstance(Request $request, Image $image) |
87
|
|
|
{ |
88
|
|
|
$imagePath = $this->contentImages->saveImage($request); |
89
|
|
|
|
90
|
|
|
Storage::delete($image->path); |
91
|
|
|
|
92
|
|
|
return ['path' => $imagePath]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: