Skip to content

Commit 08dcf87

Browse files
authored
Add files via upload
1 parent 293009f commit 08dcf87

File tree

2 files changed

+215
-2
lines changed

2 files changed

+215
-2
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
},
5151
"autoload": {
5252
"psr-4": {
53-
"LearnCodeWeb\\filesupload\\": "LearnCodeWeb/filesupload/"
53+
"learncodeweb\\filesupload\\": "learncodeweb/filesupload/src"
5454
}
5555
},
5656
"extra": {
5757
"branch-alias": {
5858
"dev-master": "1.0-dev"
5959
}
6060
}
61-
}
61+
}

src/FilesUploadAndImageResize.php

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
3+
namespace anyFileUpload;
4+
5+
class FilesUploadAndImageResize
6+
{
7+
protected $allowExtension = [];
8+
protected $fileDestination = '';
9+
protected $filePermission = '0655';
10+
protected $n = 0;
11+
protected $s = 0;
12+
protected $format = 'array';
13+
protected $param = [];
14+
public $uploadedData = '';
15+
16+
/**
17+
* Must Initialize main param
18+
* @param
19+
* 1) Format set the return result set array or json
20+
* 2) Pass file extentions in array
21+
* 3) Dir path where you want to upload the files *thumb folder will be craeted inside
22+
*
23+
*/
24+
25+
public function __construct(string $format, array $allowExtension, string $fileDestination, int $filePermission)
26+
{
27+
$this->format = strtolower($format);
28+
$this->allowExtension = $allowExtension;
29+
$this->fileDestination = $fileDestination;
30+
$this->filePermission = $filePermission;
31+
}
32+
33+
/**
34+
* Image compress and processing
35+
* Main function that we used to compress images
36+
* Consider only jpeg,jpg,png,gif images [If you want to use other extensions then use Imagick]
37+
*
38+
*/
39+
40+
public function compressImage($sourceURL, $destinationURL, $minImgWidth, $waterMark = [], $quality, $newWidth)
41+
{
42+
if (!empty($waterMark)) {
43+
$waterMark['font-size'] = (empty($waterMark['font-size'])) ? 25 : $waterMark['font-size'];
44+
$waterMark['font-family'] = (empty($waterMark['font-family'])) ? "../fonts/Myriad-Pro-Regular.ttf" : $waterMark['font-family'];
45+
$waterMark['font-color'] = (empty($waterMark['font-color'])) ? '#000000' : $waterMark['font-color'];
46+
$positionX = $waterMark['position-x'] ?? '';
47+
$positionY = $waterMark['position-y'] ?? '';
48+
}
49+
50+
$infoImg = getimagesize($sourceURL);
51+
$width = $infoImg[0];
52+
$height = $infoImg[1];
53+
if ($width < $minImgWidth) {
54+
echo '<div class="alert alert-danger">Image <strong>WIDTH</strong> is less then ' . $minImgWidth . 'px</div>';
55+
exit;
56+
}
57+
58+
$image = '';
59+
if ($infoImg['mime'] == 'image/jpeg') {
60+
$image = imagecreatefromjpeg($sourceURL);
61+
} elseif ($infoImg['mime'] == 'image/jpg') {
62+
$image = imagecreatefromjpeg($sourceURL);
63+
} elseif ($infoImg['mime'] == 'image/png') {
64+
$image = imagecreatefrompng($sourceURL);
65+
} elseif ($infoImg['mime'] == 'image/gif') {
66+
$image = imagecreatefromgif($sourceURL);
67+
}
68+
69+
//Adding watermark
70+
if (!empty($waterMark)) {
71+
if (!empty($waterMark['value']) && is_file($waterMark['value'])) {
72+
$watermark = imagecreatefrompng($waterMark['value']);
73+
imagecopy($image, $watermark, 0, 0, 0, 0, imagesx($watermark), imagesy($watermark));
74+
} else {
75+
$positionRight = $positionX;
76+
$positionBottom = $positionY;
77+
$sx = imagesx($image);
78+
$sy = imagesy($image);
79+
$watermarktext = ($waterMark['value'] != "") ? $waterMark['value'] : '';
80+
$font = ($waterMark['font-family'] != "") ? $waterMark['font-family'] : '';
81+
$fontsize = ($waterMark['font-size'] != "") ? $waterMark['font-size'] : '';
82+
list($r, $g, $b) = sscanf($waterMark['font-color'], "#%02x%02x%02x");
83+
$color = imagecolorallocate($image, $r, $g, $b);
84+
imagettftext($image, $fontsize, 0, $sx - $positionRight, $sy - $positionBottom, $color, $font, $watermarktext);
85+
}
86+
}
87+
88+
// Creating new width and height with aspect ratio
89+
if ($newWidth != "") {
90+
$diff = $width / $newWidth;
91+
$newHeight = $height / $diff;
92+
} else {
93+
$newWidth = $width;
94+
$newHeight = $height;
95+
}
96+
97+
$imgResource = imagecreatetruecolor($newWidth, $newHeight);
98+
99+
imagealphablending($imgResource, false);
100+
imagesavealpha($imgResource, true);
101+
102+
imagecopyresampled($imgResource, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
103+
if ($infoImg['mime'] == 'image/png' || $infoImg['mime'] == 'image/gif') {
104+
$newQuality = ($quality / 10) - 1;
105+
imagealphablending($imgResource, false);
106+
imagesavealpha($imgResource, true);
107+
$RET = imagepng($imgResource, $destinationURL, $newQuality); //For png quality range is 0-9
108+
} else {
109+
$RET = imagejpeg($imgResource, $destinationURL, $quality);
110+
}
111+
imagedestroy($image);
112+
return $RET;
113+
}
114+
115+
/**
116+
* Create folder with permission
117+
* If exist no need to create
118+
* @param folder path and permission [default = 0655]
119+
*
120+
*/
121+
122+
public function createDir($fileDestination, $filePermission)
123+
{
124+
if (!file_exists($fileDestination)) {
125+
mkdir($fileDestination, $filePermission, true);
126+
$fName = $fileDestination;
127+
} else {
128+
$fName = $fileDestination;
129+
}
130+
return $fName;
131+
}
132+
133+
/**
134+
* Main function to upload files
135+
* This function return Array with status & names
136+
* Array index tells the status of files
137+
* ['bad-extension-files'] return all files with bad extension which is set by user
138+
* ['bad-extensions'] return only bad extensions which is set by user
139+
* ['uploaded-files'] return all uploaded files
140+
* ['not-uploaded-files'] return all not move files into the destination folder [ Note: Folder (Dir) Permission issue ]
141+
*
142+
*/
143+
144+
public function uploadFiles($fileParamName, $minImgWidth = 400, $waterMark, $reName = "", $quality = 100, $newWidth = "", $thumbWidth = [])
145+
{
146+
if (!empty($_FILES[$fileParamName])) {
147+
148+
$srcPath = $this->createDir($this->fileDestination, $this->filePermission) . '/';
149+
if (isset($thumbWidth) && !empty($thumbWidth)) {
150+
$srcThumbPath = $this->createDir($this->fileDestination . '/thumb', $this->filePermission) . '/';
151+
}
152+
foreach ($_FILES[$fileParamName]['name'] as $val) {
153+
$this->s++;
154+
155+
$fileInfo = pathinfo(basename($_FILES[$fileParamName]['name'][$this->n]), PATHINFO_EXTENSION);
156+
$filesName = str_replace(" ", "", trim($_FILES[$fileParamName]['name'][$this->n]));
157+
$files = explode(".", $filesName);
158+
$File_Ext = substr($_FILES[$fileParamName]['name'][$this->n], strrpos($_FILES[$fileParamName]['name'][$this->n], '.'));
159+
160+
if ($reName != "") {
161+
$fileName = $this->s . $reName . $File_Ext;
162+
} else {
163+
if (count($files) > 2) {
164+
array_pop($files);
165+
$fileName = implode(".", $files) . $File_Ext;
166+
} else {
167+
$fileName = $files[0] . $File_Ext;
168+
}
169+
}
170+
$filePath = trim($srcPath . $fileName);
171+
if (in_array(strtolower($fileInfo), array_map('strtolower', $this->allowExtension)) || empty($this->allowExtension)) {
172+
// Upload and compress only images
173+
if (strtolower($fileInfo) == 'gif' || strtolower($fileInfo) == 'jpeg' || strtolower($fileInfo) == 'jpg' || strtolower($fileInfo) == 'png') {
174+
if ($this->compressImage($_FILES[$fileParamName]['tmp_name'][$this->n], $filePath, $minImgWidth, $waterMark, $quality, $newWidth)) {
175+
if (isset($thumbWidth) && !empty($thumbWidth)) {
176+
foreach ($thumbWidth as $tw) {
177+
$thumbPath = trim($srcThumbPath . $tw . '-' . $fileName);
178+
$this->compressImage($_FILES[$fileParamName]['tmp_name'][$this->n], $thumbPath, $minImgWidth, $waterMark, $quality, $tw);
179+
$this->param['uploaded-thumb-files'][] = $tw . '-' . $fileName; //All uploaded thumbnail files name are move in this array
180+
$this->param['path-uploaded-thumb-files'][] = $thumbPath; //All uploaded thumbnail files with complete path
181+
}
182+
}
183+
184+
$this->param['uploaded-files'][] = $fileName; //All uploaded files name are move in this array
185+
$this->param['path-uploaded-files'][] = $filePath; //All uploaded files name are move in this array
186+
} else {
187+
$this->param['not-uploaded-files'][] = $fileName; //All not move files name into the destination folder [ Note: Check Folder Permission ]
188+
}
189+
} else {
190+
// Upload all other files
191+
if (move_uploaded_file($_FILES[$fileParamName]['tmp_name'][$this->n], $filePath)) {
192+
$this->param['uploaded-files'][] = $fileName; //All uploaded files name are move in this array
193+
$this->param['path-uploaded-files'][] = $filePath; //All uploaded files name are move in this array
194+
} else {
195+
$this->param['not-uploaded-files'][] = $fileName; //All not move files name into the destination folder [ Note: Check Folder Permission ]
196+
}
197+
}
198+
} else {
199+
$this->param['bad-extension-files'][] = $fileName; //Bad extension files name are move in this array
200+
$this->param['bad-extensions'][] = strtolower($fileInfo); //Bad extensions move in this array
201+
}
202+
203+
$this->n++;
204+
}
205+
if ($this->format == "array") {
206+
$this->uploadedData = $this->param;
207+
} else if ($this->format == "json") {
208+
$this->uploadedData = json_encode($this->param);
209+
}
210+
return $this->uploadedData;
211+
}
212+
}
213+
}

0 commit comments

Comments
 (0)