annamaria-daneswood-25102012 /components/com_myblog/libraries/imagebrowser.class.php

Language PHP Lines 364
MD5 Hash 116a5285dbac39c355613877a482e148 Estimated Cost $5,458 (why?)
Repository https://bitbucket.org/dgough/annamaria-daneswood-25102012.git View Raw File View Project SPDX
  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
<?php
(defined('_VALID_MOS') OR defined('_JEXEC')) or die('Direct Access to this location is not allowed.');

// If the memory limit is too low, increase it 
if(ini_get('memory_limit') == '8M')
	@ini_set('memory_limit', '16M');

include_once(MY_LIBRARY_PATH . '/mediabrowser.class.php');

class MYMediaBrowser extends MYMedia{
	var $extension			= '';
	var $imagePath			= '';
	
	var $_thumbnail			= array(
										'width'		=> '44',
										'folder'	=> '',
										'folderup'	=> '',
										'unknown'	=> '' 
									);
	var $_allowedExtensions = array( 
										'GIF', 'JPG', 'JPEG', 'PNG','SWF','PSD',
										'BMP','TIFF','TIFF','JPC','JP2','ICO',
										'JPX','JB2','SWC','IFF','WBMP','XBM'
									);
	var $_imageExtensions	= array( 'jpeg', 'gif', 'png', 'gd', 'gd2',
									'im','xbm','xpm');
	var $_cache				= '';
	
	// Key are the extensions and value should represent their thumbnails
	var $_documents			= array(
										'pdf'	=> '',	// PDF
										'doc'	=> '',	// Word
										'xls'	=> '',	// Excel 
										'ppt'	=> '',	// Powerpoint
										'log'	=> '',	// Log files
										'txt'	=> '',	// Text files
										'rtf'	=> '',	// Rich Txt Format
										'xml'	=> ''	// XML
									);

	function MYMediaBrowser($filepath = ''){
		parent::MYMedia();
		$this->imagePath	= urldecode($filepath);
		$this->_cache		= MY_CACHE_PATH;
		
		$this->_thumbnail['folder']		= $this->cms->get_path('live') . '/components/com_myblog/images/bigfolder.gif';
		$this->_thumbnail['folderup']	= $this->cms->get_path('live') . '/components/com_myblog/images/folderup.gif';
		$this->_thumbnail['unknown']	= $this->cms->get_path('live') . '/components/com_myblog/images/files.png';

		// Configure here to set the thumbnails for the extensions
		$this->_documents['pdf']	= MY_COM_LIVE . '/images/pdf.gif';
		$this->_documents['doc']	= MY_COM_LIVE . '/images/text.gif';
		$this->_documents['log']	= MY_COM_LIVE . '/images/text.gif';
		$this->_documents['txt']	= MY_COM_LIVE . '/images/text.gif';
		$this->_documents['rtf']	= MY_COM_LIVE . '/images/text.gif';
		$this->_documents['xls']	= MY_COM_LIVE . '/images/excel.gif';
		$this->_documents['xlsx']	= MY_COM_LIVE . '/images/excel.gif';
		$this->_documents['ppt']	= MY_COM_LIVE . '/images/ppt.gif';
		$this->_documents['pptx']	= MY_COM_LIVE . '/images/ppt.gif';
	}

	function upload($file, $resize = false){
		global $_MY_CONFIG;

		$fileName	= strtolower($file['name']);
		$save		= $this->media . '/' . $file['name'];
		
		$error		= '';
		$msg		= '';
		
		if(strpos(strtolower($_MY_CONFIG->get('allowedUploadFileType')), substr($fileName, -3)) === false){
			$error	= 'The file must have an extension of ' . $_MY_CONFIG->get('allowedUploadFileType'); 
		}
		
		if($file['size'] >= ($_MY_CONFIG->get('uploadSizeLimit') * 1024)){
			$error	= 'File size too large.';
		}
		
		if(file_exists($save)){
			$error	= 'File exists.';
		}
		
		if(empty($error))
			move_uploaded_file($file['tmp_name'], $save);
			
		// Resize images
		
		if($_MY_CONFIG->get('enableImageResize') && $_MY_CONFIG->get('maxWidth') && $resize == '1'){
			$image		= $this->_resize($save, $_MY_CONFIG->get('maxWidth'));
			
			// Get the mime type for this image.
		 	$mimetype	= $this->_readImage($save, true);
		 	
			// Save new image
			ob_start();
	
			if ($mimetype == "gif"){
				imagegif ($image);
			}
			else if ($mimetype == "png"){
				imagepng ($image, '', 7);
			}
			else{
				imagejpeg ($image, '', 80);
			}
			$content	= ob_get_contents();
			while( @ob_end_clean() );
	
			// Remove old image
			@unlink($save);
		
			$this->_write($save, $content);
		}
		$msg	= 'File ' . $file['name'] . ' uploaded successfully.';
		// For security reason, we force to remove all uploaded file
		@unlink($file['tmp_name']);
		
		return array('error' => $error, 'msg' => $msg , 'source' => $this->mediaURI . '/' . $file['name'] );
	}
	
	function _getHeaderType($extension = 'bmp'){
		$headers		= 'Content-type: image/';
		$knownHeaders	= array('png','gif','jpg');
		
		if(!in_array($extension, $knownHeaders))
			$headers	.= 'bmp';
		else
			$headers	.= $extension;

		return $headers;
	}

	function _readImage($path, $getExtension = false){
	
		foreach($this->_imageExtensions as $extension){
			// Try to create the image from the specific extension
			$func	= 'imagecreatefrom' . strtolower($extension);
			$image	= @$func($path);
			if($image !== false ) return ($getExtension) ? $extension : $image;
		}

		// Try and load from string:
		$im = @imagecreatefromstring(file_get_contents($file));
		if ($im !== false){
			return $im;
		}
		return false;
	}

	function _openCachedImageFile($path){
		$handle		= fopen($path, 'r');
		$contents	= fread($handle, filesize($path));
		
		fclose($handle);
		return $contents;
	}
	
	function _resize($path, $maxWidth){
		$image	= $this->_readImage($path);

		if($image === false)
			return false;

		$width	= imagesx($image);
		$height	= imagesy($image);
		
		if($width > $maxWidth){
			$newWidth	= $maxWidth;
			$newHeight	= intval($maxWidth * $height / $width);
		} else {
			$newWidth	= $width;
			$newHeight	= $height;
		}
		
		$newImage	= imagecreatetruecolor($newWidth, $newHeight);
		$background	= imagecolorallocate($newImage, 0, 0, 0);

		// For GIF's and PNG's set the transparency and turn off alpha blending
		if($this->_readImage($path, true) == 'gif' || $this->_readImage($path, true) == 'png'){
			// Set thumbnail to be transparent
			ImageColorTransparent($newImage, $background);
		
			// Turn off alpha blending to keep alpha channel
			imagealphablending($newImage, false);
		}
		imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
		return $newImage;
	}
	
	function thumbnail($maxWidth = 60){
		//$imagepath	= urldecode($image);

		$cached	= $this->_cache . '/cache_img' . md5($this->imagePath);

		// Get image extension
		$extension	= strtolower(pathinfo($this->imagePath, PATHINFO_EXTENSION));

		if(file_exists($cached)){
			header($this->_getHeaderType($extension));
			echo $this->_openCachedImageFile($cached);
		} else {
			$image	= $this->_resize($this->imagePath, $maxWidth);

			if($image === false){
				// Display a not known thumbnail
				$extension	= strtolower(pathinfo($this->_thumbnail['unknown'], PATHINFO_EXTENSION));
				header($this->_getHeaderType($extension));
				echo $this->mnOpenImage($this->_thumbnail['unknown']);
			} else {
				// Display
				ob_start();
	
				// Get the mime type for this image.
	 			$mimetype	= $this->_readImage($this->imagePath, true);

			     if ($mimetype == "gif"){
				    header("Content-type: image/gif");
			 		imagegif ($image);
				 }
			     else if ($mimetype == "png"){
				    header("Content-type: image/png");
			 		imagepng ($image, '', 7);
				 }
			     else{
				    header("Content-type: image/jpeg");
			 		imagejpeg ($image, '', 80);
				 }
				
				// need to check the format
				$thumb	= ob_get_contents();
				ob_end_flush();
				
				// Write to cache file
				$this->_write($cached, $thumb);
				echo $thumb;
			}
		}
		die();
	}
	
	function _write($location, $content){
		$handle	= fopen($location, 'w');
		
		// If we can't write cache data to the file, return false
		if(!$handle)
			return false;
		
		// Proceed in storing the cached content
		if(fwrite($handle, $content) === FALSE)
			return false;
		
		// Here we assume the file gets stored correctly, now close it.
		fclose($handle);
		return true;
	}

// 	function _processFileAdvanced($file, $cwd){
// 		$filepath	= $this->media . rtrim($cwd, '/') . '/' . $file;
// 
// 		$image		= '';
// 
// 		$extension	= $this->extension($filepath);
// 		
// 		if(array_key_exists($extension, $this->_documents)){
// 			// Process documents
// 			$location	= $this->mediaURI . '/' . ltrim($file, '\\/');
// 			$image		= $this->_documents[$extension];
// 			$event		= 'myblog.mediaPreview(\\\'' . $image . '\\\',\\\'' . $location . '\\\');';
// 
// 			$file		= $file;
// 		} else {
// 			// Process images
// 			if(@list($width, $height) = getimagesize($filepath)){
// 	
// 				$image	= rtrim($this->mediaURI , '\\/') . '/' . ltrim($file, '\\/');
// 				
// 				// Image is larger than thumbnail, then we need to thumbnail the image
// 				if($width > $this->_thumbnail['width']){
// 					// Check if thumbnail image is created.
// 					$thumbnail	= rtrim($cwd, '\\/') . '/' . ltrim($file, '\\/');
// 					$image		= $this->cms->get_path('live') . '/index2.php?option=com_myblog&task=thumb&maxwidth=' . $this->_thumbnail['width'] . '&fn=' . urlencode($filepath);
// 				}
// 			}
// 	
// 			// Check if this file is a known image.
// 			$extension	= strtoupper(pathinfo($filepath, PATHINFO_EXTENSION));
// 	
// 			// We don't want to display unknown extensions since they might not be images
// 			// and we 
// 			if(!in_array($extension, $this->_allowedExtensions)){
// 				$image			= $this->_thumbnail['unknown'];
// 				$imageSource	= $this->_thumbnail['unknown'];
// 			} else {
// 				// Get image source file for known images.
// 				$imageSource	= $this->mediaURI . ltrim(rtrim($cwd,'/'), '/') . '/' . $file;
// 			}
// 			$event	= 'myblog.attachImage(\\\'' . $imageSource . '\\\',\\\'' . $imageSource . '\\\');';
// 		}
// 		
// // 		$items	= array(
// // 						'image'		=> $image,
// // 						'e'		=> $event,
// // 						'd'		=> $event,
// // 						'title'		=> $file,
// // 						'caption'	=> $file
// // 						);
// // 		return $items;
// 	}
	
	function _processFile($file, $cwd){
		$filepath	= $this->media . rtrim($cwd, '/') . '/' . $file;

		$image		= '';
		$extension	= $this->extension($filepath);
		
		if(array_key_exists($extension, $this->_documents)){
			// Process documents
			$location	= $this->mediaURI . '/' . ltrim($file, '\\/');
			$image		= $this->_documents[$extension];
			//$event		= 'myblog.attachDocument(\'' . $image . '\',\'' . $location . '\');';
			$event		= 'ad';
			$eventData 	= array($image, $location);
			$file		= $file;
		} else {
			// Process images
			if(@list($width, $height) = getimagesize($filepath)){
	
				$image	= rtrim($this->mediaURI , '\\/') . '/' . ltrim($file, '\\/');
				
				// Image is larger than thumbnail, then we need to thumbnail the image
				if($width > $this->_thumbnail['width']){
					// Check if thumbnail image is created.
					$thumbnail	= rtrim($cwd, '\\/') . '/' . ltrim($file, '\\/');
					$image		= $this->cms->get_path('live') . '/index2.php?option=com_myblog&task=thumb&maxwidth=' . $this->_thumbnail['width'] . '&fn=' . urlencode($filepath);
				}
			}
	
			// Check if this file is a known image.
			$extension	= strtoupper(pathinfo($filepath, PATHINFO_EXTENSION));
	
			// We don't want to display unknown extensions since they might not be images
			// and we 
			if(!in_array($extension, $this->_allowedExtensions)){
				$image			= $this->_thumbnail['unknown'];
				$imageSource	= $this->_thumbnail['unknown'];
			} else {
				// Get image source file for known images.
				$imageSource	= $this->mediaURI . '/' . ltrim(rtrim($cwd,'/'), '/') . '/' . $file;
			}
			$event	= 'ai';//'myblog.attachImage(\'' . $imageSource . '\');';
			$eventData = $imageSource;
		}
		
		$items	= array(
						'i'		=> $image,
						'e'		=> $event,
						'd'		=> $eventData,
						't'		=> $file,
						'c'		=> $file
						);
		return $items;
	}
}
Back to Top