slideshow.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 '../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 type="text/javascript">
  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, then need move into !file_exists($image_thumbnail, and only run when haven't the thumbnail
  177. $original_image_size = api_getimagesize($image);
  178. switch($imagetype) {
  179. case 'gif':
  180. $source_img = imagecreatefromgif($image);
  181. break;
  182. case 'jpg':
  183. $source_img = imagecreatefromjpeg($image);
  184. break;
  185. case 'jpeg':
  186. $source_img = imagecreatefromjpeg($image);
  187. break;
  188. case 'png':
  189. $source_img = imagecreatefrompng($image);
  190. break;
  191. }
  192. $new_thumbnail_size = api_calculate_image_size(
  193. $original_image_size['width'],
  194. $original_image_size['height'],
  195. $max_thumbnail_width,
  196. $max_thumbnail_height
  197. );
  198. if($max_thumbnail_width>$original_image_size['width'] && $max_thumbnail_height>$original_image_size['height']){
  199. $new_thumbnail_size['width']=$original_image_size['width'];
  200. $new_thumbnail_size['height']=$original_image_size['height'];
  201. }
  202. $crop = imagecreatetruecolor($new_thumbnail_size['width'], $new_thumbnail_size['height']);
  203. // preserve transparency
  204. if($imagetype == "png"){
  205. imagesavealpha($crop, true);
  206. $color = imagecolorallocatealpha($crop,0x00,0x00,0x00,127);
  207. imagefill($crop, 0, 0, $color);
  208. }
  209. if ($imagetype == "gif") {
  210. $transindex = imagecolortransparent($source_img);
  211. $palletsize = imagecolorstotal($source_img);
  212. //GIF89a for transparent and anim (first clip), either GIF87a
  213. if ($transindex >= 0 && $transindex < $palletsize){
  214. $transcol = imagecolorsforindex($source_img, $transindex);
  215. $transindex = imagecolorallocatealpha($crop, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
  216. imagefill($crop, 0, 0, $transindex);
  217. imagecolortransparent($crop, $transindex);
  218. }
  219. }
  220. //resampled image
  221. imagecopyresampled(
  222. $crop,
  223. $source_img,
  224. 0,
  225. 0,
  226. 0,
  227. 0,
  228. $new_thumbnail_size['width'],
  229. $new_thumbnail_size['height'],
  230. $original_image_size['width'],
  231. $original_image_size['height']
  232. );
  233. switch ($imagetype) {
  234. case 'gif':
  235. imagegif($crop,$image_thumbnail);
  236. break;
  237. case 'jpg':
  238. imagejpeg($crop,$image_thumbnail,$jpg_quality);
  239. break;
  240. case 'jpeg':
  241. imagejpeg($crop,$image_thumbnail,$jpg_quality);
  242. break;
  243. case 'png':
  244. imagepng($crop,$image_thumbnail,$png_compression);
  245. break;
  246. }
  247. //clean memory
  248. imagedestroy($crop);
  249. }//end !exist thumbnail
  250. //show thumbnail and link
  251. $one_image_thumbnail_file='.thumbs/.'.$one_image_file;//get path thumbnail
  252. $doc_url = ($path && $path !== '/') ? $path.'/'.$one_image_thumbnail_file : $path.$one_image_thumbnail_file;
  253. $image_tag[] = '<img class="img-gallery" src="download.php?doc_url='.$doc_url.'" border="0" title="'.$one_image_file.'">';
  254. } else {
  255. //if images aren't support by gd (not gif, jpg, jpeg, png)
  256. if ($imagetype=="bmp") {
  257. // 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
  258. $original_image_size = getimagesize($image);
  259. if ($max_thumbnail_width<$original_image_size[0] || $max_thumbnail_height<$original_image_size[1]){
  260. $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
  261. $image_height = $thumbnail_size['height'];
  262. $image_width = $thumbnail_size['width'];
  263. } else {
  264. $image_height=$original_image_size[0];
  265. $image_width=$original_image_size[1];
  266. }
  267. } else {
  268. //example for svg files,...
  269. $image_width=$max_thumbnail_width;
  270. $image_height=$max_thumbnail_height;
  271. }
  272. $doc_url = ($path && $path !== '/') ? $path.'/'.$one_image_file : $path.$one_image_file;
  273. $image_tag[] = '<img src="download.php?doc_url='.$doc_url.'" border="0" width="'.$image_width.'" height="'.$image_height.'" title="'.$one_image_file.'">';
  274. }//end allowed image types
  275. }//end if exist file image
  276. }//end foreach
  277. }//end image files only
  278. // Creating the table
  279. $html_table = '';
  280. $i = 0;
  281. $count_image = count($image_tag);
  282. $number_iteration = ceil($count_image/$number_image);
  283. $p = 0;
  284. $html = '';
  285. $html .= '<div class="gallery">';
  286. for ($k = 0; $k < $number_iteration; $k++) {
  287. for ($i = 0; $i < $number_image; $i++) {
  288. if (isset($image_tag[$p])) {
  289. $html .= '<div class="col-xs-6 col-sm-3 col-md-2">';
  290. $html .= '<div class="canvas-one">';
  291. $html .= '<a class="canvas-two" href="slideshow.php?slide_id='.$p.'&curdirpath='.$pathurl.'">';
  292. $html .= '<div class="frame">';
  293. $html .= '<div class="photo">';
  294. $html .= $image_tag[$p];
  295. $html .= '</div>';
  296. $html .= '</div>';
  297. $html .= '</a>';
  298. $html .= '</div>';
  299. $html .= '</div>';
  300. }
  301. $p++;
  302. }
  303. }
  304. $html .= '</div>';
  305. }//end slide==all
  306. echo $html;
  307. /* ONE AT A TIME VIEW */
  308. $course_id = api_get_course_int_id();
  309. // This is for viewing all the images in the slideshow one at a time.
  310. if ($slide_id != 'all' && !empty($image_files_only)) {
  311. if (file_exists($image) && is_file($image)) {
  312. $image_height_width = resize_image($image, $target_width, $target_height);
  313. $image_height = $image_height_width[0];
  314. $image_width = $image_height_width[1];
  315. $height_width_tags = null;
  316. if (isset($_SESSION['image_resizing']) && $_SESSION['image_resizing'] == 'resizing') {
  317. $height_width_tags = 'width="'.$image_width.'" height="'.$image_height.'"';
  318. }
  319. // This is done really quickly and should be cleaned up a little bit using the API functions
  320. $tbl_documents = Database::get_course_table(TABLE_DOCUMENT);
  321. if ($path == '/') {
  322. $pathpart = '/';
  323. } else {
  324. $pathpart = $path.'/';
  325. }
  326. $sql = "SELECT * FROM $tbl_documents
  327. WHERE c_id = $course_id AND path='".Database::escape_string($pathpart.$image_files_only[$slide])."'";
  328. $result = Database::query($sql);
  329. $row = Database::fetch_array($result);
  330. echo '<div class="thumbnail">';
  331. if ($slide < $total_slides - 1 && $slide_id != 'all') {
  332. echo "<a href='slideshow.php?slide_id=".$next_slide."&curdirpath=$pathurl'>";
  333. } else {
  334. echo "<a href='slideshow.php?slide_id=0&curdirpath=$pathurl'>";
  335. }
  336. if ($path == '/') {
  337. $path = '';
  338. }
  339. list($width, $height) = getimagesize($image);
  340. // Auto resize
  341. if (isset($_SESSION["image_resizing"]) && $_SESSION["image_resizing"] == 'resizing') {
  342. ?>
  343. <script type="text/javascript">
  344. var initial_width='<?php echo $width; ?>';
  345. var initial_height='<?php echo $height; ?>';
  346. var height = window.innerHeight -320;
  347. var width = window.innerWidth -360;
  348. if (initial_height>height || initial_width>width) {
  349. start_width=width;
  350. start_height=height;
  351. } else {
  352. start_width=initial_width;
  353. start_height=initial_height;
  354. }
  355. 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] ;?>">');
  356. function resizeImage() {
  357. var resize_factor_width = width / initial_width;
  358. var resize_factor_height = height / initial_height;
  359. var delta_width = width - initial_width * resize_factor_height;
  360. var delta_height = height - initial_height * resize_factor_width;
  361. if (delta_width > delta_height) {
  362. width = Math.ceil(initial_width * resize_factor_height);
  363. height= Math.ceil(initial_height * resize_factor_height);
  364. }
  365. else if(delta_width < delta_height) {
  366. width = Math.ceil(initial_width * resize_factor_width);
  367. height = Math.ceil(initial_height * resize_factor_width);
  368. }
  369. else {
  370. width = Math.ceil(width);
  371. height = Math.ceil(height);
  372. }
  373. document.getElementById('image').style.height = height +"px";
  374. document.getElementById('image').style.width = width +"px";
  375. document.getElementById('td_image').style.background='none';
  376. document.getElementById('image').style.visibility='visible';
  377. };
  378. if (initial_height>height || initial_width>width) {
  379. document.getElementById('image').style.visibility='hidden';
  380. document.getElementById('td_image').style.background='url(<?php echo Display::returnIconPath('loadingAnimation.gif'); ?>) center no-repeat';
  381. document.getElementById('image').onload = resizeImage;
  382. window.onresize = resizeImage;
  383. }
  384. </script>
  385. <?php
  386. } else {
  387. 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.">";
  388. }
  389. echo '</a>';
  390. echo '<div class="caption text-center">';
  391. echo Display::tag('h3', $row['title']);
  392. echo '<p>' . $row['comment'] . '</p>';
  393. echo '</div>';
  394. echo '</div>';
  395. if (api_is_allowed_to_edit(null, true)) {
  396. echo '<ul class="list-unstyled">';
  397. $aux = explode('.', htmlspecialchars($image_files_only[$slide]));
  398. $ext = $aux[count($aux) - 1];
  399. if (isset($_SESSION['image_resizing']) && $_SESSION['image_resizing'] == 'resizing') {
  400. $resize_info = get_lang('Resizing').'<br />';
  401. $resize_width = $_SESSION["image_resizing_width"].' x ';
  402. $resize_height = $_SESSION['image_resizing_height'];
  403. } elseif (isset($_SESSION['image_resizing']) && $_SESSION['image_resizing'] != 'noresizing'){
  404. $resize_info = get_lang('Resizing').'<br />';
  405. $resize_width = get_lang('Auto').' x ';
  406. $resize_height = get_lang('Auto');
  407. } else {
  408. $resize_info = get_lang('NoResizing').'<br />';
  409. $resize_width = '';
  410. $resize_height = '';
  411. }
  412. echo '<li class="text-center">';
  413. echo $image_files_only[$slide] . ' ';
  414. echo Display::toolbarButton(
  415. get_lang('Modify'),
  416. 'edit_document.php?' . api_get_cidreq() . '&' . http_build_query([
  417. 'id' => $row['id'],
  418. 'origin' => 'slideshow',
  419. 'origin_opt' => $edit_slide_id,
  420. 'curdirpath' => $pathurl
  421. ]),
  422. 'edit',
  423. 'link',
  424. [],
  425. false
  426. );
  427. echo '</li>';
  428. echo '<li class="text-center">' . $width.' x '.$height . '</li>';
  429. echo '<li class="text-center">' . round((filesize($image) / 1024), 2) . ' KB - ' . $ext . '</li>';
  430. echo '<li class="text-center">' . $resize_info . '</li>';
  431. echo '<li class="text-center">' . $resize_width . '</li>';
  432. echo '<li class="text-center">' . $resize_height . '</li>';
  433. echo '</ul>';
  434. }
  435. } else {
  436. Display::display_warning_message(get_lang('FileNotFound'));
  437. }
  438. } else {
  439. if ($slide_id != 'all') {
  440. Display::display_warning_message(get_lang('NoDataAvailable'));
  441. }
  442. }
  443. Display :: display_footer();