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
|
<?php
/**
* Helper library for working with uploads
*/
/**
* Guess on the mimetype based on file extension
*
* @param string $originalName
* @return string
*/
function tp_upload_get_mimetype($originalName) {
$extension = substr(strrchr($originalName, '.'), 1);
switch ($extension) {
case 'png':
return 'image/png';
break;
case 'gif':
return 'image/gif';
break;
case 'jpg':
default:
return 'image/jpeg';
break;
}
}
/**
* Check if this is an image
*
* @param string $mime
* @return bool false = not image
*/
function tp_upload_check_format($mime) {
$accepted_formats = array(
'image/jpeg',
'image/png',
'image/gif',
'image/pjpeg',
'image/x-png',
);
if (!in_array($mime, $accepted_formats)) {
return false;
}
return true;
}
/**
* Check if there is enough memory to process this image
*
* @param string $image_lib
* @param int $num_pixels
* @return bool false = not enough memory
*/
function tp_upload_memory_check($image_lib, $num_pixels) {
if ($image_lib !== 'GD') {
return true;
}
$mem_avail = ini_get('memory_limit');
$mem_avail = rtrim($mem_avail, 'M');
$mem_avail = $mem_avail * 1024 * 1024;
$mem_used = memory_get_usage();
$mem_required = ceil(5.35 * $num_pixels);
$mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer
if ($mem_required > $mem_avail) {
return false;
}
return true;
}
/**
* Check if image is within limits
*
* @param int $image_size
* @return bool false = too large
*/
function tp_upload_check_max_size($image_size) {
$max_file_size = (float) get_plugin_setting('maxfilesize','tidypics');
if (!$max_file_size) {
// default to 5 MB if not set
$max_file_size = 5;
}
// convert to bytes from MBs
$max_file_size = 1024 * 1024 * $max_file_size;
return $image_size <= $max_file_size;
}
/**
* Check if this image pushes user over quota
*
* @param int $image_size
* @param int $owner_guid
* @return bool false = exceed quota
*/
function tp_upload_check_quota($image_size, $owner_guid) {
static $quota;
if (!isset($quota)) {
$quota = get_plugin_setting('quota','tidypics');
$quota = 1024 * 1024 * $quota;
}
if ($quota == 0) {
// no quota
return true;
}
$image_repo_size_md = get_metadata_byname($owner_guid, "image_repo_size");
$image_repo_size = (int)$image_repo_size_md->value;
return ($image_repo_size + $image_size) < $quota;
}
|