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