|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelFormPartials; |
|
4
|
|
|
|
|
5
|
|
|
use Intervention\Image\ImageManagerStatic as Image; |
|
6
|
|
|
|
|
7
|
|
|
class LaravelFormPartials |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Upload image on server. |
|
11
|
|
|
* $imageFile - the file to upload |
|
12
|
|
|
* $imageSubdir is the subdir in /storage/app/public/images/.. |
|
13
|
|
|
* |
|
14
|
|
|
* @param array $imageFile |
|
15
|
|
|
* @param string $imageName |
|
16
|
|
|
* @param string $imageSubdir |
|
17
|
|
|
* @param string $imageWidth |
|
18
|
|
|
* @param string $thumbWidth |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
2 |
|
public static function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth) |
|
22
|
|
|
{ |
|
23
|
2 |
|
if ($imageFile) { |
|
|
|
|
|
|
24
|
|
|
// Create dir if not exist (in /storage/app/public/images/..) |
|
25
|
1 |
|
if (! \Storage::disk('public')->has('images/'.$imageSubdir.'/')) { |
|
|
|
|
|
|
26
|
1 |
|
\Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
$destinationPath = 'app/public/images/'.$imageSubdir.'/'; |
|
30
|
1 |
|
$imageName = $imageFile->hashName(); |
|
31
|
|
|
// Resize the image with Intervention - http://image.intervention.io/api/resize |
|
32
|
|
|
// - resize and store the image to a specific width and constrain aspect ratio (auto height) |
|
33
|
|
|
// - save file as jpg with medium quality |
|
34
|
1 |
|
$image = Image::make($imageFile->getRealPath()) |
|
35
|
1 |
|
->resize((int) $imageWidth, null, |
|
36
|
|
|
function ($constraint) { |
|
37
|
1 |
|
$constraint->aspectRatio(); |
|
38
|
1 |
|
}) |
|
39
|
1 |
|
->save(storage_path($destinationPath.$imageName), 75); |
|
40
|
|
|
|
|
41
|
|
|
// Create the thumb |
|
42
|
1 |
|
$image->resize((int) $thumbWidth, null, |
|
43
|
|
|
function ($constraint) { |
|
44
|
1 |
|
$constraint->aspectRatio(); |
|
45
|
1 |
|
}) |
|
46
|
1 |
|
->save(storage_path($destinationPath.'thumb_'.$imageName), 75); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
$ret = $imageName; |
|
50
|
|
|
|
|
51
|
2 |
|
return $ret; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.