Skip to content

Commit 229f896

Browse files
authored
Add fonts and update the code.
Update the PHP version
1 parent fcd9349 commit 229f896

File tree

12 files changed

+139
-124
lines changed

12 files changed

+139
-124
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "learncodeweb/filesupload",
3-
"description": "A very simple (GD library Image processing) class for upload multiple files. You can create multiple thumbnails, add watermark (text/image), resize and change the quality of the images.",
3+
"description": "A very simple (GD library Image processing) class for upload multiple files. You can create multiple thumbnails, add watermark (text/image), resize and change the quality of the images. You can also use this class with any PHP framework, tested with Laravel 8 and Laravel 9.",
44
"homepage": "https://learncodeweb.com/web-development/resize-and-upload-multiple-images-using-php-and-ajax/",
55
"type": "resize-and-upload-multiple-images-using-php-and-ajax",
66
"keywords": [
@@ -38,7 +38,7 @@
3838
"issues": "https://github.com/LearnCodeWeb/multi-files-upload-and-image-resizer/issues"
3939
},
4040
"require": {
41-
"php": ">=5.6.0"
41+
"php": ">=7.0"
4242
},
4343
"require-dev": {
4444
"phpunit/phpunit": "^4.8 || ^5.7 || ^7.5.15"

src/FilesUploadAndImageResize.php

Lines changed: 137 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace anyFileUpload;
44

5+
use Exception;
6+
57
class FilesUploadAndImageResize
68
{
79
protected $allowExtension = [];
@@ -37,79 +39,84 @@ public function __construct(string $format, array $allowExtension, string $fileD
3739
*
3840
*/
3941

40-
public function compressImage($sourceURL, $destinationURL, $minImgWidth, $waterMark = [], $quality, $newWidth)
42+
public function compressImage(string $sourceURL, string $destinationURL, int $minImgWidth, array $waterMark = [], int $quality, string $newWidth): bool|string
4143
{
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-
}
44+
try {
45+
if (!empty($waterMark)) {
46+
$waterMark['font-size'] = (empty($waterMark['font-size'])) ? 25 : $waterMark['font-size'];
47+
$waterMark['font-family'] = (empty($waterMark['font-family'])) ? __DIR__ . "/fonts/Myriad-Pro-Regular.ttf" : $waterMark['font-family'];
48+
$waterMark['font-color'] = (empty($waterMark['font-color'])) ? '#000000' : $waterMark['font-color'];
49+
$positionX = $waterMark['position-x'] ?? '';
50+
$positionY = $waterMark['position-y'] ?? '';
51+
}
5752

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-
}
53+
$infoImg = getimagesize($sourceURL);
54+
$width = $infoImg[0];
55+
$height = $infoImg[1];
56+
if ($width < $minImgWidth) {
57+
echo '<div class="alert alert-danger">Image <strong>WIDTH</strong> is less then ' . $minImgWidth . 'px</div>';
58+
exit;
59+
}
6860

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);
61+
$image = '';
62+
if ($infoImg['mime'] == 'image/jpeg') {
63+
$image = imagecreatefromjpeg($sourceURL);
64+
} elseif ($infoImg['mime'] == 'image/jpg') {
65+
$image = imagecreatefromjpeg($sourceURL);
66+
} elseif ($infoImg['mime'] == 'image/png') {
67+
$image = imagecreatefrompng($sourceURL);
68+
} elseif ($infoImg['mime'] == 'image/gif') {
69+
$image = imagecreatefromgif($sourceURL);
8570
}
86-
}
8771

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-
}
72+
//Adding watermark
73+
if (!empty($waterMark)) {
74+
if (!empty($waterMark['value']) && is_file($waterMark['value'])) {
75+
$watermark = imagecreatefrompng($waterMark['value']);
76+
imagecopy($image, $watermark, 0, 0, 0, 0, imagesx($watermark), imagesy($watermark));
77+
} else {
78+
$positionRight = $positionX;
79+
$positionBottom = $positionY;
80+
$sx = imagesx($image);
81+
$sy = imagesy($image);
82+
$watermarktext = ($waterMark['value'] != "") ? $waterMark['value'] : '';
83+
$font = ($waterMark['font-family'] != "") ? $waterMark['font-family'] : '';
84+
$fontsize = ($waterMark['font-size'] != "") ? $waterMark['font-size'] : '';
85+
list($r, $g, $b) = sscanf($waterMark['font-color'], "#%02x%02x%02x");
86+
$color = imagecolorallocate($image, $r, $g, $b);
87+
imagettftext($image, $fontsize, 0, $sx - $positionRight, $sy - $positionBottom, $color, $font, $watermarktext);
88+
}
89+
}
9690

97-
$imgResource = imagecreatetruecolor($newWidth, $newHeight);
91+
// Creating new width and height with aspect ratio
92+
if ($newWidth != "") {
93+
$diff = $width / $newWidth;
94+
$newHeight = $height / $diff;
95+
} else {
96+
$newWidth = $width;
97+
$newHeight = $height;
98+
}
9899

99-
imagealphablending($imgResource, false);
100-
imagesavealpha($imgResource, true);
100+
$imgResource = imagecreatetruecolor($newWidth, $newHeight);
101101

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;
105102
imagealphablending($imgResource, false);
106103
imagesavealpha($imgResource, true);
107-
$RET = imagepng($imgResource, $destinationURL, $newQuality); //For png quality range is 0-9
108-
} else {
109-
$RET = imagejpeg($imgResource, $destinationURL, $quality);
104+
105+
imagecopyresampled($imgResource, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
106+
if ($infoImg['mime'] == 'image/png' || $infoImg['mime'] == 'image/gif') {
107+
$newQuality = ($quality / 10) - 1;
108+
imagealphablending($imgResource, false);
109+
imagesavealpha($imgResource, true);
110+
$responseImage = imagepng($imgResource, $destinationURL, $newQuality); //For png quality range is 0-9
111+
} else {
112+
$responseImage = imagejpeg($imgResource, $destinationURL, $quality);
113+
}
114+
115+
imagedestroy($image);
116+
return $responseImage;
117+
} catch (Exception $e) {
118+
return $e->getMessage();
110119
}
111-
imagedestroy($image);
112-
return $RET;
113120
}
114121

115122
/**
@@ -119,15 +126,19 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $waterM
119126
*
120127
*/
121128

122-
public function createDir($fileDestination, $filePermission)
129+
public function createDir(string $fileDestination, int $filePermission): string
123130
{
124-
if (!file_exists($fileDestination)) {
125-
mkdir($fileDestination, $filePermission, true);
126-
$fName = $fileDestination;
127-
} else {
128-
$fName = $fileDestination;
131+
try {
132+
if (!file_exists($fileDestination)) {
133+
mkdir($fileDestination, $filePermission, true);
134+
$fName = $fileDestination;
135+
} else {
136+
$fName = $fileDestination;
137+
}
138+
return $fName;
139+
} catch (Exception $e) {
140+
return $e->getMessage();
129141
}
130-
return $fName;
131142
}
132143

133144
/**
@@ -141,73 +152,77 @@ public function createDir($fileDestination, $filePermission)
141152
*
142153
*/
143154

144-
public function uploadFiles($fileParamName, $minImgWidth = 400, $waterMark, $reName = "", $quality = 100, $newWidth = "", $thumbWidth = [])
155+
public function uploadFiles(string $fileParamName, int $minImgWidth = 400, array $waterMark, string $reName = "", int $quality = 100, string $newWidth = "", array $thumbWidth = []): array|string
145156
{
146-
if (!empty($_FILES[$fileParamName])) {
157+
try {
158+
if (!empty($_FILES[$fileParamName])) {
147159

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++;
160+
$srcPath = $this->createDir($this->fileDestination, $this->filePermission) . '/';
161+
if (isset($thumbWidth) && !empty($thumbWidth)) {
162+
$srcThumbPath = $this->createDir($this->fileDestination . '/thumb', $this->filePermission) . '/';
163+
}
164+
foreach ($_FILES[$fileParamName]['name'] as $val) {
165+
$this->s++;
154166

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], '.'));
167+
$fileInfo = pathinfo(basename($_FILES[$fileParamName]['name'][$this->n]), PATHINFO_EXTENSION);
168+
$filesName = str_replace(" ", "", trim($_FILES[$fileParamName]['name'][$this->n]));
169+
$files = explode(".", $filesName);
170+
$File_Ext = substr($_FILES[$fileParamName]['name'][$this->n], strrpos($_FILES[$fileParamName]['name'][$this->n], '.'));
159171

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;
172+
if ($reName != "") {
173+
$fileName = $this->s . $reName . $File_Ext;
166174
} else {
167-
$fileName = $files[0] . $File_Ext;
175+
if (count($files) > 2) {
176+
array_pop($files);
177+
$fileName = implode(".", $files) . $File_Ext;
178+
} else {
179+
$fileName = $files[0] . $File_Ext;
180+
}
168181
}
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][] = $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
182+
$filePath = trim($srcPath . $fileName);
183+
if (in_array(strtolower($fileInfo), array_map('strtolower', $this->allowExtension)) || empty($this->allowExtension)) {
184+
// Upload and compress only images
185+
if (strtolower($fileInfo) == 'gif' || strtolower($fileInfo) == 'jpeg' || strtolower($fileInfo) == 'jpg' || strtolower($fileInfo) == 'png') {
186+
if ($this->compressImage($_FILES[$fileParamName]['tmp_name'][$this->n], $filePath, $minImgWidth, $waterMark, $quality, $newWidth)) {
187+
if (isset($thumbWidth) && !empty($thumbWidth)) {
188+
foreach ($thumbWidth as $tw) {
189+
$thumbPath = trim($srcThumbPath . $tw . '-' . $fileName);
190+
$this->compressImage($_FILES[$fileParamName]['tmp_name'][$this->n], $thumbPath, $minImgWidth, $waterMark, $quality, $tw);
191+
$this->param['uploaded-thumb-files'][$tw][] = $tw . '-' . $fileName; //All uploaded thumbnail files name are move in this array
192+
$this->param['path-uploaded-thumb-files'][] = $thumbPath; //All uploaded thumbnail files with complete path
193+
}
181194
}
182-
}
183195

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
196+
$this->param['uploaded-files'][] = $fileName; //All uploaded files name are move in this array
197+
$this->param['path-uploaded-files'][] = $filePath; //All uploaded files name are move in this array
198+
} else {
199+
$this->param['not-uploaded-files'][] = $fileName; //All not move files name into the destination folder [ Note: Check Folder Permission ]
200+
}
186201
} else {
187-
$this->param['not-uploaded-files'][] = $fileName; //All not move files name into the destination folder [ Note: Check Folder Permission ]
202+
// Upload all other files
203+
if (move_uploaded_file($_FILES[$fileParamName]['tmp_name'][$this->n], $filePath)) {
204+
$this->param['uploaded-files'][] = $fileName; //All uploaded files name are move in this array
205+
$this->param['path-uploaded-files'][] = $filePath; //All uploaded files name are move in this array
206+
} else {
207+
$this->param['not-uploaded-files'][] = $fileName; //All not move files name into the destination folder [ Note: Check Folder Permission ]
208+
}
188209
}
189210
} 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-
}
211+
$this->param['bad-extension-files'][] = $fileName; //Bad extension files name are move in this array
212+
$this->param['bad-extensions'][] = strtolower($fileInfo); //Bad extensions move in this array
197213
}
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-
}
202214

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);
215+
$this->n++;
216+
}
217+
if ($this->format == "array") {
218+
$this->uploadedData = $this->param;
219+
} else if ($this->format == "json") {
220+
$this->uploadedData = json_encode($this->param);
221+
}
222+
return $this->uploadedData;
209223
}
210-
return $this->uploadedData;
224+
} catch (Exception $e) {
225+
return $e->getMessage();
211226
}
212227
}
213228
}

src/fonts/CALIBRIB_5.TTF

830 KB
Binary file not shown.

src/fonts/Calibri_5.ttf

344 KB
Binary file not shown.

src/fonts/Myriad-Pro-Regular.ttf

92.7 KB
Binary file not shown.

src/fonts/MyriadPro-Bold.otf

101 KB
Binary file not shown.

src/fonts/MyriadPro-BoldCond.otf

98.6 KB
Binary file not shown.

src/fonts/MyriadPro-It.otf

103 KB
Binary file not shown.

src/fonts/sw.ttf

40.2 KB
Binary file not shown.

src/fonts/sw2.ttf

40.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)