-
Notifications
You must be signed in to change notification settings - Fork 3
/
az.multi.upload.class.php
133 lines (118 loc) · 4.94 KB
/
az.multi.upload.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
<?php
class ImageUploadAndResize
{
private $newWidth;
private $folderName;
public $n = 0;
public $s = 0;
public $Sflag = 0;
public $prepareNames;
protected $isSubQuery = false;
/**
* Image compress and processing
*/
public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImageSource = "", $positionX = "", $positionY = "", $quality, $newWidth)
{
$infoImg = getimagesize($sourceURL);
$width = $infoImg[0];
$height = $infoImg[1];
if ($width < $minImgWidth) {
echo '<div class="alert alert-danger">Image <strong>WIDTH</strong> is less then ' . $minImgWidth . 'px</div>';
exit;
}
if ($newWidth != "") {
$diff = $width / $newWidth;
$newHeight = $height / $diff; // creating new width and height with aspect ratio
} else {
$newWidth = $width;
$newHeight = $height;
}
$image = '';
if ($infoImg['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($sourceURL);
} elseif ($infoImg['mime'] == 'image/jpg') {
$image = imagecreatefromjpeg($sourceURL);
} elseif ($infoImg['mime'] == 'image/png') {
$image = imagecreatefrompng($sourceURL);
} elseif ($infoImg['mime'] == 'image/gif') {
$image = imagecreatefromgif($sourceURL);
}
$imgResource = imagecreatetruecolor($newWidth, $newHeight);
if (!empty($wmImageSource)) {
$watermark = imagecreatefrompng($wmImageSource);
// Set the margins for the watermark and get the height/width of the watermark image
$positionRight = $positionX;
$positionBottom = $positionY;
$sx = imagesx($watermark);
$sy = imagesy($watermark);
// width to calculate positioning of the watermark.
imagecopy($image, $watermark, imagesx($image) - $sx - $positionRight, imagesy($image) - $sy - $positionBottom, 0, 0, imagesx($watermark), imagesy($watermark));
}
imagealphablending($imgResource, false);
imagesavealpha($imgResource, true);
imagecopyresampled($imgResource, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if ($infoImg['mime'] == 'image/png' || $infoImg['mime'] == 'image/gif') {
$newQuality = ($quality / 10) - 1;
imagealphablending($imgResource, false);
imagesavealpha($imgResource, true);
$RET = imagepng($imgResource, $destinationURL, $newQuality); //For png quality range is 0-9
} else {
$RET = imagejpeg($imgResource, $destinationURL, $quality);
}
imagedestroy($image);
return $RET;
}
public function createDir($folderName, $permission)
{
if (!file_exists($folderName)) {
mkdir($folderName, $permission, true);
$fName = $folderName;
} else {
$fName = $folderName;
}
return $fName;
}
public function uploadFiles($yourFileName, $yourDestination, $createThumb = false, $minImgWidth = 400, $waterMarkImgSrc = "", $xPosition = "", $yPosition = "", $reName = "", $permission = 0655, $quality = 100, $newWidth = "", $thumbWidth = "")
{
if (!empty($_FILES[$yourFileName])) {
if ($createThumb != "" and $createThumb === true) {
$srcThumbPath = $this->createDir($yourDestination . '/thumb', $permission) . '/';
}
foreach ($_FILES[$yourFileName]['name'] as $val) {
$infoExt = getimagesize($_FILES[$yourFileName]['tmp_name'][$this->n]);
$this->s++;
$filesName = str_replace(" ", "", trim($_FILES[$yourFileName]['name'][$this->n]));
$files = explode(".", $filesName);
$File_Ext = substr($_FILES[$yourFileName]['name'][$this->n], strrpos($_FILES[$yourFileName]['name'][$this->n], '.'));
if ($infoExt['mime'] == 'image/gif' || $infoExt['mime'] == 'image/jpeg' || $infoExt['mime'] == 'image/png') {
$srcPath = $this->createDir($yourDestination, $permission) . '/';
if ($reName != "") {
$fileName = $this->s . $reName . $File_Ext;
} else {
$fileName = $files[0] . $File_Ext;
}
$path = trim($srcPath . $fileName);
$thumbPath = trim($srcThumbPath . $fileName);
if ($this->compressImage($_FILES[$yourFileName]['tmp_name'][$this->n], $path, $minImgWidth, $waterMarkImgSrc, $xPosition, $yPosition, $quality, $newWidth)) {
$this->compressImage($_FILES[$yourFileName]['tmp_name'][$this->n], $thumbPath, $minImgWidth, $waterMarkImgSrc, $xPosition, $yPosition, $quality, $thumbWidth);
$this->prepareNames[] = $fileName; //need to be fixed.
$this->Sflag = 1; // success
} else {
$this->Sflag = 2; // file not move to the destination
}
} else {
$this->Sflag = 3; //extention not valid
}
$this->n++;
}
if ($this->Sflag == 1) {
return $this->prepareNames;
echo '<div class="alert alert-success">Images uploaded successfully!</div>';
} else if ($this->Sflag == 2) {
echo '<div class="alert alert-danger">File not move to the destination.</div>';
} else if ($this->Sflag == 3) {
echo '<div class="alert alert-danger">File extention not good. Try with <em>.PNG, .JPEG, .GIF, .JPG</em></div>';
}
}
}
}