aboutsummaryrefslogtreecommitdiff
path: root/lib/upload.php
blob: a7dd7611d19c2372293c26a0fbf8324b211af070 (plain)
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
<?php
/**
 * Helper library for working with uploads
 */

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;
}

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;
}