PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_myblog/libraries/imagebrowser.class.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 363 lines | 219 code | 60 blank | 84 comment | 42 complexity | 116a5285dbac39c355613877a482e148 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. (defined('_VALID_MOS') OR defined('_JEXEC')) or die('Direct Access to this location is not allowed.');
  3. // If the memory limit is too low, increase it
  4. if(ini_get('memory_limit') == '8M')
  5. @ini_set('memory_limit', '16M');
  6. include_once(MY_LIBRARY_PATH . '/mediabrowser.class.php');
  7. class MYMediaBrowser extends MYMedia{
  8. var $extension = '';
  9. var $imagePath = '';
  10. var $_thumbnail = array(
  11. 'width' => '44',
  12. 'folder' => '',
  13. 'folderup' => '',
  14. 'unknown' => ''
  15. );
  16. var $_allowedExtensions = array(
  17. 'GIF', 'JPG', 'JPEG', 'PNG','SWF','PSD',
  18. 'BMP','TIFF','TIFF','JPC','JP2','ICO',
  19. 'JPX','JB2','SWC','IFF','WBMP','XBM'
  20. );
  21. var $_imageExtensions = array( 'jpeg', 'gif', 'png', 'gd', 'gd2',
  22. 'im','xbm','xpm');
  23. var $_cache = '';
  24. // Key are the extensions and value should represent their thumbnails
  25. var $_documents = array(
  26. 'pdf' => '', // PDF
  27. 'doc' => '', // Word
  28. 'xls' => '', // Excel
  29. 'ppt' => '', // Powerpoint
  30. 'log' => '', // Log files
  31. 'txt' => '', // Text files
  32. 'rtf' => '', // Rich Txt Format
  33. 'xml' => '' // XML
  34. );
  35. function MYMediaBrowser($filepath = ''){
  36. parent::MYMedia();
  37. $this->imagePath = urldecode($filepath);
  38. $this->_cache = MY_CACHE_PATH;
  39. $this->_thumbnail['folder'] = $this->cms->get_path('live') . '/components/com_myblog/images/bigfolder.gif';
  40. $this->_thumbnail['folderup'] = $this->cms->get_path('live') . '/components/com_myblog/images/folderup.gif';
  41. $this->_thumbnail['unknown'] = $this->cms->get_path('live') . '/components/com_myblog/images/files.png';
  42. // Configure here to set the thumbnails for the extensions
  43. $this->_documents['pdf'] = MY_COM_LIVE . '/images/pdf.gif';
  44. $this->_documents['doc'] = MY_COM_LIVE . '/images/text.gif';
  45. $this->_documents['log'] = MY_COM_LIVE . '/images/text.gif';
  46. $this->_documents['txt'] = MY_COM_LIVE . '/images/text.gif';
  47. $this->_documents['rtf'] = MY_COM_LIVE . '/images/text.gif';
  48. $this->_documents['xls'] = MY_COM_LIVE . '/images/excel.gif';
  49. $this->_documents['xlsx'] = MY_COM_LIVE . '/images/excel.gif';
  50. $this->_documents['ppt'] = MY_COM_LIVE . '/images/ppt.gif';
  51. $this->_documents['pptx'] = MY_COM_LIVE . '/images/ppt.gif';
  52. }
  53. function upload($file, $resize = false){
  54. global $_MY_CONFIG;
  55. $fileName = strtolower($file['name']);
  56. $save = $this->media . '/' . $file['name'];
  57. $error = '';
  58. $msg = '';
  59. if(strpos(strtolower($_MY_CONFIG->get('allowedUploadFileType')), substr($fileName, -3)) === false){
  60. $error = 'The file must have an extension of ' . $_MY_CONFIG->get('allowedUploadFileType');
  61. }
  62. if($file['size'] >= ($_MY_CONFIG->get('uploadSizeLimit') * 1024)){
  63. $error = 'File size too large.';
  64. }
  65. if(file_exists($save)){
  66. $error = 'File exists.';
  67. }
  68. if(empty($error))
  69. move_uploaded_file($file['tmp_name'], $save);
  70. // Resize images
  71. if($_MY_CONFIG->get('enableImageResize') && $_MY_CONFIG->get('maxWidth') && $resize == '1'){
  72. $image = $this->_resize($save, $_MY_CONFIG->get('maxWidth'));
  73. // Get the mime type for this image.
  74. $mimetype = $this->_readImage($save, true);
  75. // Save new image
  76. ob_start();
  77. if ($mimetype == "gif"){
  78. imagegif ($image);
  79. }
  80. else if ($mimetype == "png"){
  81. imagepng ($image, '', 7);
  82. }
  83. else{
  84. imagejpeg ($image, '', 80);
  85. }
  86. $content = ob_get_contents();
  87. while( @ob_end_clean() );
  88. // Remove old image
  89. @unlink($save);
  90. $this->_write($save, $content);
  91. }
  92. $msg = 'File ' . $file['name'] . ' uploaded successfully.';
  93. // For security reason, we force to remove all uploaded file
  94. @unlink($file['tmp_name']);
  95. return array('error' => $error, 'msg' => $msg , 'source' => $this->mediaURI . '/' . $file['name'] );
  96. }
  97. function _getHeaderType($extension = 'bmp'){
  98. $headers = 'Content-type: image/';
  99. $knownHeaders = array('png','gif','jpg');
  100. if(!in_array($extension, $knownHeaders))
  101. $headers .= 'bmp';
  102. else
  103. $headers .= $extension;
  104. return $headers;
  105. }
  106. function _readImage($path, $getExtension = false){
  107. foreach($this->_imageExtensions as $extension){
  108. // Try to create the image from the specific extension
  109. $func = 'imagecreatefrom' . strtolower($extension);
  110. $image = @$func($path);
  111. if($image !== false ) return ($getExtension) ? $extension : $image;
  112. }
  113. // Try and load from string:
  114. $im = @imagecreatefromstring(file_get_contents($file));
  115. if ($im !== false){
  116. return $im;
  117. }
  118. return false;
  119. }
  120. function _openCachedImageFile($path){
  121. $handle = fopen($path, 'r');
  122. $contents = fread($handle, filesize($path));
  123. fclose($handle);
  124. return $contents;
  125. }
  126. function _resize($path, $maxWidth){
  127. $image = $this->_readImage($path);
  128. if($image === false)
  129. return false;
  130. $width = imagesx($image);
  131. $height = imagesy($image);
  132. if($width > $maxWidth){
  133. $newWidth = $maxWidth;
  134. $newHeight = intval($maxWidth * $height / $width);
  135. } else {
  136. $newWidth = $width;
  137. $newHeight = $height;
  138. }
  139. $newImage = imagecreatetruecolor($newWidth, $newHeight);
  140. $background = imagecolorallocate($newImage, 0, 0, 0);
  141. // For GIF's and PNG's set the transparency and turn off alpha blending
  142. if($this->_readImage($path, true) == 'gif' || $this->_readImage($path, true) == 'png'){
  143. // Set thumbnail to be transparent
  144. ImageColorTransparent($newImage, $background);
  145. // Turn off alpha blending to keep alpha channel
  146. imagealphablending($newImage, false);
  147. }
  148. imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  149. return $newImage;
  150. }
  151. function thumbnail($maxWidth = 60){
  152. //$imagepath = urldecode($image);
  153. $cached = $this->_cache . '/cache_img' . md5($this->imagePath);
  154. // Get image extension
  155. $extension = strtolower(pathinfo($this->imagePath, PATHINFO_EXTENSION));
  156. if(file_exists($cached)){
  157. header($this->_getHeaderType($extension));
  158. echo $this->_openCachedImageFile($cached);
  159. } else {
  160. $image = $this->_resize($this->imagePath, $maxWidth);
  161. if($image === false){
  162. // Display a not known thumbnail
  163. $extension = strtolower(pathinfo($this->_thumbnail['unknown'], PATHINFO_EXTENSION));
  164. header($this->_getHeaderType($extension));
  165. echo $this->mnOpenImage($this->_thumbnail['unknown']);
  166. } else {
  167. // Display
  168. ob_start();
  169. // Get the mime type for this image.
  170. $mimetype = $this->_readImage($this->imagePath, true);
  171. if ($mimetype == "gif"){
  172. header("Content-type: image/gif");
  173. imagegif ($image);
  174. }
  175. else if ($mimetype == "png"){
  176. header("Content-type: image/png");
  177. imagepng ($image, '', 7);
  178. }
  179. else{
  180. header("Content-type: image/jpeg");
  181. imagejpeg ($image, '', 80);
  182. }
  183. // need to check the format
  184. $thumb = ob_get_contents();
  185. ob_end_flush();
  186. // Write to cache file
  187. $this->_write($cached, $thumb);
  188. echo $thumb;
  189. }
  190. }
  191. die();
  192. }
  193. function _write($location, $content){
  194. $handle = fopen($location, 'w');
  195. // If we can't write cache data to the file, return false
  196. if(!$handle)
  197. return false;
  198. // Proceed in storing the cached content
  199. if(fwrite($handle, $content) === FALSE)
  200. return false;
  201. // Here we assume the file gets stored correctly, now close it.
  202. fclose($handle);
  203. return true;
  204. }
  205. // function _processFileAdvanced($file, $cwd){
  206. // $filepath = $this->media . rtrim($cwd, '/') . '/' . $file;
  207. //
  208. // $image = '';
  209. //
  210. // $extension = $this->extension($filepath);
  211. //
  212. // if(array_key_exists($extension, $this->_documents)){
  213. // // Process documents
  214. // $location = $this->mediaURI . '/' . ltrim($file, '\\/');
  215. // $image = $this->_documents[$extension];
  216. // $event = 'myblog.mediaPreview(\\\'' . $image . '\\\',\\\'' . $location . '\\\');';
  217. //
  218. // $file = $file;
  219. // } else {
  220. // // Process images
  221. // if(@list($width, $height) = getimagesize($filepath)){
  222. //
  223. // $image = rtrim($this->mediaURI , '\\/') . '/' . ltrim($file, '\\/');
  224. //
  225. // // Image is larger than thumbnail, then we need to thumbnail the image
  226. // if($width > $this->_thumbnail['width']){
  227. // // Check if thumbnail image is created.
  228. // $thumbnail = rtrim($cwd, '\\/') . '/' . ltrim($file, '\\/');
  229. // $image = $this->cms->get_path('live') . '/index2.php?option=com_myblog&task=thumb&maxwidth=' . $this->_thumbnail['width'] . '&fn=' . urlencode($filepath);
  230. // }
  231. // }
  232. //
  233. // // Check if this file is a known image.
  234. // $extension = strtoupper(pathinfo($filepath, PATHINFO_EXTENSION));
  235. //
  236. // // We don't want to display unknown extensions since they might not be images
  237. // // and we
  238. // if(!in_array($extension, $this->_allowedExtensions)){
  239. // $image = $this->_thumbnail['unknown'];
  240. // $imageSource = $this->_thumbnail['unknown'];
  241. // } else {
  242. // // Get image source file for known images.
  243. // $imageSource = $this->mediaURI . ltrim(rtrim($cwd,'/'), '/') . '/' . $file;
  244. // }
  245. // $event = 'myblog.attachImage(\\\'' . $imageSource . '\\\',\\\'' . $imageSource . '\\\');';
  246. // }
  247. //
  248. // // $items = array(
  249. // // 'image' => $image,
  250. // // 'e' => $event,
  251. // // 'd' => $event,
  252. // // 'title' => $file,
  253. // // 'caption' => $file
  254. // // );
  255. // // return $items;
  256. // }
  257. function _processFile($file, $cwd){
  258. $filepath = $this->media . rtrim($cwd, '/') . '/' . $file;
  259. $image = '';
  260. $extension = $this->extension($filepath);
  261. if(array_key_exists($extension, $this->_documents)){
  262. // Process documents
  263. $location = $this->mediaURI . '/' . ltrim($file, '\\/');
  264. $image = $this->_documents[$extension];
  265. //$event = 'myblog.attachDocument(\'' . $image . '\',\'' . $location . '\');';
  266. $event = 'ad';
  267. $eventData = array($image, $location);
  268. $file = $file;
  269. } else {
  270. // Process images
  271. if(@list($width, $height) = getimagesize($filepath)){
  272. $image = rtrim($this->mediaURI , '\\/') . '/' . ltrim($file, '\\/');
  273. // Image is larger than thumbnail, then we need to thumbnail the image
  274. if($width > $this->_thumbnail['width']){
  275. // Check if thumbnail image is created.
  276. $thumbnail = rtrim($cwd, '\\/') . '/' . ltrim($file, '\\/');
  277. $image = $this->cms->get_path('live') . '/index2.php?option=com_myblog&task=thumb&maxwidth=' . $this->_thumbnail['width'] . '&fn=' . urlencode($filepath);
  278. }
  279. }
  280. // Check if this file is a known image.
  281. $extension = strtoupper(pathinfo($filepath, PATHINFO_EXTENSION));
  282. // We don't want to display unknown extensions since they might not be images
  283. // and we
  284. if(!in_array($extension, $this->_allowedExtensions)){
  285. $image = $this->_thumbnail['unknown'];
  286. $imageSource = $this->_thumbnail['unknown'];
  287. } else {
  288. // Get image source file for known images.
  289. $imageSource = $this->mediaURI . '/' . ltrim(rtrim($cwd,'/'), '/') . '/' . $file;
  290. }
  291. $event = 'ai';//'myblog.attachImage(\'' . $imageSource . '\');';
  292. $eventData = $imageSource;
  293. }
  294. $items = array(
  295. 'i' => $image,
  296. 'e' => $event,
  297. 'd' => $eventData,
  298. 't' => $file,
  299. 'c' => $file
  300. );
  301. return $items;
  302. }
  303. }