This repository was archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils-pic.php
More file actions
109 lines (91 loc) · 3.1 KB
/
utils-pic.php
File metadata and controls
109 lines (91 loc) · 3.1 KB
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
<?php // vim:set ts=4 sw=4 sts=4 et:
require_once "config.php";
use Aws\S3\S3Client;
function pictureHandling($id, $picture) {
if ($picture == NULL) {
return ""; // No file uploaded
}
//echo 'valid picture <br />';
if ($picture['size'] == 0) {
echo "Problem: uploaded file is zero length";
return "";
}
if (($picture['type'] != "image/jpeg") &&
($picture['type'] != "image/jpg") &&
($picture['type'] != "image/png") &&
($picture['type'] != "image/gif")) {
echo "Problem: file is not a proper png, gif, jpg, or jpeg";
return "";
}
if (!is_uploaded_file($picture['tmp_name'])) {
echo "Problem: possible file upload attack";
return "";
}
$upfile = picName($id, $picture['name']);
$thumb = thumbName($id);
if (USING_AWS) {
$client = S3Client::factory(array(
'endpoint' => AWS_ENDPOINT,
'key' => AWS_ACCESS_KEY,
'secret' => AWS_SECRET_KEY));
}
if (!move_uploaded_file($picture['tmp_name'], $upfile)) {
echo "Problem: Could not move picture into pictures directory";
return "";
} elseif (USING_AWS) {
$key = $upfile;
// TODO: Nobody is reading this result; the site proceeds to
// link to the bucket on the assumption that this succeeded.
$result = $client->putObject(array(
'Bucket' => AWS_BUCKET,
'Key' => $key,
'Body' => file_get_contents($upfile),
'ContentDisposition' => 'inline'));
}
makeThumb($upfile, $thumb);
if (USING_AWS) {
$key = $thumb;
// TODO: Nobody is reading this result; the site proceeds to
// link to the bucket on the assumption that this succeeded.
$result = $client->putObject(array(
'Bucket' => AWS_BUCKET,
'Key' => $key,
'Body' => file_get_contents($key),
'ContentDisposition' => 'inline'));
}
return $upfile;
}
function picName($id, $name) {
return PICPATH . $id . "--" . $name;
}
function thumbName($id) {
return PICPATH . "thumbs/$id.jpg";
}
function makeThumb($uploaded, $thumbName) {
$maxW = 120;
$maxH = 120;
list($width, $height, $type) = getimagesize($uploaded);
// If the image is too big, scale it down
// From kvslaap on http://us2.php.net/manual/en/function.imagecopyresized.php
$imgratio = ($width / $height);
if ($imgratio > 1) {
$newW = $maxW;
$newH = ($maxW / $imgratio);
} else {
$newH = $maxH;
$newW = ($maxH * $imgratio);
}
$thumb = imagecreatetruecolor($newW, $newH);
if ($type == IMAGETYPE_JPEG) {
$source = imagecreatefromjpeg($uploaded);
} elseif ($type == IMAGETYPE_GIF) {
$source = imagecreatefromgif ($uploaded);
} elseif ($type == IMAGETYPE_PNG) {
$source = imagecreatefrompng($uploaded);
} else {
echo "Unrecognized file type.";
exit(1);
}
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newW, $newH, $width, $height);
imagejpeg($thumb, $thumbName);
}