-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupload.php
117 lines (90 loc) · 3.57 KB
/
upload.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
<?php
ob_start();
session_start();
include_once( "priedai/conf.php" );
include_once( "priedai/prisijungimas.php" );
if ( isset( $_SESSION[SLAPTAS]['level'] ) && $_SESSION[SLAPTAS]['level'] > 0 && isset( $_FILES['userfile']['type'] ) ) {
//$allowed_types = array('image/jpg', 'image/jpeg');
//if(in_array($_FILES['userfile']['type'], $allowed_types))
//{
function create_th( $files ) {
global $_POST;
//$big_img = "tmp/"; //Kur bus saugomi didesni paveiksliukai
$mini_img = "images/avatars"; //Kur bus saugomos miniatiuros
$img_thumb_width = 250; //Mini paveiksliuku dydis
//Sara�as leid�iamu failu
$limitedext = array( ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".gif", ".GIF" );
$file_type = $files['type'];
$file_name = $files['name'];
$file_size = $files['size'];
$file_tmp = $files['tmp_name'];
//Patikrinam ar failas ikeltas sekmingai
if ( !is_uploaded_file( $file_tmp ) ) {
throw new Exception( "File: <b>{$file_tmp}</b> was not uploaded" );
} else {
//gaunamm failo galune
$ext = strrchr( $file_name, '.' );
$ext = strtolower( $ext );
//Tikrinam ar tinkamas failas
if ( !in_array( $ext, $limitedext ) ) {
throw new Exception( "File: <b>{$file_tmp}</b> is wrong type: <b>{$file_type}</b>" );
}
//create a random file name
//$rand_name = basename($file_tmp,'.tmp');
$rand_name = md5( $_POST['email'] );
//the new width variable
if ( $file_size ) {
if ( $file_type == "image/pjpeg" || $file_type == "image/jpeg" ) {
$img = imagecreatefromjpeg( $file_tmp );
} elseif ( $file_type == "image/x-png" || $file_type == "image/png" ) {
$img = imagecreatefrompng( $file_tmp );
} elseif ( $file_type == "image/gif" ) {
$img = imagecreatefromgif( $file_tmp );
} elseif ( $file_type == "image/bmp" ) {
$img = imagecreatefrombmp( $file_tmp );
}
//list the width and height and keep the height ratio.
$width = imageSX( $img );
$height = imageSY( $img );
// Build the thumbnail
$target_width = 100;
$target_height = 100;
$target_ratio = $target_width / $target_height;
$img_ratio = $width / $height;
//calculate the image ratio
$imgratio = $width / $height;
if ( $target_ratio > $img_ratio ) {
$new_height = $target_height;
$new_width = $img_ratio * $target_height;
} else {
$new_height = $target_width / $img_ratio;
$new_width = $target_width;
}
if ( $new_height > $target_height ) {
$new_height = $target_height;
}
if ( $new_width > $target_width ) {
$new_height = $target_width;
}
$new_img = ImageCreateTrueColor( $target_width, $target_height );
if ( !@imagefilledrectangle( $new_img, 0, 0, $target_width - 1, $target_height - 1, 0 ) ) { // Fill the image black
throw new Exception( "Wrong GD version" );
}
if ( !@imagecopyresampled( $new_img, $img, ( $target_width - $new_width ) / 2, ( $target_height - $new_height ) / 2, 0, 0, $new_width, $new_height, $width, $height ) ) {
throw new Exception( "Wrong GD version" );
}
imagejpeg( $new_img, $mini_img . "/" . $rand_name . '.jpeg', 95 );
chmod( $mini_img . "/" . $rand_name . '.jpeg', 0777 );
ImageDestroy( $img );
ImageDestroy( $new_img );
//move_uploaded_file($file_tmp, $big_img . "/" . $rand_name . $ext);
//chmod($big_img . "/" . $rand_name . $ext, 0777);
return $mini_img . "/" . $rand_name . $ext;
}
}
}
create_th( $_FILES['userfile'] );
}
//}
// echo $_POST['email'];
?>