aboutsummaryrefslogtreecommitdiff
path: root/classes/TidypicsImage.php
blob: c604d869edd5bf28016360d6cd2cc639593eec13 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/**
 * Tidypics Image class
 *
 * @package TidypicsImage
 * @author Cash Costello
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2
 */


class TidypicsImage extends ElggFile {
	protected function initialise_attributes() {
		parent::initialise_attributes();

		$this->attributes['subtype'] = "image";
	}

	public function __construct($guid = null) {
		parent::__construct($guid);
	}

	/**
	 *
	 * @warning container_guid must be set first
	 *
	 * @param array $data
	 * @return bool
	 */
	public function save($data = null) {

		if (!parent::save()) {
			return false;
		}

		if ($data) {
			// new image
			$this->simpletype = "image";
			$this->saveImageFile($data);
			$this->saveThumbnails();
		}

		return true;
	}

	/**
	 * Get the title of the image
	 *
	 * @return string
	 */
	public function getTitle() {
		return $this->title;
	}

	/**
	 * Get the src URL for the image
	 * 
	 * @return string
	 */
	public function getSrcUrl($size = 'small') {
		return "photos/thumbnail/$this->guid/$size/";
	}

	/**
	 * delete image
	 *
	 * @return bool
	 */
	public function delete() {

		// check if batch should be deleted
		$batch = elgg_get_entities_from_relationship(array(
			'relationship' => 'belongs_to_batch',
			'relationship_guid' => $this->guid,
			'inverse_relationship' => false,
		));
		if ($batch) {
			$batch = $batch[0];
			$count = elgg_get_entities_from_relationship(array(
				'relationship' => 'belongs_to_batch',
				'relationship_guid' => $batch->guid,
				'inverse_relationship' => true,
				'count' => true,
			));
			if ($count == 1) {
				// last image so delete batch
				$batch->delete();
			}
		}

		$album = get_entity($this->container_guid);
		if ($album) {
			$album->removeImage($this->guid);
		}

		$this->removeThumbnails();

		// update quota
		$owner = $this->getOwnerEntity();
		$owner->image_repo_size = (int)$owner->image_repo_size - $this->size();

		return parent::delete();
	}

	/**
	 * Set the internal filenames
	 */
	protected function setOriginalFilename($originalName) {
		$prefix = "image/" . $this->container_guid . "/";
		$filestorename = elgg_strtolower(time() . $originalName);
		$this->setFilename($prefix . $filestorename);
		$this->originalfilename = $originalName;
	}

	/**
	 * Save the uploaded image
	 * 
	 * @param array $data
	 */
	protected function saveImageFile($data) {
		$this->checkUploadErrors($data);

		// we need to make sure the directory for the album exists
		// @note for group albums, the photos are distributed among the users
		$dir = tp_get_img_dir() . $this->getContainerGUID();
		if (!file_exists($dir)) {
			mkdir($dir, 0755, true);
		}

		// move the uploaded file into album directory
		$this->setOriginalFilename($data['name']);
		$filename = $this->getFilenameOnFilestore();
		$result = move_uploaded_file($data['tmp_name'], $filename);
		if (!$result) {
			return false;
		}

		$owner = $this->getOwnerEntity();
		$owner->image_repo_size = (int)$owner->image_repo_size + $size;

		return true;
	}

	protected function checkUploadErrors($data) {
		// check for upload errors
		if ($data['error']) {
			if ($data['error'] == 1) {
				trigger_error('Tidypics warning: image exceeded server php upload limit', E_USER_WARNING);
				throw new Exception(elgg_echo('tidypics:image_mem'));
			} else {
				throw new Exception(elgg_echo('tidypics:unk_error'));
			}
		}

		// must be an image
		if (!tp_upload_check_format($data['type'])) {
			throw new Exception(elgg_echo('tidypics:not_image'));
		}

		// make sure file does not exceed memory limit
		if (!tp_upload_check_max_size($data['size'])) {
			throw new Exception(elgg_echo('tidypics:image_mem'));
		}

		// make sure the in memory image size does not exceed memory available
		$imginfo = getimagesize($data['tmp_name']);
		if (!tp_upload_memory_check($image_lib, $imginfo[0] * $imginfo[1])) {
			trigger_error('Tidypics warning: image memory size too large for resizing so rejecting', E_USER_WARNING);
			throw new Exception(elgg_echo('tidypics:image_pixels'));
		}
	}

	/**
	 * Save the image thumbnails
	 */
	protected function saveThumbnails() {
		elgg_load_library('tidypics:resize');

		$imageLib = elgg_get_plugin_setting('image_lib', 'tidypics');
		
		$prefix = "image/" . $this->container_guid . "/";
		$filename = $this->getFilename();
		$filename = substr($filename, strrpos($filename, '/') + 1);
		
		if ($imageLib == 'ImageMagick') {
			// ImageMagick command line
			if (tp_create_im_cmdline_thumbnails($this, $prefix, $filename) != true) {
				trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick command line', E_USER_WARNING);
			}
		} else if ($imageLib == 'ImageMagickPHP') {
			// imagick php extension
			if (tp_create_imagick_thumbnails($this, $prefix, $filename) != true) {
				trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick PHP', E_USER_WARNING);
			}
		} else {
			if (tp_create_gd_thumbnails($this, $prefix, $filename) != true) {
				trigger_error('Tidypics warning: failed to create thumbnails - GD', E_USER_WARNING);
			}
		}
	}

	/**
	 * Get the image data of a thumbnail
	 *
	 * @param string $size
	 * @return string
	 */
	public function getThumbnail($size) {
		switch ($size) {
			case 'thumb':
				$thumb = $this->thumbnail;
				break;
			case 'small':
				$thumb = $this->smallthumb;
				break;
			case 'large':
				$thumb = $this->largethumb;
				break;
			default:
				return '';
				break;
		}

		if (!$thumb) {
			return '';
		}

		$file = new ElggFile();
		$file->owner_guid = $this->getObjectOwnerGUID();
		$file->setFilename($thumb);
		return $file->grabFile();
	}

	/**
	 * Extract EXIF Data from image
	 *
	 * @warning image file must be saved first
	 */
	public function extractExifData() {
		include_once dirname(dirname(__FILE__)) . "/lib/exif.php";
		td_get_exif($this);
	}
	
	/**
	 * Has the photo been tagged with "in this photo" tags
	 *
	 * @return true/false
	 */
	public function isPhotoTagged() {
		$num_tags = count_annotations($this->getGUID(), 'object', 'image', 'phototag');
		if ($num_tags > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get an array of photo tag information
	 *
	 * @return array of json representations of the tags and the tag link text
	 */
	public function getPhotoTags() {
		global $CONFIG;

		// get tags as annotations
		$photo_tags = get_annotations($this->getGUID(), 'object', 'image', 'phototag');
		if (!$photo_tags) {
			// no tags or user doesn't have permission to tags, so return
			return false;
		}

		$photo_tags_json = "[";
		foreach ($photo_tags as $p) {
			$photo_tag = unserialize($p->value);

			// create link to page with other photos tagged with same tag
			$phototag_text = $photo_tag->value;
			$phototag_link = $CONFIG->wwwroot . 'search/?tag=' . $phototag_text . '&amp;subtype=image&amp;object=object';
			if ($photo_tag->type === 'user') {
				$user = get_entity($photo_tag->value);
				if ($user) {
					$phototag_text = $user->name;
				} else {
					$phototag_text = "unknown user";
				}

				$phototag_link = $CONFIG->wwwroot . "pg/photos/tagged/" . $photo_tag->value;
			}

			if (isset($photo_tag->x1)) {
				// hack to handle format of Pedro Prez's tags - ugh
				$photo_tag->coords = "\"x1\":\"{$photo_tag->x1}\",\"y1\":\"{$photo_tag->y1}\",\"width\":\"{$photo_tag->width}\",\"height\":\"{$photo_tag->height}\"";
				$photo_tags_json .= '{' . $photo_tag->coords . ',"text":"' . $phototag_text . '","id":"' . $p->id . '"},';
			} else {
				$photo_tags_json .= '{' . $photo_tag->coords . ',"text":"' . $phototag_text . '","id":"' . $p->id . '"},';
			}

			// prepare variable arrays for tagging view
			$photo_tag_links[$p->id] = array('text' => $phototag_text, 'url' => $phototag_link);
		}

		$photo_tags_json = rtrim($photo_tags_json,',');
		$photo_tags_json .= ']';

		$ret_data = array('json' => $photo_tags_json, 'links' => $photo_tag_links);
		return $ret_data;
	}

	/**
	 * Get the view information for this image
	 *
	 * @param $viewer_guid the guid of the viewer (0 if not logged in)
	 * @return array with number of views, number of unique viewers, and number of views for this viewer
	 */
	public function getViews($viewer_guid) {
		$views = get_annotations($this->getGUID(), "object", "image", "tp_view", "", 0, 99999);
		if ($views) {
			$total_views = count($views);

			if ($this->owner_guid == $viewer_guid) {
				// get unique number of viewers
				foreach ($views as $view) {
					$diff_viewers[$view->owner_guid] = 1;
				}
				$unique_viewers = count($diff_viewers);
			}
			else if ($viewer_guid) {
				// get the number of times this user has viewed the photo
				$my_views = 0;
				foreach ($views as $view) {
					if ($view->owner_guid == $viewer_guid) {
						$my_views++;
					}
				}
			}

			$view_info = array("total" => $total_views, "unique" => $unique_viewers, "mine" => $my_views);
		}
		else {
			$view_info = array("total" => 0, "unique" => 0, "mine" => 0);
		}

		return $view_info;
	}

	/**
	 * Add a tidypics view annotation to this image
	 *
	 * @param $viewer_guid
	 * @return none
	 */
	public function addView($viewer_guid) {
		if ($viewer_guid != $this->owner_guid && tp_is_person()) {
			create_annotation($this->getGUID(), "tp_view", "1", "integer", $viewer_guid, ACCESS_PUBLIC);
		}
	}

	/**
	 * Remove thumbnails - usually in preparation for deletion
	 *
	 * The thumbnails are not actually ElggObjects so we create
	 * temporary objects to delete them.
	 */
	protected function removeThumbnails() {
		$thumbnail = $this->thumbnail;
		$smallthumb = $this->smallthumb;
		$largethumb = $this->largethumb;

		//delete standard thumbnail image
		if ($thumbnail) {
			$delfile = new ElggFile();
			$delfile->owner_guid = $this->getOwner();
			$delfile->setFilename($thumbnail);
			$delfile->delete();
		}
		//delete small thumbnail image
		if ($smallthumb) {
			$delfile = new ElggFile();
			$delfile->owner_guid = $this->getOwner();
			$delfile->setFilename($smallthumb);
			$delfile->delete();
		}
		//delete large thumbnail image
		if ($largethumb) {
			$delfile = new ElggFile();
			$delfile->owner_guid = $this->getOwner();
			$delfile->setFilename($largethumb);
			$delfile->delete();
		}
	}
}