forked from ramonszo/2048-generator.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
73 lines (63 loc) · 1.8 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
<?php
$prettyError = '<script>alert("An error occurred while processing your image. Try again.");</script>';
if(!(int)$_POST['tile-num']){
echo $prettyError;
exit();
}
$filename = sha1(sha1(time().uniqid().md5(rand())).uniqid());
$thumbfile = uploadpic(false, 'images/'.$filename.'.jpg', 'tile-img', 107);
if(!$thumbfile){
echo $prettyError;
exit();
} else {
echo '<script>parent.uploadSuccess("'.(int)$_POST['tile-num'].'", "'.$filename.'");</script>';
exit();
}
function uploadpic($file,$thumb_file,$field,$thumb_width=100){
$result=false;
if(is_uploaded_file($_FILES[$field]['tmp_name']))
{
//move_uploaded_file($_FILES[$field]['tmp_name'],$file);
resize($_FILES[$field]['tmp_name'],$thumb_file,$thumb_width);
$result=true;
}
return $result;
}
function resize($orig_file,$thumb_file,$prop){
$img = $orig_file;
$constrain = true;
$w = $prop;
$h = $prop;
// get image size of img
$x = @getimagesize($img);
// image width
$sw = $x[0];
// image height
$sh = $x[1];
if($sw > $sh){
$ratio = $sw/$sh;
$h = $prop;
$w = round($prop * $ratio);
} else {
$ratio = $sh/$sw;
$w = $prop;
$h = round($prop * $ratio);
}
$im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
$im = @ImageCreateFromPNG ($img) or // or PNG Image
$im = @ImageCreateFromGIF ($img) or // or GIF Image
$im = false; // If image is not JPEG, PNG, or GIF
if (!$im) {
// We get errors from PHP's ImageCreate functions...
// So let's echo back the contents of the actual image.
readfile ($img);
} else {
// Create the resized image destination
$thumb = @ImageCreateTrueColor ($w, $h);
// Copy from image source, resize it, and paste to image destination
@ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
// Output resized image
@ImageJPEG ($thumb,$thumb_file);
}
}
?>