-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathprocessupload.php
217 lines (190 loc) · 6.93 KB
/
processupload.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
<?php
if(isset($_POST) && isset($_POST["w"]) && isset($_POST["h"]) && isset($_POST["id"]) && isset($_POST["ImageName"]))
{
############ Edit settings ##############
$imageName = $_POST["ImageName"];
$ThumbSquareSize = 200; //Thumbnail will be 200x200
$container_width = $_POST["w"];
$container_height = $_POST["h"];
$ThumbPrefix = "thumb_"; //Normal thumb Prefix
$DestinationDirectory = getcwd().DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR; //specify upload directory ends with / (slash)
$Quality = 90; //jpeg quality
##########################################
//check if this is an ajax request
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
die();
}
// check $_FILES['ImageFile'] not empty
if(!isset($_FILES[$imageName]) || !is_uploaded_file($_FILES[$imageName]['tmp_name']))
{
die('Something wrong with uploaded file, something missing!'); // output error when above checks fail.
}
// Random number will be added after image name
$RandomNumber = rand(0, 9999999999);
$ImageName = str_replace(' ','-',strtolower($_FILES[$imageName]['name'])); //get image name
$ImageSize = $_FILES[$imageName]['size']; // get original image size
$TempSrc = $_FILES[$imageName]['tmp_name']; // Temp name of image file stored in PHP tmp folder
$ImageType = $_FILES[$imageName]['type']; //get file type, returns "image/png", image/jpeg, text/plain etc.
//Let's check allowed $ImageType, we use PHP SWITCH statement here
switch(strtolower($ImageType))
{
case 'image/png':
//Create a new image from file
$CreatedImage = imagecreatefrompng($_FILES[$imageName]['tmp_name']);
break;
case 'image/gif':
$CreatedImage = imagecreatefromgif($_FILES[$imageName]['tmp_name']);
break;
case 'image/jpeg':
case 'image/pjpeg':
$CreatedImage = imagecreatefromjpeg($_FILES[$imageName]['tmp_name']);
break;
default:
die('Unsupported File!'); //output error and exit
}
//PHP getimagesize() function returns height/width from image file stored in PHP tmp folder.
//Get first two values from image, width and height.
//list assign svalues to $CurWidth,$CurHeight
list($CurWidth,$CurHeight)=getimagesize($TempSrc);
$image_ratio = $CurWidth / $CurHeight;
$src_width = $CurWidth;
$src_height = $CurHeight;
// Resize image proportionally according to the size of container
if($CurWidth > $container_width)
{
$CurWidth = $container_width;
$CurHeight = $CurWidth / $image_ratio;
}
if($CurHeight > $container_height)
{
$CurHeight = $container_height;
$CurWidth = $CurHeight / $image_ratio;
}
if($CurWidth < $container_width)
{
$CurWidth = $container_width;
$CurHeight = $CurWidth / $image_ratio;
}
if($CurHeight < $container_height){
$CurHeight = $container_height;
$CurWidth = $CurHeight * $image_ratio;
}
//Get file extension from Image name, this will be added after random name
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
//remove extension from filename
$ImageName = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName);
//Construct a new name with random number and extension.
$NewImageName = $ImageName.'-'.$RandomNumber.'.'.$ImageExt;
//set the Destination Image
$thumb_DestRandImageName = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumbnail name with destination directory
$DestRandImageName = $DestinationDirectory.$NewImageName; // Image with destination directory
//Resize image to Specified Size by calling resizeImage function.
if(resizeImage($CurWidth,$CurHeight,$DestRandImageName,$CreatedImage,$Quality,$ImageType, $src_width, $src_height))
{
//Create a square Thumbnail right after, this time we are using cropImage() function
if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType))
{
echo 'Error Creating thumbnail';
}
/*
We have succesfully resized and created thumbnail image
We can now output image to user's browser or store information in the database
*/
$json = array("imgSrc"=> ("uploads/".$NewImageName),"thumbSrc"=> ("uploads/".$ThumbPrefix.$NewImageName));
echo json_encode($json);
/****************************************************/
/****************************************************/
/*
// Insert info into database table!
mysql_query("INSERT INTO myImageTable (ImageName, ThumbName, ImgPath)
VALUES ($DestRandImageName, $thumb_DestRandImageName, 'uploads/')");
/****************************************************/
/****************************************************/
}else{
die('Resize Error'); //output error
}
}
// This function will proportionally resize image
function resizeImage($CurWidth,$CurHeight,$DestFolder,$SrcImage,$Quality,$ImageType, $src_width, $src_height)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0)
{
return false;
}
$NewWidth = ceil($CurWidth);
$NewHeight = ceil($CurHeight);
//Construct a proportional size of new image
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
// Resize Image
if(imagecopyresized($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $src_width, $src_height))
{
switch(strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
//Destroy image, frees memory
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}
//This function corps image to create exact square images, no matter what its original size!
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0)
{
return false;
}
//abeautifulsite.net has excellent article about "Cropping an Image to Make Square bit.ly/1gTwXW9
if($CurWidth>$CurHeight)
{
$y_offset = 0;
$x_offset = ($CurWidth - $CurHeight) / 2;
$square_size = $CurWidth - ($x_offset * 2);
}else{
$x_offset = 0;
$y_offset = ($CurHeight - $CurWidth) / 2;
$square_size = $CurHeight - ($y_offset * 2);
}
$NewCanves = imagecreatetruecolor($iSize, $iSize);
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size))
{
switch(strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
//Destroy image, frees memory
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}
if(isset($_POST["offsetX"]) || isset($_POST["offsetY"]))
{
// When somebody saves a picture you can read offsetY and offsetX and save them, so it become stored
// offsetX / offsetY
echo "Success";
}