-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload_img.php
59 lines (50 loc) · 2.06 KB
/
upload_img.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
<?php
include_once('includes/init.php');
$currUser = $_SESSION['username'];
function addThumbnail($original, $newFileName, $newSize, $width, $height, $square) {
$thumbnail = imagecreatetruecolor($newSize, $newSize);
imagecopyresized($thumbnail, $original, 0, 0,
($width > $square)? ($width - $square)/2 : 0,
($height > $square)? ($height - $square)/2 : 0,
$newSize, $newSize, $square, $square);
imagejpeg($thumbnail, $newFileName);
}
function createImageFromType($image) {
switch ($image['type']) {
case 'image/jpg':
case 'image/jpeg':
return imagecreatefromjpeg($image['tmp_name']);
case 'image/png':
return imagecreatefrompng($image['tmp_name']);
default:
return FALSE;
}
}
//If an image was uploaded
if($_FILES['image']['name'])
{
//No errors detected
if((!$_FILES['image']['error']) && ($_FILES['image']['size'] != FALSE))
{
// Generate filenames for original, small and medium files
$profileFileName = "images/user/profile/$currUser.jpg";
$smallFileName = "images/user/thumbnails/$currUser.jpg";
// Create an image representation of the original image
if ( !($original = createImageFromType($_FILES['image'])) ) {
//Error message here and go on - $message = 'Received wrong file type. Please use jpeg or png'
return FALSE;
}
$width = imagesx($original); // width of the original image
$height = imagesy($original); // height of the original image
$square = min($width, $height); // size length of the maximum square
addThumbnail($original, $profileFileName, 300, $width, $height, $square);
addThumbnail($original, $smallFileName, 30, $width, $height, $square);
//$message = 'File succesfully loaded';
}
//Errors detected
else {
//Send message indicating bad file or some shit
}
}
header("Location: profile.php"); //-> TODO: meter em ajax sem redericeionamento, SOMEHOW
?>