1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application\Entity; |
4
|
|
|
|
5
|
|
|
use Cocur\Slugify\Slugify; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Abstract image upload. |
10
|
|
|
* |
11
|
|
|
* Some default methods for the abstract image upload |
12
|
|
|
* |
13
|
|
|
* @author Borut Balažek <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class AbstractImageUpload |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var File |
19
|
|
|
*/ |
20
|
|
|
protected $image; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $imageUploadPath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $imageUploadDir; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $imageUrl; |
36
|
|
|
|
37
|
|
|
/*** Image ***/ |
38
|
|
|
/** |
39
|
|
|
* @return File |
40
|
|
|
*/ |
41
|
|
|
public function getImage() |
42
|
|
|
{ |
43
|
|
|
return $this->image; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param File $image |
48
|
|
|
* |
49
|
|
|
* @return AbstractImageUpload |
50
|
|
|
*/ |
51
|
|
|
public function setImage(File $image = null) |
52
|
|
|
{ |
53
|
|
|
$this->image = $image; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/*** Image path ***/ |
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getImageUploadPath() |
63
|
|
|
{ |
64
|
|
|
return $this->imageUploadPath; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $imageUploadPath |
69
|
|
|
* |
70
|
|
|
* @return AbstractImageUpload |
71
|
|
|
*/ |
72
|
|
|
public function setImageUploadPath($imageUploadPath) |
73
|
|
|
{ |
74
|
|
|
$this->imageUploadPath = $imageUploadPath; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/*** Image upload dir ***/ |
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getImageUploadDir() |
84
|
|
|
{ |
85
|
|
|
return $this->imageUploadDir; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $imageUploadDir |
90
|
|
|
* |
91
|
|
|
* @return AbstractImageUpload |
92
|
|
|
*/ |
93
|
|
|
public function setImageUploadDir($imageUploadDir) |
94
|
|
|
{ |
95
|
|
|
$this->imageUploadDir = $imageUploadDir; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/*** Image URL ***/ |
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getImageUrl() |
105
|
|
|
{ |
106
|
|
|
return $this->imageUrl; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $imageUrl |
111
|
|
|
*/ |
112
|
|
|
public function setImageUrl($imageUrl) |
113
|
|
|
{ |
114
|
|
|
$this->imageUrl = $imageUrl; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/*** Image upload ***/ |
120
|
|
|
/** |
121
|
|
|
* @return AbstractImageUpload |
122
|
|
|
* |
123
|
|
|
* @throws \Exception If upload dir and path are not set |
124
|
|
|
*/ |
125
|
|
|
public function imageUpload() |
126
|
|
|
{ |
127
|
|
|
$image = $this->getImage(); |
128
|
|
|
|
129
|
|
|
if (null !== $image) { |
130
|
|
|
$uploadDir = $this->getImageUploadDir(); |
131
|
|
|
$uploadPath = $this->getImageUploadPath(); |
132
|
|
|
|
133
|
|
|
if (!($uploadDir || $uploadPath)) { |
134
|
|
|
throw new \Exception('You must define the imageUploadDir and the imageUploadPath!'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$slugify = new Slugify(); |
138
|
|
|
|
139
|
|
|
$filename = $slugify->slugify( |
140
|
|
|
$image->getClientOriginalName() |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$filename .= '_'.sha1(uniqid(mt_rand(), true)).'.'. |
144
|
|
|
$image->guessExtension() |
145
|
|
|
; |
146
|
|
|
|
147
|
|
|
$image->move( |
148
|
|
|
$uploadDir, |
149
|
|
|
$filename |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$this->setImageUrl($uploadPath.$filename); |
153
|
|
|
|
154
|
|
|
$this->setImage(null); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|