PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/ww.plugins/image-gallery/api.php

http://kv-webme.googlecode.com/
PHP | 290 lines | 229 code | 11 blank | 50 comment | 21 complexity | 14a6eaf20cac3e54de6b1427dde00d08 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause, BSD-2-Clause, Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * script for retrieving a JSON array of images/videos in a gallery
  4. *
  5. * PHP Version 5
  6. *
  7. * @category Whatever
  8. * @package None
  9. * @subpackage None
  10. * @author Kae Verens <kae@kvsites.ie>
  11. * @license GPL Version 2
  12. * @link www.kvweb.me
  13. */
  14. $kfm_do_not_save_session=true;
  15. require_once KFM_BASE_PATH.'/api/api.php';
  16. require_once KFM_BASE_PATH.'/initialise.php';
  17. // { ImageGallery_galleryGet
  18. /**
  19. * script for retrieving a JSON array of images/videos in a gallery
  20. *
  21. * @return array gallery details
  22. */
  23. function ImageGallery_galleryGet() {
  24. $page_id=(int)@$_REQUEST['id'];
  25. if ($page_id==0) {
  26. Core_quit();
  27. }
  28. $image_dir=@$_REQUEST['image_gallery_directory'];
  29. if ($image_dir!=''&&is_dir(USERBASE.'/f'.$image_dir)) { // read from KFM
  30. $dir=preg_replace('/^\//', '', $image_dir);
  31. $dir_id=kfm_api_getDirectoryID($dir);
  32. $images=kfm_loadFiles($dir_id);
  33. $n=count($images);
  34. if ($n==0) {
  35. die('none');
  36. }
  37. $f=array();
  38. foreach ($images['files'] as $file) {
  39. array_push(
  40. $f,
  41. array(
  42. 'id'=>$file['id'],
  43. 'name'=>$file['name'],
  44. 'width'=>$file['width'],
  45. 'media'=>'image',
  46. 'height'=>$file['height'],
  47. 'caption'=>$file['caption'],
  48. 'url'=>'/kfmget/'.$file['id']
  49. )
  50. );
  51. }
  52. }
  53. else { // fall back to reading from database
  54. $files=dbAll(
  55. 'select * from image_gallery where gallery_id='
  56. .$page_id.' order by position asc'
  57. );
  58. $dir=dbOne(
  59. 'select value from page_vars where name="image_gallery_directory"'
  60. .'and page_id='.$page_id,
  61. 'value'
  62. );
  63. if ($dir===false) {
  64. $dir='/image-galleries/imagegallery-'.$page_id;
  65. }
  66. $f=array();
  67. foreach ($files as $file) {
  68. $meta=json_decode($file['meta'], true);
  69. switch ($file['media']) {
  70. case 'image': // {
  71. $arr=array(
  72. 'id'=>$file['id'],
  73. 'name'=>$meta['name'],
  74. 'media'=>'image',
  75. 'width'=>$meta['width'],
  76. 'height'=>$meta['height'],
  77. 'url'=>(isset($DBVARS['cdn']) && $DBVARS['cdn']?'//'.$DBVARS['cdn']:'')
  78. .'/a/f=getImg/'.$dir.'/'.$meta['name']
  79. );
  80. if (isset($meta['author'])) {
  81. $arr['author']=$meta['author'];
  82. }
  83. if (isset($meta['caption'])) {
  84. $arr['caption']=$meta['caption'];
  85. }
  86. if (isset($meta['description'])) {
  87. $arr['description']=$meta['description'];
  88. }
  89. if (isset($meta['tags'])) {
  90. $arr['tags']=$meta['tags'];
  91. }
  92. $f[]=$arr;
  93. break; // }
  94. case 'video': // {
  95. $image=($meta['image']=='')?
  96. '/ww.plugins/image-gallery/files/video.png':
  97. $meta['image'];
  98. array_push(
  99. $f,
  100. array(
  101. 'id'=>$file['id'],
  102. 'media'=>'video',
  103. 'url'=>(isset($DBVARS['cdn']) && $DBVARS['cdn']?'//'.$DBVARS['cdn']:'').'/a/f=getImg/'.$image,
  104. 'href'=>$meta['href']
  105. )
  106. );
  107. break; // }
  108. }
  109. }
  110. }
  111. // { get gallery data
  112. $rs=dbAll(
  113. 'select * from page_vars where page_id='.$page_id
  114. .' and name like "image_gallery_%"',
  115. 'name'
  116. );
  117. // }
  118. return array(
  119. 'items'=>$f,
  120. 'caption-in-slider'=>(int)@$rs['image_gallery_captions_in_slider']['value'],
  121. 'image-width'=>(int)@$rs['image_gallery_image_x']['value'],
  122. 'image-height'=>(int)@$rs['image_gallery_image_y']['value'],
  123. 'frame'=>array(
  124. 'type'=>@$rs['image_gallery_frame']['value'],
  125. 'padding'=>@$rs['image_gallery_frame_custom_padding']['value'],
  126. 'border'=>@$rs['image_gallery_frame_custom_border']['value']
  127. )
  128. );
  129. }
  130. // }
  131. // { ImageGallery_img
  132. /**
  133. * retrieve an image from the database
  134. *
  135. * @return null
  136. */
  137. function ImageGallery_img() {
  138. global $DBVARS;
  139. $id =(int)$_REQUEST['id'];
  140. $width =@(int)$_REQUEST['w'];
  141. $height=@(int)$_REQUEST['h'];
  142. $sql='select * from image_gallery where id='.$id;
  143. $r=dbRow($sql);
  144. $meta=json_decode($r['meta']);
  145. $url=(isset($DBVARS['cdn']) && $DBVARS['cdn']?'//'.$DBVARS['cdn']:'')
  146. .'/a/f=getImg/w='.$width.'/h='.$height.'/image-galleries/'
  147. .'imagegallery-'.$r['gallery_id'].'/'.$meta->name;
  148. header('Location: '.$url);
  149. Core_quit();
  150. }
  151. // }
  152. // { ImageGallery_tagsUpdate
  153. /**
  154. * update the tags on an image
  155. *
  156. * @return status
  157. */
  158. function ImageGallery_tagsUpdate() {
  159. $id=(int)$_REQUEST['id'];
  160. $tags=$_REQUEST['tags'];
  161. $meta=dbOne('select meta from image_gallery where id='.$id, 'meta');
  162. if ($meta) {
  163. $meta=json_decode($meta, true);
  164. }
  165. else {
  166. $meta=array();
  167. }
  168. $meta['tags']=$tags;
  169. dbQuery(
  170. 'update image_gallery set meta="'.addslashes(json_encode($meta)).'"'
  171. .' where id='.$id
  172. );
  173. return array('ok'=>1);
  174. }
  175. // }
  176. // { ImageGallery_frameGet
  177. /**
  178. * get a frame for images
  179. *
  180. * @return null
  181. */
  182. function ImageGallery_frameGet() {
  183. if (isset($_REQUEST['ratio'])) {
  184. $ratio=(float)$_REQUEST['ratio'];
  185. }
  186. else {
  187. $ratio=1;
  188. }
  189. $padding=explode(' ', $_REQUEST['pa']);
  190. $border=explode(' ', $_REQUEST['bo']);
  191. $width=$_REQUEST['w']+($padding[1]+$padding[3])/$ratio;
  192. $height=$_REQUEST['h']+($padding[0]+$padding[2])/$ratio;
  193. $file=USERBASE.'/f/'.$_REQUEST['_remainder'];
  194. if (strpos($file, '/.')!==false) {
  195. Core_quit();
  196. }
  197. if (!file_exists($file)) {
  198. header('Location: /i/blank.gif');
  199. Core_quit();
  200. }
  201. $md5=md5($_SERVER['REQUEST_URI']);
  202. $frame=USERBASE.'/ww.cache/image-gallery-frames/frame-'.$md5.'.png';
  203. if (!file_exists($frame)) {
  204. @mkdir(USERBASE.'/ww.cache/image-gallery-frames');
  205. $imgO=imagecreatefrompng($file);
  206. if ($img0===false) { // not a PNG
  207. header('Location: /i/blank.gif');
  208. Core_quit();
  209. }
  210. $imgOsize=getimagesize($file);
  211. $imgN=imagecreatetruecolor($width, $height);
  212. $black = imagecolorallocate($imgN, 0, 0, 0);
  213. imagecolortransparent($imgN, $black);
  214. // top left
  215. imagecopyresampled(
  216. $imgN, $imgO,
  217. 0, 0, 0, 0,
  218. ceil($border[3]/$ratio), ceil($border[0]/$ratio), $border[3], $border[0]
  219. );
  220. // top right
  221. imagecopyresampled(
  222. $imgN, $imgO,
  223. $width-floor($border[1]/$ratio)-1, 0, $imgOsize[0]-$border[1]-1, 0,
  224. ceil($border[1]/$ratio), ceil($border[0]/$ratio), $border[1], $border[0]
  225. );
  226. // bottom left
  227. imagecopyresampled(
  228. $imgN, $imgO,
  229. 0, $height-floor($border[2]/$ratio)-1, 0, $imgOsize[1]-$border[2]-1,
  230. ceil($border[3]/$ratio), ceil($border[2]/$ratio), $border[3], $border[2]
  231. );
  232. // bottom right
  233. imagecopyresampled(
  234. $imgN, $imgO,
  235. $width-floor($border[1]/$ratio)-1, $height-floor($border[2]/$ratio)-1,
  236. $imgOsize[0]-$border[1]-1, $imgOsize[1]-$border[2]-1,
  237. ceil($border[1]/$ratio), ceil($border[2]/$ratio), $border[1], $border[2]
  238. );
  239. // left
  240. imagecopyresampled(
  241. $imgN, $imgO,
  242. 0, floor($border[0]/$ratio), 0, $border[0],
  243. ceil($border[3]/$ratio), $height-floor(($border[2]+$border[0])/$ratio),
  244. $border[3], $imgOsize[1]-$border[2]-$border[0]
  245. );
  246. // right
  247. imagecopyresampled(
  248. $imgN, $imgO,
  249. $width-floor($border[1]/$ratio)-1, floor($border[0]/$ratio),
  250. $imgOsize[0]-$border[1]-1, $border[0],
  251. ceil($border[1]/$ratio), $height-floor(($border[2]+$border[0])/$ratio),
  252. $border[3], $imgOsize[1]-$border[2]-$border[0]
  253. );
  254. // top
  255. imagecopyresampled(
  256. $imgN, $imgO,
  257. floor($border[3]/$ratio), 0, $border[3], 0,
  258. $width-floor(($border[3]+$border[1])/$ratio), ceil($border[0]/$ratio),
  259. $imgOsize[0]-$border[3]-$border[1], $border[0]
  260. );
  261. // bottom
  262. imagecopyresampled(
  263. $imgN, $imgO,
  264. floor($border[3]/$ratio), $height-floor($border[2]/$ratio)-1,
  265. $border[3], $imgOsize[1]-$border[2]-1,
  266. $width-floor(($border[3]+$border[1])/$ratio), ceil($border[2]/$ratio),
  267. $imgOsize[0]-$border[3]-$border[1], $border[2]
  268. );
  269. }
  270. header('Content-type: image/png');
  271. imagepng($imgN, $frame);
  272. header('Cache-Control: max-age=2592000, public');
  273. header('Expires-Active: On');
  274. header('Expires: Fri, 1 Jan 2500 01:01:01 GMT');
  275. header('Pragma:');
  276. header('Content-Length: ' . filesize($frame));
  277. readfile($frame);
  278. }
  279. // }