slideshow.php 19 KB

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