slideshow.php 22 KB

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