aboutsummaryrefslogtreecommitdiff
path: root/mod/lightbox/lib/lightbox.php
blob: 6b557d241b867745ff33a306ecd1063693f88dc7 (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
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
<?php
/**
 * Lightbox helper functions
 *
 * @package ElggLightbox
 */

/**
 * Save uploaded images and return an array of its entities
 *
 * @return array of LightboxPluginImages
 */
function lightbox_get_image_inputs() {
	
	$files = array();
	$file = new ElggFile();
	
	// check if upload failed
	foreach($_FILES as $key => $sent_file) {
		if (empty($sent_file['name'])) {
			continue;
		}
		if($sent_file['error'] != 0) {
			register_error(elgg_echo('file:cannotload'));
			forward(REFERER);
		}
		$mime_type = $file->detectMimeType($sent_file['tmp_name'], $sent_file['type']);
		if(file_get_simple_type($mime_type) != "image"){
			register_error(elgg_echo('lightbox:noimage'));
			forward(REFERER);
		}
	}
	
	foreach($_FILES as $key => $sent_file) {
		
		if (empty($sent_file['name'])) {
			continue;
		}
		
		$file = new LightboxPluginImage();
		
		$prefix = "image/";
		$filestorename = elgg_strtolower(time().$sent_file['name']);

		$mime_type = $file->detectMimeType($sent_file['tmp_name'], $sent_file['type']);
		$file->setFilename($prefix . $filestorename);
		$file->setMimeType($mime_type);
		$file->originalfilename = $sent_file['name'];
		$file->simpletype = "image";

		// Open the file to guarantee the directory exists
		$file->open("write");
		$file->close();
		move_uploaded_file($sent_file['tmp_name'], $file->getFilenameOnFilestore());

		// We need to create thumbnails (this should be moved into a function)
		if ($file->save()) {
			$thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),60,60, true);
			if ($thumbnail) {
				$thumb = new ElggFile();
				$thumb->setMimeType($_FILES['upload']['type']);

				$thumb->setFilename($prefix."thumb".$filestorename);
				$thumb->open("write");
				$thumb->write($thumbnail);
				$thumb->close();

				$file->thumbnail = $prefix."thumb".$filestorename;
				unset($thumbnail);
			}

			$thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),153,153, true);
			if ($thumbsmall) {
				$thumb->setFilename($prefix."smallthumb".$filestorename);
				$thumb->open("write");
				$thumb->write($thumbsmall);
				$thumb->close();
				$file->smallthumb = $prefix."smallthumb".$filestorename;
				unset($thumbsmall);
			}

			$thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),600,600, false);
			if ($thumblarge) {
				$thumb->setFilename($prefix."largethumb".$filestorename);
				$thumb->open("write");
				$thumb->write($thumblarge);
				$thumb->close();
				$file->largethumb = $prefix."largethumb".$filestorename;
				unset($thumblarge);
			}
			
			$files[$file->guid] = $file;
		}
	}
	
	return $files;
}

/**
 * Delete uploaded images if album creation fails
 *
 * @params array  $images Array of LightboxPluginImages
 * @return null
 */
function lightbox_delete_image_inputs($images) {
	//TODO
}