1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\Profile; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
6
|
|
|
use Ffcms\Core\Arch\Model; |
7
|
|
|
use Ffcms\Core\Interfaces\iUser; |
8
|
|
|
use Gregwar\Image\Image; |
9
|
|
|
|
10
|
|
|
class FormAvatarUpload extends Model |
11
|
|
|
{ |
12
|
|
|
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile */ |
13
|
|
|
public $file; |
14
|
|
|
|
15
|
|
|
const AVATAR_SIZE = 2097152; // 2mb |
16
|
|
|
const COMPRESS_QUALITY = 90; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Example of usage magic labels for future form helper usage |
20
|
|
|
*/ |
21
|
|
|
public function labels() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'file' => __('Select avatar') |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Example of usage magic rules for future usage in condition $model->validate() |
30
|
|
|
*/ |
31
|
|
|
public function rules() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
['file', 'required'], |
35
|
|
|
['file', 'isFile', ['jpg', 'png', 'gif', 'jpeg']], |
36
|
|
|
['file', 'sizeFile', [1, static::AVATAR_SIZE]] |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function sources() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
'file' => 'file' |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function copyFile(iUser $user) |
48
|
|
|
{ |
49
|
|
|
// move file to original folder |
50
|
|
|
$upload = $this->file->move(root . '/upload/user/avatar/original/', $user->id . '.' . $this->file->guessExtension()); |
|
|
|
|
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
// big image |
54
|
|
|
$this->resizeAndSave($upload, $user->id, 'big'); |
|
|
|
|
55
|
|
|
$this->resizeAndSave($upload, $user->id, 'medium'); |
|
|
|
|
56
|
|
|
$this->resizeAndSave($upload, $user->id, 'small'); |
|
|
|
|
57
|
|
|
|
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
if (App::$Debug) { |
60
|
|
|
App::$Debug->addException($e); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $original |
67
|
|
|
* @param int $user_id |
68
|
|
|
* @param string $size |
69
|
|
|
* @throws \Exception |
70
|
|
|
* @return null |
71
|
|
|
*/ |
72
|
|
|
protected function resizeAndSave($original, $user_id, $size = 'small') |
73
|
|
|
{ |
74
|
|
|
$sizeConvert = [ |
75
|
|
|
'big' => [400, 400], |
76
|
|
|
'medium' => [200, 200], |
77
|
|
|
'small' => [100, 100] |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
if (!array_key_exists($size, $sizeConvert)) { |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$image = new Image(); |
85
|
|
|
$image->setCacheDir(root . '/Private/Cache/images'); |
86
|
|
|
|
87
|
|
|
$image->open($original->getPathname()) |
88
|
|
|
->cropResize($sizeConvert[$size][0], $sizeConvert[$size][1]) |
89
|
|
|
->save(root . '/upload/user/avatar/' . $size . '/' . $user_id . '.jpg', 'jpg', static::COMPRESS_QUALITY); |
90
|
|
|
|
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: