-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_uploader.class.php
298 lines (238 loc) · 10.5 KB
/
image_uploader.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
//THIS IS THE CLASS WHICH IS IN THE CLASSES FOLDER USUALLY. NOT USING A FRAMEWORK NOW, THOUGH!
define('SYSTEM_FOLDER', '');
define('USER_CONTENT_VERSION', '1');
class ImageUploader {
// Attributes
// Constructor
public $versionPath;
protected $allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');
public function __construct () {
if (defined('SYSTEM_FOLDER') && defined('USER_CONTENT_VERSION')) {
//This is something like /website/user_content/1/
$this->versionPath = SYSTEM_FOLDER . 'user_content/' . USER_CONTENT_VERSION .'/';
} else {
die('No version path');
}
}
protected $extension;
public function getFileExtension() {
if (!$this->extension) {
if ($this->theFile) {
$this->extension = pathinfo($this->theFile['original_name'], PATHINFO_EXTENSION);
if ($this->isAllowedExtension($this->extension)) {
return $this->extension;
}
}
}
return $this->extension;
}
protected function isAllowedExtension ($ext) {
if ($ext && $this->allowedExtensions) {
if (in_array($ext, $this->allowedExtensions)) {
return true;
}
}
return false;
}
protected $imageSource;
protected function getImageSource () {
if (!$this->imageSource) {
if ($this->theFile) {
$uploadedFile = $this->theFile['name'];
if ($uploadedFile) {
switch ($this->getFileExtension()) {
case 'jpg':
$this->imageSource = imagecreatefromjpeg($uploadedFile);
break;
case 'jpeg':
$this->imageSource = imagecreatefromjpeg($uploadedFile);
break;
case 'png':
$this->imageSource = imagecreatefrompng($uploadedFile);
break;
case 'gif':
$this->imageSource = imagecreatefromgif($uploadedFile);
break;
}
}
}
}
return $this->imageSource;
}
protected $theFile;
protected function getTheFile($file) {
if (is_array($file)) {
$newFile['name'] = $file['tmp_name'];
$newFile['original_name'] = $file['name'];
} else {
$newFile['name'] = $file;
$newFile['original_name'] = $file;
}
return $newFile;
}
public function placeImageInContainer ($target, $newFilename, $file,
$containerWidth, $containerHeight,
$imageDesiredFileWidth, $imageDesiredFileHeight,
$posX, $posY, $background) {
$this->theFile = $this->getTheFile($file);
//Get the width and height of the file thats being uploaded
//list($currentFileWidth, $currentFileHeight) = getimagesize($this->theFile['name']);
//Make an image container that is as big as the desired width and height. The image width and height is being calculated after this
//And is being pasted into this container. The image cant be bigger than this container but it CAN be smaller, in that case
//The transparent background sustains the desiredWidth*desiredHeight dimension.
$newImage = imagecreatetruecolor($containerWidth, $containerHeight);
// Make the background
if ($background != 'transparent') {
if (stripos($background, '#') !== false) {
$background = substr($background, 1, strlen($background));
}
$R = '0X' . substr($background, 0, 2);
$B = '0X' . substr($background, 2, 2);
$G = '0X' . substr($background, 4, 2);
$bgColor = imagecolorallocate($newImage, $R, $B, $G);
imagefill($newImage, 0, 0, $bgColor);
//imagefilledrectangle($newImage, 0, 0, $containerWidth, $containerHeight, $bgColor);
} else {
// Make the background transparent
$black = imagecolorallocate($newImage, 0, 0, 0);
imagecolortransparent($newImage, $black);
}
$src = $this->getImageSource();
if ($newImage && $src) {
$fileDestination = $this->versionPath.$target;
//In order to place an image in a container the image that will be put IN the container should be resized to the size he requires(imageDesiredFileWidth and height)
$resizedFilename = $this->createNewSizeTmp($src, $imageDesiredFileWidth, $imageDesiredFileHeight, $fileDestination .'tmp/');
if ($resizedFilename) {
//When the resizedFilename TMP is made use this image to place into the container.
$this->theFile['name'] = $fileDestination .'tmp/'.$resizedFilename;
$newSrc = imagecreatefrompng($this->theFile['name']);
imagecopyresampled($newImage, $newSrc, $posX, $posY, 0, 0, $imageDesiredFileWidth, $imageDesiredFileHeight, $imageDesiredFileWidth, $imageDesiredFileHeight);
$fileDestination = $this->versionPath.$target;
$fileName = $newFilename .'.png';
if ($this->uploadImage($newImage, $fileDestination, $fileName)) {
//The image is saved. Delete the recently made TMP
unlink($fileDestination .'/tmp/'.$resizedFilename);
//Return the target and filename so it can be saved in a database or whatever the user wants with it
//Returns something like managedMedia/participant/logo_1.png
return $target.$fileName;
} else {
//The image is not saved but will not be saved. Delete the recently made TMP
unlink($fileDestination .'/tmp/'.$resizedFilename);
}
}
}
return false;
}
protected function createNewSizeTmp ($src, $width, $height, $destination) {
list($currentFileWidth, $currentFileHeight) = getimagesize($this->theFile['name']);
$resizeImageContainer = imagecreatetruecolor($width, $height);
imagecopyresampled($resizeImageContainer, $src, 0, 0, 0, 0, $width, $height, $currentFileWidth, $currentFileHeight);
$resizedFilename = 'tmp_' . substr(md5(time()), 0, 5) .'.png';
if ($this->uploadImage($resizeImageContainer, $destination, $resizedFilename)) {
return $resizedFilename;
}
return false;
}
public function createImageWithFixedDimensions ($target, $newFilename, $file, $desiredWidth, $desiredHeight) {
$this->theFile = $this->getTheFile($file);
//Get the width and height of the file thats being uploaded
list($currentFileWidth, $currentFileHeight) = getimagesize($this->theFile['name']);
//Make an image container that is as big as the desired width and height. The image width and height is being calculated after this
//And is being pasted into this container. The image cant be bigger than this container but it CAN be smaller, in that case
//The transparent background sustains the desiredWidth*desiredHeight dimension.
$newImage = imagecreatetruecolor($desiredWidth, $desiredHeight);
// Make the background transparent
$black = imagecolorallocate($newImage, 0, 0, 0);
imagecolortransparent($newImage, $black);
$src = $this->getImageSource();
$newX = 0;
$newY = 0;
$ratio = $currentFileWidth/$currentFileHeight;
if ($currentFileWidth > $currentFileHeight) {
//Image is wider than high. Therefore the width can be the desired width and the height has to be relative to its width;
$newWidth = $desiredWidth;
$newHeight = $desiredWidth / $ratio;
$newY = ($desiredHeight - $newHeight) / 2;
} else {
//Image is higher than wide
$newWidth = $desiredHeight * $ratio;
$newHeight = $desiredHeight;
$newX = ($desiredWidth - $newWidth) / 2;
}
if ($newImage && $src) {
imagecopyresampled($newImage, $src, $newX, $newY, 0, 0, $newWidth, $newHeight, $currentFileWidth, $currentFileHeight);
$fileDestination = $this->versionPath.$target;
$fileName = $newFilename .'.png';
if ($this->uploadImage($newImage, $fileDestination, $fileName)) {
//Return the target and filename so it can be saved in a database or whatever the user wants with it
//Returns something like managedMedia/participant/logo_1.png
return $target.$fileName;
}
}
return false;
}
public function createImageWithMaxDimensions ($target, $newFilename, $file, $maxWidth, $maxHeight) {
$this->theFile = $this->getTheFile($file);
//Get the width and height of the file thats being uploaded
list($currentFileWidth, $currentFileHeight) = getimagesize($this->theFile['name']);
if ($maxWidth >= $currentFileWidth && $maxHeight >= $currentFileHeight) {
//Max width and height are not reached. Use their original dimensions
$newWidth = $currentFileWidth;
$newHeight = $currentFileHeight;
} else {
$ratio = $currentFileWidth/$currentFileHeight;
if ($currentFileWidth > $currentFileHeight) {
//Check if the new width should be capped to the maxWidth or can sustain his own width(Because its lower than the maximum)
$newTempWidth = ($maxWidth >= $currentFileWidth) ? $currentFileWidth : $maxWidth;
$newWidth = $newTempWidth;
$newHeight = $newTempWidth / $ratio;
} else {
//Check if the new height should be capped to the maxHeight or can sustain his own height(Because its lower than the maximum)
$newTempHeight = ($maxHeight >= $currentFileHeight) ? $currentFileHeight : $maxHeight;
$newWidth = $newTempHeight * $ratio;
$newHeight = $newTempHeight;
}
}
$src = $this->getImageSource();
//Create an image container that is equally big as the newly founded width and height.
$newImage = imagecreatetruecolor($newWidth, $newHeight);
if ($newImage && $src) {
imagecopyresampled($newImage, $src, 0, 0, 0, 0, $newWidth, $newHeight, $currentFileWidth, $currentFileHeight);
$fileDestination = $this->versionPath.$target;
$fileName = $newFilename .'.png';
if ($this->uploadImage($newImage, $fileDestination, $fileName)) {
//Return the target and filename so it can be saved in a database or whatever the user wants with it
//Returns something like managedMedia/participant/logo_1.png
return $target.$fileName;
}
}
return false;
}
public function uploadUnalteredImage($target, $newFilename, $file) {
$this->theFile = $this->getTheFile($file);
//Get the width and height of the file thats being uploaded
list($currentFileWidth, $currentFileHeight) = getimagesize($this->theFile['name']);
$src = $this->getImageSource();
//Create an image container that is equally big as the newly founded width and height.
$newImage = imagecreatetruecolor($currentFileWidth, $currentFileHeight);
if ($newImage && $src) {
imagecopyresampled($newImage, $src, 0, 0, 0, 0, $currentFileWidth, $currentFileHeight, $currentFileWidth, $currentFileHeight);
$fileDestination = $this->versionPath.$target;
$fileName = $newFilename .'.png';
if ($this->uploadImage($newImage, $fileDestination, $fileName)) {
//Return the target and filename so it can be saved in a database or whatever the user wants with it
//Returns something like managedMedia/participant/logo_1.png
return $target.$fileName;
}
}
return false;
}
protected function uploadImage($image, $destination, $imageName) {
if (imagepng($image, $destination.$imageName)) {
return true;
}
return false;
}
}
?>