1
|
|
|
<?php |
2
|
|
|
/* Note: This thumbnail creation script requires the GD PHP Extension. |
3
|
|
|
If GD is not installed correctly PHP does not render this page correctly |
4
|
|
|
and SWFUpload will get "stuck" never calling uploadSuccess or uploadError |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
// Get the session Id passed from SWFUpload. We have to do this to work-around the Flash Player Cookie Bug |
8
|
|
|
if (isset($_POST["PHPSESSID"])) { |
9
|
|
|
session_id($_POST["PHPSESSID"]); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
session_start(); |
13
|
|
|
ini_set("html_errors", "0"); |
14
|
|
|
|
15
|
|
|
// Check the upload |
16
|
|
|
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { |
17
|
|
|
echo "ERROR:invalid upload"; |
18
|
|
|
exit(0); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* 得到图片类型 |
23
|
|
|
**/ |
24
|
|
|
$FileType = strtolower(substr(strrchr($_FILES["Filedata"]['name'], "."), 1)); |
25
|
|
|
|
26
|
|
|
$file_id = md5($_FILES["Filedata"]["tmp_name"] + mt_rand() * 100000); |
27
|
|
|
|
28
|
|
|
if (!is_dir('../images/hotel/tmp/')) { |
29
|
|
|
mkdir('../images/hotel/tmp/', 0777); |
30
|
|
|
} |
31
|
|
|
//upload to tmp files |
32
|
|
|
//move_uploaded_file($_FILES["Filedata"]["tmp_name"], "../images/hotel/tmp/" . $file_id . '.' . $FileType ); |
33
|
|
|
copy($_FILES["Filedata"]["tmp_name"], "../images/hotel/tmp/" . $file_id . '.' . $FileType); |
34
|
|
|
|
35
|
|
|
// Get the image and create a thumbnail |
36
|
|
|
switch ($FileType) { |
37
|
|
|
case "jpg": |
38
|
|
|
$img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]); |
39
|
|
|
break; |
40
|
|
|
case "png": |
41
|
|
|
$img = imagecreatefrompng($_FILES["Filedata"]["tmp_name"]); |
42
|
|
|
break; |
43
|
|
|
case "gif": |
44
|
|
|
$img = imagecreatefromgif($_FILES["Filedata"]["tmp_name"]); |
45
|
|
|
break; |
46
|
|
|
case "bmp": |
47
|
|
|
$img = imagecreatefromwbmp($_FILES["Filedata"]["tmp_name"]); |
48
|
|
|
break; |
49
|
|
|
default: |
50
|
|
|
echo "ERROR:image type error " . $_FILES["Filedata"]["tmp_name"]; |
51
|
|
|
exit(0); |
52
|
|
|
break; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!$img) { |
56
|
|
|
echo "ERROR:could not create image handle " . $_FILES["Filedata"]["tmp_name"]; |
57
|
|
|
exit(0); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$width = imageSX($img); |
61
|
|
|
$height = imageSY($img); |
62
|
|
|
|
63
|
|
|
if (!$width || !$height) { |
64
|
|
|
echo _AM_MARTIN_ERROR_INVALID_WIDTH_OR_HEIGHT; |
65
|
|
|
exit(0); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Build the thumbnail |
69
|
|
|
$target_width = 100; |
70
|
|
|
$target_height = 100; |
71
|
|
|
$target_ratio = $target_width / $target_height; |
72
|
|
|
|
73
|
|
|
$img_ratio = $width / $height; |
74
|
|
|
|
75
|
|
View Code Duplication |
if ($target_ratio > $img_ratio) { |
|
|
|
|
76
|
|
|
$new_height = $target_height; |
77
|
|
|
$new_width = $img_ratio * $target_height; |
78
|
|
|
} else { |
79
|
|
|
$new_height = $target_width / $img_ratio; |
80
|
|
|
$new_width = $target_width; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($new_height > $target_height) { |
84
|
|
|
$new_height = $target_height; |
85
|
|
|
} |
86
|
|
|
if ($new_width > $target_width) { |
87
|
|
|
$new_height = $target_width; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$new_img = ImageCreateTrueColor(100, 100); |
91
|
|
View Code Duplication |
if (!@imagefilledrectangle($new_img, 0, 0, $target_width - 1, $target_height - 1, 0)) { // Fill the image black |
|
|
|
|
92
|
|
|
echo _AM_MARTIN_ERROR_COULD_NOT_FILL_NEW_IMAGE; |
93
|
|
|
exit(0); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!@imagecopyresampled($new_img, $img, ($target_width - $new_width) / 2, ($target_height - $new_height) / 2, 0, 0, $new_width, $new_height, $width, $height)) { |
97
|
|
|
echo _AM_MARTIN_ERROR_COULD_NOT_RESIZE_IMAGE; |
98
|
|
|
exit(0); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!isset($_SESSION["file_info"])) { |
102
|
|
|
$_SESSION["file_info"] = array(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Use a output buffering to load the image into a variable |
106
|
|
|
ob_start(); |
107
|
|
|
// Get the image and create a thumbnail |
108
|
|
View Code Duplication |
switch ($FileType) { |
|
|
|
|
109
|
|
|
case "jpg": |
110
|
|
|
imagejpeg($new_img); |
111
|
|
|
break; |
112
|
|
|
case "png": |
113
|
|
|
imagepng($new_img); |
114
|
|
|
break; |
115
|
|
|
case "gif": |
116
|
|
|
imagegif($new_img); |
117
|
|
|
break; |
118
|
|
|
case "bmp": |
119
|
|
|
imagewbmp($new_img); |
120
|
|
|
break; |
121
|
|
|
} |
122
|
|
|
$imagevariable = ob_get_contents(); |
123
|
|
|
ob_end_clean(); |
124
|
|
|
|
125
|
|
|
$_SESSION["file_info"][$file_id] = $imagevariable; |
126
|
|
|
|
127
|
|
|
echo "FILEID:" . $file_id; // Return the file id to the script |
128
|
|
|
; |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.