slideshow.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * @author Patrick Cool patrick.cool@UGent.be Ghent University Mai 2004
  6. * @author Julio Montoya Lots of improvements, cleaning, adding security
  7. * @author Juan Carlos Raña Trabado herodoto@telefonica.net January 2008
  8. * @package chamilo.document
  9. */
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. api_protect_course_script();
  12. $noPHP_SELF = true;
  13. $path = Security::remove_XSS($_GET['curdirpath']);
  14. $pathurl = urlencode($path);
  15. $slide_id = isset($_GET['slide_id']) ? Security::remove_XSS($_GET['slide_id']) : null;
  16. if (empty($slide_id)) {
  17. $edit_slide_id = 1;
  18. } else {
  19. $edit_slide_id = $slide_id;
  20. }
  21. if ($path != '/') {
  22. $folder = $path.'/';
  23. } else {
  24. $folder = '/';
  25. }
  26. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  27. // Breadcrumb navigation
  28. $url = 'document.php?curdirpath='.$pathurl;
  29. $originaltoolname = get_lang('Documents');
  30. $_course = api_get_course_info();
  31. $interbreadcrumb[] = array('url' => Security::remove_XSS($url), 'name' => $originaltoolname);
  32. $originaltoolname = get_lang('SlideShow');
  33. Display :: display_header($originaltoolname, 'Doc');
  34. $imageFilesOnlySession = Session::read('image_files_only');
  35. $image_files_only = [];
  36. // Loading the slides from the session
  37. if (!empty($imageFilesOnlySession)) {
  38. $image_files_only = $imageFilesOnlySession;
  39. }
  40. // Calculating the current slide, next slide, previous slide and the number of slides
  41. $slide = null;
  42. if ($slide_id != 'all') {
  43. $slide = $slide_id ? $slide_id : 0;
  44. $previous_slide = $slide - 1;
  45. $next_slide = $slide + 1;
  46. }
  47. $total_slides = count($image_files_only);
  48. ?>
  49. <script>
  50. function MM_openBrWindow(theURL,winName,features) { //v2.0
  51. window.open(theURL,winName,features);
  52. }
  53. </script>
  54. <div class="actions">
  55. <?php
  56. if ($slide_id != 'all') {
  57. $image = null;
  58. if (isset($image_files_only[$slide])) {
  59. $image = $sys_course_path.$_course['path'].'/document'.$folder.$image_files_only[$slide];
  60. }
  61. if (file_exists($image)) {
  62. echo '<div class="actions-pagination">';
  63. // Back forward buttons
  64. if ($slide == 0) {
  65. $imgp = 'action_prev_na.png';
  66. $first = Display::return_icon('action_first_na.png');
  67. } else {
  68. $imgp = 'action_prev.png';
  69. $first = '<a href="slideshow.php?slide_id=0&curdirpath='.$pathurl.'">
  70. '.Display::return_icon('action_first.png', get_lang('FirstSlide')).'
  71. </a>';
  72. }
  73. // First slide
  74. echo $first;
  75. // Previous slide
  76. if ($slide > 0) {
  77. echo '<a href="slideshow.php?slide_id='.$previous_slide.'&curdirpath='.$pathurl.'">';
  78. }
  79. echo Display::return_icon($imgp, get_lang('Previous'));
  80. if ($slide > 0) {
  81. echo '</a>';
  82. }
  83. // Divider
  84. echo ' [ '.$next_slide.'/'.$total_slides.' ] ';
  85. // Next slide
  86. if ($slide < $total_slides - 1) {
  87. echo '<a href="slideshow.php?slide_id='.$next_slide.'&curdirpath='.$pathurl.'">';
  88. }
  89. if ($slide == $total_slides - 1) {
  90. $imgn = 'action_next_na.png';
  91. $last = Display::return_icon('action_last_na.png', get_lang('LastSlide'));
  92. } else {
  93. $imgn = 'action_next.png';
  94. $last = '<a href="slideshow.php?slide_id='.($total_slides - 1).'&curdirpath='.$pathurl.'">
  95. '.Display::return_icon('action_last.png', get_lang('LastSlide')).'
  96. </a>';
  97. }
  98. echo Display::return_icon($imgn, get_lang('Next'));
  99. if ($slide > 0) {
  100. echo '</a>';
  101. }
  102. // Last slide
  103. echo $last;
  104. echo '</div>';
  105. }
  106. }
  107. // Exit the slideshow
  108. echo '<a href="document.php?action=exit_slideshow&curdirpath='.$pathurl.'&'.api_get_cidreq().'">'.
  109. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).'</a>';
  110. // Show thumbnails
  111. if ($slide_id != 'all') {
  112. echo '<a href="slideshow.php?slide_id=all&curdirpath='.$pathurl.'&'.api_get_cidreq().'">'.
  113. Display::return_icon('thumbnails.png', get_lang('ShowThumbnails'), '', ICON_SIZE_MEDIUM).'</a>';
  114. } else {
  115. echo Display::return_icon('thumbnails_na.png', get_lang('ShowThumbnails'), '', ICON_SIZE_MEDIUM);
  116. }
  117. // Slideshow options
  118. echo '<a href="slideshowoptions.php?curdirpath='.$pathurl.'&'.api_get_cidreq().'">'.
  119. Display::return_icon('settings.png', get_lang('SetSlideshowOptions'), '', ICON_SIZE_MEDIUM).'</a>';
  120. echo '</div>';
  121. echo '<br />';
  122. /* TREATING THE POST DATA FROM SLIDESHOW OPTIONS */
  123. // If we come from slideshowoptions.php we sessionize (new word !!! ;-) the options
  124. if (isset($_POST['Submit'])) {
  125. // We come from slideshowoptions.php
  126. Session::write('image_resizing', Security::remove_XSS($_POST['radio_resizing']));
  127. if ($_POST['radio_resizing'] == "resizing" && $_POST['width'] != '' && $_POST['height'] != '') {
  128. Session::write('image_resizing_width', Security::remove_XSS($_POST['width']));
  129. Session::write('image_resizing_height', Security::remove_XSS($_POST['height']));
  130. } else {
  131. Session::write('image_resizing_width', null);
  132. Session::write('image_resizing_height', null);
  133. }
  134. }
  135. $target_width = $target_height = null;
  136. $imageResize = Session::read('image_resizing');
  137. // The target height and width depends if we choose resizing or no resizing
  138. if ($imageResize == 'resizing') {
  139. $target_width = Session::read('image_resizing_width');
  140. $target_height = Session::read('image_resizing_height');
  141. }
  142. /* THUMBNAIL VIEW */
  143. // This is for viewing all the images in the slideshow as thumbnails.
  144. $image_tag = array();
  145. $html = '';
  146. if ($slide_id == 'all') {
  147. // Config for make thumbnails
  148. $allowed_thumbnail_types = array('jpg', 'jpeg', 'gif', 'png');
  149. $max_thumbnail_width = 250;
  150. $max_thumbnail_height = 250;
  151. $png_compression = 0; //0(none)-9
  152. $jpg_quality = 75; //from 0 to 100 (default is 75). More quality less compression
  153. $directory_thumbnails = $sys_course_path.$_course['path'].'/document'.$folder.'.thumbs/';
  154. //Other parameters only for show tumbnails
  155. $row_items = 4; //only in slideshow.php
  156. $number_image = 7; //num icons cols to show
  157. $thumbnail_width_frame = $max_thumbnail_width; //optional $max_thumbnail_width+x
  158. $thumbnail_height_frame = $max_thumbnail_height;
  159. // Create the template_thumbnails folder (if no exist)
  160. if (!file_exists($directory_thumbnails)) {
  161. @mkdir($directory_thumbnails, api_get_permissions_for_new_directories());
  162. }
  163. // check files and thumbnails
  164. if (is_array($image_files_only)) {
  165. foreach ($image_files_only as $one_image_file) {
  166. $image = $sys_course_path.$_course['path'].'/document'.$folder.$one_image_file;
  167. $image_thumbnail = $directory_thumbnails.'.'.$one_image_file;
  168. if (file_exists($image)) {
  169. //check thumbnail
  170. $imagetype = explode(".", $image);
  171. //or check $imagetype = image_type_to_extension(exif_imagetype($image), false);
  172. $imagetype = strtolower($imagetype[count($imagetype) - 1]);
  173. if (in_array($imagetype, $allowed_thumbnail_types)) {
  174. if (!file_exists($image_thumbnail)) {
  175. //run each once we view thumbnails is too heavy,
  176. // then need move into !file_exists($image_thumbnail,
  177. // and only run when haven't the thumbnail
  178. $original_image_size = api_getimagesize($image);
  179. switch ($imagetype) {
  180. case 'gif':
  181. $source_img = imagecreatefromgif($image);
  182. break;
  183. case 'jpg':
  184. $source_img = imagecreatefromjpeg($image);
  185. break;
  186. case 'jpeg':
  187. $source_img = imagecreatefromjpeg($image);
  188. break;
  189. case 'png':
  190. $source_img = imagecreatefrompng($image);
  191. break;
  192. }
  193. $new_thumbnail_size = api_calculate_image_size(
  194. $original_image_size['width'],
  195. $original_image_size['height'],
  196. $max_thumbnail_width,
  197. $max_thumbnail_height
  198. );
  199. if ($max_thumbnail_width > $original_image_size['width'] &&
  200. $max_thumbnail_height > $original_image_size['height']
  201. ) {
  202. $new_thumbnail_size['width'] = $original_image_size['width'];
  203. $new_thumbnail_size['height'] = $original_image_size['height'];
  204. }
  205. $crop = imagecreatetruecolor($new_thumbnail_size['width'], $new_thumbnail_size['height']);
  206. // preserve transparency
  207. if ($imagetype == "png") {
  208. imagesavealpha($crop, true);
  209. $color = imagecolorallocatealpha($crop, 0x00, 0x00, 0x00, 127);
  210. imagefill($crop, 0, 0, $color);
  211. }
  212. if ($imagetype == "gif") {
  213. $transindex = imagecolortransparent($source_img);
  214. $palletsize = imagecolorstotal($source_img);
  215. //GIF89a for transparent and anim (first clip), either GIF87a
  216. if ($transindex >= 0 && $transindex < $palletsize) {
  217. $transcol = imagecolorsforindex($source_img, $transindex);
  218. $transindex = imagecolorallocatealpha($crop, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
  219. imagefill($crop, 0, 0, $transindex);
  220. imagecolortransparent($crop, $transindex);
  221. }
  222. }
  223. //resampled image
  224. imagecopyresampled(
  225. $crop,
  226. $source_img,
  227. 0,
  228. 0,
  229. 0,
  230. 0,
  231. $new_thumbnail_size['width'],
  232. $new_thumbnail_size['height'],
  233. $original_image_size['width'],
  234. $original_image_size['height']
  235. );
  236. switch ($imagetype) {
  237. case 'gif':
  238. imagegif($crop, $image_thumbnail);
  239. break;
  240. case 'jpg':
  241. imagejpeg($crop, $image_thumbnail, $jpg_quality);
  242. break;
  243. case 'jpeg':
  244. imagejpeg($crop, $image_thumbnail, $jpg_quality);
  245. break;
  246. case 'png':
  247. imagepng($crop, $image_thumbnail, $png_compression);
  248. break;
  249. }
  250. //clean memory
  251. imagedestroy($crop);
  252. }//end !exist thumbnail
  253. //show thumbnail and link
  254. $one_image_thumbnail_file = '.thumbs/.'.$one_image_file; //get path thumbnail
  255. $doc_url = ($path && $path !== '/') ? $path.'/'.$one_image_thumbnail_file : $path.$one_image_thumbnail_file;
  256. $image_tag[] = '<img class="img-gallery" src="download.php?doc_url='.$doc_url.'" border="0" title="'.$one_image_file.'">';
  257. } else {
  258. //if images aren't support by gd (not gif, jpg, jpeg, png)
  259. if ($imagetype == "bmp") {
  260. // use getimagesize instead api_getimagesize($image); becasuse api_getimagesize doesn't support bmp files. Put here for each show, only for a few bmp files isn't heavy
  261. $original_image_size = getimagesize($image);
  262. if ($max_thumbnail_width < $original_image_size[0] || $max_thumbnail_height < $original_image_size[1]) {
  263. $thumbnail_size = api_calculate_image_size($original_image_size[0], $original_image_size[1], $max_thumbnail_width, $max_thumbnail_height); //don't use resize_image because doesn't run with bmp files
  264. $image_height = $thumbnail_size['height'];
  265. $image_width = $thumbnail_size['width'];
  266. } else {
  267. $image_height = $original_image_size[0];
  268. $image_width = $original_image_size[1];
  269. }
  270. } else {
  271. //example for svg files,...
  272. $image_width = $max_thumbnail_width;
  273. $image_height = $max_thumbnail_height;
  274. }
  275. $doc_url = ($path && $path !== '/') ? $path.'/'.$one_image_file : $path.$one_image_file;
  276. $image_tag[] = '<img src="download.php?doc_url='.$doc_url.'" border="0" width="'.$image_width.'" height="'.$image_height.'" title="'.$one_image_file.'">';
  277. }//end allowed image types
  278. }//end if exist file image
  279. }//end foreach
  280. }//end image files only
  281. // Creating the table
  282. $html_table = '';
  283. $i = 0;
  284. $count_image = count($image_tag);
  285. $number_iteration = ceil($count_image / $number_image);
  286. $p = 0;
  287. $html = '';
  288. $html .= '<div class="gallery">';
  289. for ($k = 0; $k < $number_iteration; $k++) {
  290. for ($i = 0; $i < $number_image; $i++) {
  291. if (isset($image_tag[$p])) {
  292. $html .= '<div class="col-xs-6 col-sm-3 col-md-2">';
  293. $html .= '<div class="canvas-one">';
  294. $html .= '<a class="canvas-two" href="slideshow.php?slide_id='.$p.'&curdirpath='.$pathurl.'">';
  295. $html .= '<div class="frame">';
  296. $html .= '<div class="photo">';
  297. $html .= $image_tag[$p];
  298. $html .= '</div>';
  299. $html .= '</div>';
  300. $html .= '</a>';
  301. $html .= '</div>';
  302. $html .= '</div>';
  303. }
  304. $p++;
  305. }
  306. }
  307. $html .= '</div>';
  308. }//end slide==all
  309. echo $html;
  310. /* ONE AT A TIME VIEW */
  311. $course_id = api_get_course_int_id();
  312. // This is for viewing all the images in the slideshow one at a time.
  313. if ($slide_id != 'all' && !empty($image_files_only)) {
  314. if (file_exists($image) && is_file($image)) {
  315. $image_height_width = DocumentManager::resizeImageSlideShow($image, $target_width, $target_height);
  316. $image_height = $image_height_width[0];
  317. $image_width = $image_height_width[1];
  318. $height_width_tags = null;
  319. if ($imageResize == 'resizing') {
  320. $height_width_tags = 'width="'.$image_width.'" height="'.$image_height.'"';
  321. }
  322. // This is done really quickly and should be cleaned up a little bit using the API functions
  323. $tbl_documents = Database::get_course_table(TABLE_DOCUMENT);
  324. if ($path == '/') {
  325. $pathpart = '/';
  326. } else {
  327. $pathpart = $path.'/';
  328. }
  329. $sql = "SELECT * FROM $tbl_documents
  330. WHERE c_id = $course_id AND path='".Database::escape_string($pathpart.$image_files_only[$slide])."'";
  331. $result = Database::query($sql);
  332. $row = Database::fetch_array($result);
  333. echo '<div class="thumbnail">';
  334. if ($slide < $total_slides - 1 && $slide_id != 'all') {
  335. echo "<a href='slideshow.php?slide_id=".$next_slide."&curdirpath=$pathurl'>";
  336. } else {
  337. echo "<a href='slideshow.php?slide_id=0&curdirpath=$pathurl'>";
  338. }
  339. if ($path == '/') {
  340. $path = '';
  341. }
  342. list($width, $height) = getimagesize($image);
  343. // Auto resize
  344. if ($imageResize == 'resizing') {
  345. ?>
  346. <script>
  347. var initial_width='<?php echo $width; ?>';
  348. var initial_height='<?php echo $height; ?>';
  349. var height = window.innerHeight -320;
  350. var width = window.innerWidth -360;
  351. if (initial_height>height || initial_width>width) {
  352. start_width=width;
  353. start_height=height;
  354. } else {
  355. start_width=initial_width;
  356. start_height=initial_height;
  357. }
  358. document.write('<img id="image" src="<?php echo 'download.php?doc_url='.$path.'/'.$image_files_only[$slide]; ?>" width="'+start_width+'" height="'+start_height+'" border="0" alt="<?php echo $image_files_only[$slide]; ?>">');
  359. function resizeImage() {
  360. var resize_factor_width = width / initial_width;
  361. var resize_factor_height = height / initial_height;
  362. var delta_width = width - initial_width * resize_factor_height;
  363. var delta_height = height - initial_height * resize_factor_width;
  364. if (delta_width > delta_height) {
  365. width = Math.ceil(initial_width * resize_factor_height);
  366. height= Math.ceil(initial_height * resize_factor_height);
  367. }
  368. else if(delta_width < delta_height) {
  369. width = Math.ceil(initial_width * resize_factor_width);
  370. height = Math.ceil(initial_height * resize_factor_width);
  371. }
  372. else {
  373. width = Math.ceil(width);
  374. height = Math.ceil(height);
  375. }
  376. document.getElementById('image').style.height = height +"px";
  377. document.getElementById('image').style.width = width +"px";
  378. document.getElementById('td_image').style.background='none';
  379. document.getElementById('image').style.visibility='visible';
  380. };
  381. if (initial_height>height || initial_width>width) {
  382. document.getElementById('image').style.visibility='hidden';
  383. document.getElementById('td_image').style.background='url(<?php echo Display::returnIconPath('loadingAnimation.gif'); ?>) center no-repeat';
  384. document.getElementById('image').onload = resizeImage;
  385. window.onresize = resizeImage;
  386. }
  387. </script>
  388. <?php
  389. } else {
  390. echo "<img class=\"img-responsive\" src='download.php?doc_url=$path/".$image_files_only[$slide]."' alt='".$image_files_only[$slide]."' border='0'".$height_width_tags.">";
  391. }
  392. echo '</a>';
  393. echo '<div class="caption text-center">';
  394. echo Display::tag('h3', $row['title']);
  395. echo '<p>'.$row['comment'].'</p>';
  396. echo '</div>';
  397. echo '</div>';
  398. if (api_is_allowed_to_edit(null, true)) {
  399. echo '<ul class="list-unstyled">';
  400. $aux = explode('.', htmlspecialchars($image_files_only[$slide]));
  401. $ext = $aux[count($aux) - 1];
  402. if ($imageResize == 'resizing') {
  403. $resize_info = get_lang('Resizing').'<br />';
  404. $resize_width = Session::read('image_resizing_width').' x ';
  405. $resize_height = Session::read('image_resizing_height');
  406. } elseif ($imageResize != 'noresizing') {
  407. $resize_info = get_lang('Resizing').'<br />';
  408. $resize_width = get_lang('Auto').' x ';
  409. $resize_height = get_lang('Auto');
  410. } else {
  411. $resize_info = get_lang('NoResizing').'<br />';
  412. $resize_width = '';
  413. $resize_height = '';
  414. }
  415. echo '<li class="text-center">';
  416. echo $image_files_only[$slide].' ';
  417. echo Display::toolbarButton(
  418. get_lang('Modify'),
  419. 'edit_document.php?'.api_get_cidreq().'&'.http_build_query([
  420. 'id' => $row['id'],
  421. 'origin' => 'slideshow',
  422. 'origin_opt' => $edit_slide_id,
  423. 'curdirpath' => $pathurl
  424. ]),
  425. 'edit',
  426. 'link',
  427. [],
  428. false
  429. );
  430. echo '</li>';
  431. echo '<li class="text-center">'.$width.' x '.$height.'</li>';
  432. echo '<li class="text-center">'.round((filesize($image) / 1024), 2).' KB - '.$ext.'</li>';
  433. echo '<li class="text-center">'.$resize_info.'</li>';
  434. echo '<li class="text-center">'.$resize_width.'</li>';
  435. echo '<li class="text-center">'.$resize_height.'</li>';
  436. echo '</ul>';
  437. }
  438. } else {
  439. echo Display::return_message(get_lang('FileNotFound'), 'warning');
  440. }
  441. } else {
  442. if ($slide_id != 'all') {
  443. echo Display::return_message(get_lang('NoDataAvailable'), 'warning');
  444. }
  445. }
  446. Display :: display_footer();