Source for file api.gallery.php

Documentation is available at api.gallery.php

  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // Copyright (C) ReloadCMS Development Team //
  4. // http://reloadcms.sf.net //
  5. // //
  6. // This program is distributed in the hope that it will be useful, //
  7. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  9. // //
  10. // This product released under GNU General Public License v2 //
  11. ////////////////////////////////////////////////////////////////////////////////
  12.  
  13. $gd_formats = '';
  14. if(function_exists('imagegif')) $gd_formats .= 'gif ';
  15. if(function_exists('imagejpeg')) $gd_formats .= 'jpg jpeg jpe ';
  16. if(function_exists('imagepng')) $gd_formats .= 'png ';
  17. define('GD_SUPPORTED_FORMATS', $gd_formats);
  18. define('GD_SUPPORTED_FORMATS_PREG', '#.*\.(' . implode('|', explode(' ', $gd_formats)) . ')$#i');
  19. define('GALLERY_UPLOAD_DIR', GALLERY_PATH . 'new/');
  20. define('GALLERY_INDEXES_DIR', GALLERY_PATH . 'indexes/');
  21. define('GALLERY_IMAGES_DIR', GALLERY_PATH . 'images/');
  22. define('GALLERY_THUMBS_DIR', GALLERY_PATH . 'thumbnails/');
  23. define('GALLERY_COMMENTS_DIR', GALLERY_PATH . 'comments/');
  24.  
  25. class gallery{
  26. var $img_preg = '/.*\.(jpg|jpe|jpeg|gif|png|bmp)$/i';
  27. var $gd_preg = GD_SUPPORTED_FORMATS_PREG;
  28. var $path = GALLERY_PATH;
  29. var $indexes = array();
  30.  
  31. function gallery(){
  32. // Load Index files
  33. $this->loadIndexFiles();
  34. }
  35.  
  36. function rebuildIndex(){
  37. @set_time_limit(0);
  38. $this->scanForRemovedImages();
  39. $this->cleanUpDirectories();
  40. $this->scanForNewImages();
  41. $this->cleanUpIndexes();
  42. $this->saveIndexFiles();
  43. }
  44. function scanForRemovedImages(){
  45. foreach ($this->indexes['filename'] as $filename){
  46. if(!is_file(GALLERY_IMAGES_DIR . $filename)){
  47. $this->removeImage($filename);
  48. }
  49. }
  50. }
  51. function cleanUpDirectories(){
  52. $images = rcms_scandir(GALLERY_IMAGES_DIR);
  53. foreach ($images as $image){
  54. if(!in_array($image, $this->indexes['filename'])){
  55. rcms_delete_files(GALLERY_IMAGES_DIR . $image);
  56. }
  57. }
  58. $images = rcms_scandir(GALLERY_COMMENTS_DIR);
  59. foreach ($images as $image){
  60. if(!in_array(substr($image, 0, -4), $this->indexes['filename'])){
  61. rcms_delete_files(GALLERY_COMMENTS_DIR . $image);
  62. }
  63. }
  64. $images = rcms_scandir(GALLERY_THUMBS_DIR);
  65. foreach ($images as $image){
  66. if(!in_array(substr($image, 0, -4), $this->indexes['filename'])){
  67. rcms_delete_files(GALLERY_THUMBS_DIR . $image);
  68. }
  69. }
  70. }
  71. function scanForNewImages(){
  72. $return = array();
  73. $new_images = $this->getImages(GALLERY_UPLOAD_DIR);
  74. foreach ($new_images as $image){
  75. $image_newname = $image;
  76. $temp_i = 0;
  77. $ext = array_reverse(explode('.', $image));
  78. $ext = $ext[0];
  79. while(in_array($image_newname, $this->indexes['filename']) || is_file(GALLERY_IMAGES_DIR . $image_newname)){
  80. $temp_i++;
  81. $image_newname = substr($image, 0, -(strlen($ext))-1) . '_' . $temp_i . '.' . $ext;
  82. }
  83. if(substr($ext, 0, 2) == 'jp') $type = 'jpeg'; else $type = $ext;
  84. list($width, $height, $x, $x) = getimagesize(GALLERY_UPLOAD_DIR . $image);
  85. $size = $width . 'x' . $height;
  86. rcms_rename_file(GALLERY_UPLOAD_DIR . $image, GALLERY_IMAGES_DIR . $image_newname);
  87. $this->registerInIndex($image_newname, $image_newname, $size, $type);
  88. $return[$image] = $image_newname;
  89. }
  90. return $return;
  91. }
  92.  
  93. function cleanUpIndexes(){
  94. foreach ($this->indexes['type'] as $type => $images){
  95. if(empty($images)){
  96. rcms_remove_index($type, $this->indexes['type'][$type], true);
  97. }
  98. }
  99.  
  100. foreach ($this->indexes['size'] as $size => $images){
  101. if(empty($images)){
  102. rcms_remove_index($size, $this->indexes['size'][$size], true);
  103. }
  104. }
  105.  
  106. foreach ($this->indexes['keywords'] as $keyword => $images){
  107. if(empty($images)){
  108. rcms_remove_index($size, $this->indexes['keywords'][$keyword], true);
  109. }
  110. }
  111. }
  112. function loadIndexFiles(){
  113. if(!is_readable(GALLERY_INDEXES_DIR . 'main.dat') || !($this->indexes['main'] = @unserialize(file_get_contents(GALLERY_INDEXES_DIR . 'main.dat')))){
  114. $this->indexes['main'] = array();
  115. $this->indexes['filename'] = array();
  116. $this->indexes['title'] = array();
  117. $this->indexes['size'] = array();
  118. $this->indexes['type'] = array();
  119. $this->indexes['keywords'] = array();
  120. return true;
  121. }
  122. $this->indexes['filename'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'filename.dat'));
  123. $this->indexes['title'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'title.dat'));
  124. $this->indexes['size'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'size.dat'));
  125. $this->indexes['type'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'type.dat'));
  126. $this->indexes['keywords'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'keywords.dat'));
  127. return true;
  128. }
  129.  
  130. function registerInIndex($filename, $title, $size, $type){
  131. $this->indexes['main'][$filename] = array('title' => $title, 'size' => $size, 'type' => $type);
  132. $this->indexes['filename'][] = $filename;
  133. $this->indexes['title'][$filename] = $title;
  134. $this->indexes['size'][$size][] = $filename;
  135. $this->indexes['type'][strtolower($type)][] = $filename;
  136. }
  137.  
  138. function unregisterInIndex($filename){
  139. if(empty($this->indexes['main'][$filename])) return false;
  140. $k_f = array_search($filename, $this->indexes['filename']);
  141. $size = $this->indexes['main'][$filename]['size'];
  142. $type = strtolower($this->indexes['main'][$filename]['type']);
  143. $k_s = @array_search($filename, $this->indexes['size'][$size]);
  144. $k_t = @array_search($filename, $this->indexes['type'][$type]);
  145.  
  146. $this->indexes['filename'][$k_f] = '';
  147. $this->indexes['size'][$size][$k_s] = '';
  148. $this->indexes['type'][$type][$k_t] = '';
  149.  
  150. $this->unsetKeywords($filename);
  151.  
  152. rcms_remove_index($k_f, $this->indexes['filename'], true);
  153. rcms_remove_index($filename, $this->indexes['title'], true);
  154. rcms_remove_index($k_s, $this->indexes['size'][$size], true);
  155. rcms_remove_index($k_t, $this->indexes['type'][$type], true);
  156.  
  157. $this->indexes['main'][$filename] = array();
  158. rcms_remove_index($filename, $this->indexes['main'], true);
  159.  
  160. return true;
  161. }
  162.  
  163. function setKeywords($filename, $keywords){
  164. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  165. $this->indexes['main'][$filename]['keywords'] = $keywords;
  166. if(!empty($keywords)){
  167. $keywords_array = explode(';', $keywords);
  168. foreach ($keywords_array as $keyword){
  169. $keyword = trim($keyword);
  170. $this->indexes['keywords'][$keyword][] = $filename;
  171. }
  172. }
  173. return true;
  174. }
  175.  
  176. function unsetKeywords($filename){
  177. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  178. $keywords = explode(';', @$this->indexes['main'][$filename]['keywords']);
  179. if(!empty($keywords)){
  180. foreach ($keywords as $keyword){
  181. $keyword = trim($keyword);
  182. $k = @array_search($filename, $this->indexes['keywords'][$keyword]);
  183. @rcms_remove_index($k, $this->indexes['keywords'][$keyword], true);
  184. }
  185. }
  186. return true;
  187. }
  188.  
  189.  
  190. function changeKeywords($filename, $keywords){
  191. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  192. if(!empty($this->indexes['main'][$filename]['keywords'])){
  193. $this->unsetKeywords($filename);
  194. }
  195. $this->setKeywords($filename, $keywords);
  196. return true;
  197. }
  198.  
  199. function removeImage($filename){
  200. $this->unregisterInIndex($filename);
  201. if(is_file(GALLERY_IMAGES_DIR . $filename)) rcms_delete_files(GALLERY_IMAGES_DIR . $filename);
  202. if(is_file(GALLERY_COMMENTS_DIR . $filename . '.dat')) rcms_delete_files(GALLERY_COMMENTS_DIR . $filename . '.dat');
  203. if(is_file(GALLERY_THUMBS_DIR . $filename . '.jpg')) rcms_delete_files(GALLERY_THUMBS_DIR . $filename . '.jpg');
  204. return true;
  205. }
  206.  
  207. function saveIndexFiles(){
  208. file_write_contents(GALLERY_INDEXES_DIR . 'main.dat', serialize($this->indexes['main']));
  209. file_write_contents(GALLERY_INDEXES_DIR . 'filename.dat', serialize($this->indexes['filename']));
  210. file_write_contents(GALLERY_INDEXES_DIR . 'title.dat', serialize($this->indexes['title']));
  211. file_write_contents(GALLERY_INDEXES_DIR . 'size.dat', serialize($this->indexes['size']));
  212. file_write_contents(GALLERY_INDEXES_DIR . 'type.dat', serialize($this->indexes['type']));
  213. file_write_contents(GALLERY_INDEXES_DIR . 'keywords.dat', serialize($this->indexes['keywords']));
  214. return true;
  215. }
  216.  
  217. function getImages($directory){
  218. $directory = rcms_scandir($directory);
  219. $images = array();
  220. foreach ($directory as $file){
  221. if (preg_match($this->img_preg, $file)) {
  222. $images[] = $file;
  223. }
  224. }
  225. return $images;
  226. }
  227.  
  228. function getFullImagesList(){
  229. $temp = $this->indexes['filename'];
  230. $this->indexes['filename'] = array();
  231. foreach ($temp as $key => $data){
  232. if(!empty($data)) $this->indexes['filename'][$key] = $data;
  233. }
  234. $this->saveIndexFiles();
  235. natsort($this->indexes['filename']);
  236. return $this->indexes['filename'];
  237. }
  238.  
  239. function getAvaiableValues($field){
  240. if(empty($this->indexes[$field])) return false;
  241. $result = array();
  242. foreach (array_keys($this->indexes[$field]) as $key){
  243. if(!empty($this->indexes[$field][$key])){
  244. $result[] = $key;
  245. }
  246. }
  247. natsort($result);
  248. return $result;
  249. }
  250.  
  251. function getLimitedImagesList($field, $value){
  252. if(empty($this->indexes[$field][$value])) return false;
  253. $result = array();
  254. foreach ($this->indexes[$field][$value] as $image){
  255. if(in_array($image, $this->indexes['filename'])) $result[] = $image;
  256. }
  257. natsort($result);
  258. return $result;
  259. }
  260.  
  261. function getImage($filename){
  262. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  263. return '<div style="overflow: hidden; width: 100%;"><a href="' . GALLERY_IMAGES_DIR . $filename . '" target="_blank"><img src="' . GALLERY_IMAGES_DIR . $filename . '" alt="' . $this->indexes['main'][$filename]['title'] . '" style="max-width: 95%;" /></a></div>';
  264. }
  265.  
  266. function getData($filename){
  267. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  268. return $this->indexes['main'][$filename];
  269. }
  270.  
  271. function getComments($filename){
  272. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  273. if(true){
  274. $comments = guestbook_get_last_msgs(
  275. null,
  276. true,
  277. false,
  278. GALLERY_COMMENTS_DIR . $filename . '.dat'
  279. );
  280. foreach ($comments as $mid => $message) {
  281. $comments[$mid]['id'] = $mid;
  282. }
  283. return $comments;
  284. }
  285. return false;
  286. }
  287.  
  288.  
  289. function countComments($filename){
  290. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  291. if(true){
  292. return count(guestbook_get_last_msgs(null, false, false, GALLERY_COMMENTS_DIR . $filename . '.dat'));
  293. }
  294. return false;
  295. }
  296.  
  297. function postComment($filename, $text){
  298. global $system;
  299. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  300. if(!empty($text)){
  301. return guestbook_post_msg($system->user['username'],
  302. $system->user['nickname'],
  303. $text,
  304. GALLERY_COMMENTS_DIR . $filename . '.dat'
  305. );
  306. }
  307. }
  308.  
  309. function removeComment($filename, $cid){
  310. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  311. return guestbook_post_remove($cid, GALLERY_COMMENTS_DIR . $filename . '.dat');
  312. }
  313.  
  314. function getThumbnail($filename, $mw = 150, $mh = 150){
  315. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  316. if($return = $this->generateThumbnail($filename, $mw, $mh)){
  317. return $return;
  318. } else {
  319. $path = GALLERY_IMAGES_DIR . $filename;
  320. $stat = getimagesize($path);
  321. $iw = $stat[0];
  322. $ih = $stat[1];
  323. if(($iw > $mh) || ($iw < $mw)) {
  324. $sizefactor = (($ih > $iw) ? ($mh / $ih) : ($mw / $iw));
  325. } else {
  326. $sizefactor =1;
  327. }
  328. $nw = (int) ($iw * $sizefactor);
  329. $nh = (int) ($ih * $sizefactor);
  330. unset($sizefactor);
  331. return '<img src="' . $path . '" width="' . $nw . '" height="' . $nh . '" border="0" alt="" />';
  332. }
  333. }
  334.  
  335. function generateThumbnail($filename, $mw, $mh){
  336. $path = GALLERY_IMAGES_DIR . $filename;
  337. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename) || !preg_match($this->gd_preg, $filename)) return false;
  338. if(!is_file(GALLERY_THUMBS_DIR . $filename . '.jpg')){
  339. if(substr(strtolower($filename), -4) == '.jpg') {
  340. $img = imagecreatefromjpeg($path);
  341. } elseif(substr(strtolower($filename), -4) == '.gif') {
  342. $img = imagecreatefromgif($path);
  343. } elseif(substr(strtolower($filename), -4) == '.png') {
  344. $img = imagecreatefrompng($path);
  345. } else return false;
  346. if(!empty($img)){
  347. $stat = getimagesize($path);
  348. $iw = $stat[0];
  349. $ih = $stat[1];
  350. if(($iw > $mh) || ($iw < $mw)) {
  351. $sizefactor = (($ih > $iw) ? ($mh / $ih) : ($mw / $iw));
  352. } else {
  353. $sizefactor =1;
  354. }
  355. $nw = (int) ($iw * $sizefactor);
  356. $nh = (int) ($ih * $sizefactor);
  357. unset($sizefactor);
  358. $thumb = imagecreatetruecolor($nw, $nh);
  359. imagecopyresampled($thumb, $img, 0, 0, 0, 0, $nw, $nh, $iw, $ih);
  360. imagejpeg($thumb, GALLERY_THUMBS_DIR . $filename . '.jpg');
  361. imagedestroy($thumb);
  362. imagedestroy($img);
  363. }
  364. }
  365. return '<img src="' . GALLERY_THUMBS_DIR . $filename . '.jpg" alt="' . $filename . '" />';
  366. }
  367.  
  368. function setDataValue($filename, $dataname, $value){
  369. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  370. $this->indexes['main'][$filename][$dataname] = $value;
  371. $this->indexes[$dataname][$filename] = $value;
  372. return true;
  373. }
  374. }
  375. ?>

Documentation generated on Fri, 08 Jun 2007 12:21:19 +0300 by phpDocumentor 1.3.0RC3