slideshow.php 21 KB

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