slideshow.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 class="actions-pagination">';
  68. // Back forward buttons
  69. if ($slide == 0) {
  70. $imgp = 'action_prev_na.png';
  71. $first = '<img src="'.api_get_path(WEB_IMG_PATH).'action_first_na.png">';
  72. } else {
  73. $imgp = 'action_prev.png';
  74. $first = '<a href="slideshow.php?slide_id=0&curdirpath='.$pathurl.'"><img src="'.api_get_path(WEB_IMG_PATH).'action_first.png" title="'.get_lang('FirstSlide').'" alt="'.get_lang('FirstSlide').'"></a>';
  75. }
  76. // First slide
  77. echo $first;
  78. // Previous slide
  79. if ($slide > 0) {
  80. echo '<a href="slideshow.php?slide_id='.$previous_slide.'&amp;curdirpath='.$pathurl.'">';
  81. }
  82. echo '<img src="'.api_get_path(WEB_IMG_PATH).$imgp.'" title="'.get_lang('Previous').'" alt="'.get_lang('Previous').'">';
  83. if ($slide > 0) {
  84. echo '</a>';
  85. }
  86. // Divider
  87. echo ' [ '.$next_slide.'/'.$total_slides.' ] ';
  88. // Next slide
  89. if ($slide < $total_slides - 1) {
  90. echo '<a href="slideshow.php?slide_id='.$next_slide.'&curdirpath='.$pathurl.'">';
  91. }
  92. if ($slide == $total_slides - 1) {
  93. $imgn = 'action_next_na.png';
  94. $last = '<img src="'.api_get_path(WEB_IMG_PATH).'action_last_na.png" title="'.get_lang('LastSlide').'" alt="'.get_lang('LastSlide').'">';
  95. } else {
  96. $imgn = 'action_next.png';
  97. $last = '<a href="slideshow.php?slide_id='.($total_slides-1).'&curdirpath='.$pathurl.'"><img src="'.api_get_path(WEB_IMG_PATH).'action_last.png" title="'.get_lang('LastSlide').'" alt="'.get_lang('LastSlide').'"></a>';
  98. }
  99. echo '<img src="'.api_get_path(WEB_IMG_PATH).$imgn.'" title="'.get_lang('Next').'" alt="'.get_lang('Next').'">';
  100. if ($slide > 0) {
  101. echo '</a>';
  102. }
  103. // Last slide
  104. echo $last;
  105. echo '</div>';
  106. }
  107. }
  108. // Exit the slideshow
  109. 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>';
  110. // Show thumbnails
  111. if ($slide_id != 'all') {
  112. echo '<a href="slideshow.php?slide_id=all&curdirpath='.$pathurl.'">'.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.'">'.Display::return_icon('settings.png',get_lang('SetSlideshowOptions'),'',ICON_SIZE_MEDIUM).'</a>';
  118. ?>
  119. </div>
  120. <?php
  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["auto_image_resizing"]=Security::remove_XSS($_POST['auto_radio_resizing']);
  127. $_SESSION["image_resizing"] = Security::remove_XSS($_POST['radio_resizing']);
  128. if ($_POST['radio_resizing'] == "resizing" && $_POST['width'] != '' && $_POST['height'] != '') {
  129. //echo "resizing";
  130. $_SESSION["image_resizing_width"] = Security::remove_XSS($_POST['width']);
  131. $_SESSION["image_resizing_height"] = Security::remove_XSS($_POST['height']);
  132. } else {
  133. //echo "unsetting the session heighte and width";
  134. $_SESSION["image_resizing_width"] = null;
  135. $_SESSION["image_resizing_height"] = null;
  136. }
  137. }
  138. $target_width = $target_height = null;
  139. // The target height and width depends if we choose resizing or no resizing
  140. if (isset($_SESSION["image_resizing"]) && $_SESSION["image_resizing"] == "resizing") {
  141. $target_width = $_SESSION["image_resizing_width"];
  142. $target_height = $_SESSION["image_resizing_height"];
  143. }
  144. /* THUMBNAIL VIEW */
  145. // This is for viewing all the images in the slideshow as thumbnails.
  146. $image_tag = array ();
  147. if ($slide_id == 'all') {
  148. // Config for make thumbnails
  149. $allowed_thumbnail_types = array('jpg','jpeg','gif','png');
  150. $max_thumbnail_width = 100;
  151. $max_thumbnail_height = 100;
  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. /*
  165. //
  166. //disabled by now, because automatic mode is heavy for server (scandir), only manual
  167. //
  168. // Delete orphaned thumbnails
  169. $directory_images=$sys_course_path.$_course['path'].'/document'.$folder;
  170. $all_thumbnails = scandir($directory_thumbnails);
  171. $all_files = scandir($directory_images);
  172. foreach ($all_thumbnails as $check_thumb) {
  173. $temp_filename=substr($check_thumb,1);//erase the first dot in file, and translate .. to .
  174. if ($temp_filename=='.') {
  175. continue; //need because scandir also return . and .. simbols
  176. }
  177. if(in_array($filename, $all_files)==false) {
  178. unlink($directory_thumbnails.'.'.$temp_filename);
  179. }
  180. }
  181. */
  182. // check files and thumbnails
  183. if (is_array($image_files_only)) {
  184. foreach ($image_files_only as $one_image_file) {
  185. $image = $sys_course_path.$_course['path'].'/document'.$folder.$one_image_file;
  186. $image_thumbnail= $directory_thumbnails.'.'.$one_image_file;
  187. if (file_exists($image)) {
  188. //check thumbnail
  189. $imagetype = explode(".", $image);
  190. $imagetype = strtolower($imagetype[count($imagetype)-1]);//or check $imagetype = image_type_to_extension(exif_imagetype($image), false);
  191. if(in_array($imagetype,$allowed_thumbnail_types)) {
  192. if (!file_exists($image_thumbnail)){
  193. $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
  194. switch($imagetype) {
  195. case 'gif':
  196. $source_img = imagecreatefromgif($image);
  197. break;
  198. case 'jpg':
  199. $source_img = imagecreatefromjpeg($image);
  200. break;
  201. case 'jpeg':
  202. $source_img = imagecreatefromjpeg($image);
  203. break;
  204. case 'png':
  205. $source_img = imagecreatefrompng($image);
  206. break;
  207. }
  208. $new_thumbnail_size = api_calculate_image_size($original_image_size['width'], $original_image_size['height'], $max_thumbnail_width, $max_thumbnail_height);
  209. if($max_thumbnail_width>$original_image_size['width'] && $max_thumbnail_height>$original_image_size['height']){
  210. $new_thumbnail_size['width']=$original_image_size['width'];
  211. $new_thumbnail_size['height']=$original_image_size['height'];
  212. }
  213. $crop = imagecreatetruecolor($new_thumbnail_size['width'], $new_thumbnail_size['height']);
  214. // preserve transparency
  215. if($imagetype == "png"){
  216. imagesavealpha($crop, true);
  217. $color = imagecolorallocatealpha($crop,0x00,0x00,0x00,127);
  218. imagefill($crop, 0, 0, $color);
  219. }
  220. if ($imagetype == "gif") {
  221. $transindex = imagecolortransparent($source_img);
  222. $palletsize = imagecolorstotal($source_img);
  223. //GIF89a for transparent and anim (first clip), either GIF87a
  224. if ($transindex >= 0 && $transindex < $palletsize){
  225. $transcol = imagecolorsforindex($source_img, $transindex);
  226. $transindex = imagecolorallocatealpha($crop, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
  227. imagefill($crop, 0, 0, $transindex);
  228. imagecolortransparent($crop, $transindex);
  229. }
  230. }
  231. //resampled image
  232. imagecopyresampled($crop,$source_img,0,0,0,0,$new_thumbnail_size['width'],$new_thumbnail_size['height'],$original_image_size['width'],$original_image_size['height']);
  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 src="download.php?doc_url='.$doc_url.'" border="0" title="'.$one_image_file.'">';
  254. }
  255. else{
  256. //if images aren't support by gd (not gif, jpg, jpeg, png)
  257. if ($imagetype=="bmp"){
  258. $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
  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. }
  264. else{
  265. $image_height=$original_image_size[0];
  266. $image_width=$original_image_size[1];
  267. }
  268. }
  269. else{
  270. //example for svg files,...
  271. $image_width=$max_thumbnail_width;
  272. $image_height=$max_thumbnail_height;
  273. }
  274. $doc_url = ($path && $path !== '/') ? $path.'/'.$one_image_file : $path.$one_image_file;
  275. $image_tag[] = '<img src="download.php?doc_url='.$doc_url.'" border="0" width="'.$image_width.'" height="'.$image_height.'" title="'.$one_image_file.'">';
  276. }//end allowed image types
  277. }//end if exist file image
  278. }//end foreach
  279. }//end image files only
  280. // Creating the table
  281. $html_table = '';
  282. echo '<table align="center" width="760px" border="0" cellspacing="10">';
  283. $i = 0;
  284. $count_image = count($image_tag);
  285. $number_iteration = ceil($count_image/$number_image);
  286. $p = 0;
  287. for ($k = 0; $k < $number_iteration; $k++) {
  288. echo '<tr height="'.$thumbnail_height.'">';
  289. for ($i = 0; $i < $number_image; $i++) {
  290. if (!is_null($image_tag[$p])) {
  291. echo '<td>';
  292. //TODO:move styles to css files and center image vertical
  293. ?>
  294. <style>
  295. div.thumbnail:hover {
  296. border-color: #0088cc;
  297. background-color:#FBFFFF;
  298. -webkit-box-shadow: 0 1px 4px rgba(0, 105, 214, 0.25);
  299. -moz-box-shadow: 0 1px 4px rgba(0, 105, 214, 0.25);
  300. box-shadow: 0 1px 4px rgba(0, 105, 214, 0.25);
  301. }
  302. </style>
  303. <?php
  304. echo '<div class="thumbnail" style="text-align: center; margin:5px; padding:9px; height:'.$thumbnail_height_frame.'px; width:'.$thumbnail_width_frame.'px">';
  305. echo '<a href="slideshow.php?slide_id='.$p.'&curdirpath='.$pathurl.'">'.$image_tag[$p].'</a>';
  306. echo '</div>';
  307. echo '</td>';
  308. }
  309. $p++;
  310. }
  311. echo '</tr>';
  312. }
  313. echo '</table>';
  314. }//end slide==all
  315. /* ONE AT A TIME VIEW */
  316. $course_id = api_get_course_int_id();
  317. // This is for viewing all the images in the slideshow one at a time.
  318. if ($slide_id != 'all') {
  319. if (file_exists($image)) {
  320. $image_height_width = resize_image($image, $target_width, $target_height);
  321. $image_height = $image_height_width[0];
  322. $image_width = $image_height_width[1];
  323. $height_width_tags = null;
  324. if (isset($_SESSION['image_resizing']) && $_SESSION['image_resizing'] == 'resizing') {
  325. $height_width_tags = 'width="'.$image_width.'" height="'.$image_height.'"';
  326. }
  327. // This is done really quickly and should be cleaned up a little bit using the API functions
  328. $tbl_documents = Database::get_course_table(TABLE_DOCUMENT);
  329. if ($path == '/') {
  330. $pathpart = '/';
  331. } else {
  332. $pathpart = $path.'/';
  333. }
  334. $sql = "SELECT * FROM $tbl_documents WHERE c_id = $course_id AND path='".Database::escape_string($pathpart.$image_files_only[$slide])."'";
  335. $result = Database::query($sql);
  336. $row = Database::fetch_array($result);
  337. echo '<table align="center" border="0" cellspacing="10">';
  338. echo '<tr>';
  339. echo '<td style="padding:10px;" align="center">';
  340. echo Display::tag('h2',$row['title']);
  341. echo '</td>';
  342. echo '</tr>';
  343. echo '<tr>';
  344. echo '<td id="td_image" align="center">';
  345. if ($slide < $total_slides - 1 && $slide_id != 'all') {
  346. echo "<a href='slideshow.php?slide_id=".$next_slide."&curdirpath=$pathurl'>";
  347. } else {
  348. echo "<a href='slideshow.php?slide_id=0&curdirpath=$pathurl'>";
  349. }
  350. if ($path == '/') {
  351. $path = '';
  352. }
  353. list($width, $height) = getimagesize($image);
  354. //auto resize
  355. if($_SESSION["image_resizing"]!="noresizing" && $_SESSION["image_resizing"]!="resizing" ){
  356. ?>
  357. <script type="text/javascript">
  358. var initial_width='<?php echo $width; ?>';
  359. var initial_height='<?php echo $height; ?>';
  360. var height = window.innerHeight -320;
  361. var width = window.innerWidth -360;
  362. if (initial_height>height || initial_width>width) {
  363. start_width=width;
  364. start_height=height;
  365. }
  366. else{
  367. start_width=initial_width;
  368. start_height=initial_height;
  369. }
  370. 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] ;?>">');
  371. function resizeImage() {
  372. var resize_factor_width = width / initial_width;
  373. var resize_factor_height = height / initial_height;
  374. var delta_width = width - initial_width * resize_factor_height;
  375. var delta_height = height - initial_height * resize_factor_width;
  376. if (delta_width > delta_height) {
  377. width = Math.ceil(initial_width * resize_factor_height);
  378. height= Math.ceil(initial_height * resize_factor_height);
  379. }
  380. else if(delta_width < delta_height) {
  381. width = Math.ceil(initial_width * resize_factor_width);
  382. height = Math.ceil(initial_height * resize_factor_width);
  383. }
  384. else {
  385. width = Math.ceil(width);
  386. height = Math.ceil(height);
  387. }
  388. document.getElementById('image').style.height = height +"px";
  389. document.getElementById('image').style.width = width +"px";
  390. document.getElementById('td_image').style.background='none';
  391. document.getElementById('image').style.visibility='visible';
  392. };
  393. if (initial_height>height || initial_width>width) {
  394. document.getElementById('image').style.visibility='hidden';
  395. document.getElementById('image').onload = resizeImage;
  396. window.onresize = resizeImage;
  397. }
  398. </script>
  399. <?php
  400. }
  401. else{
  402. echo "<img src='download.php?doc_url=$path/".$image_files_only[$slide]."' alt='".$image_files_only[$slide]."' border='0'".$height_width_tags.">";
  403. }
  404. echo '</a>';
  405. echo '</td>';
  406. echo '</tr>';
  407. echo '<tr>';
  408. echo '<td>';
  409. echo $row['comment'];
  410. echo '</td>';
  411. echo '</tr>';
  412. echo '</table>';
  413. echo '<table align="center" border="0">';
  414. if (api_is_allowed_to_edit(null, true)) {
  415. echo '<tr>';
  416. echo '<td align="center">';
  417. echo '<a href="edit_document.php?'.api_get_cidreq().'&id='.$row['id'].'&origin=slideshow&amp;origin_opt='.$edit_slide_id.'&amp;">
  418. '.Display::return_icon('edit.gif', get_lang('Modify')).'</a><br />';
  419. $aux = explode('.', htmlspecialchars($image_files_only[$slide]));
  420. $ext = $aux[count($aux) - 1];
  421. echo $image_files_only[$slide].' <br />';
  422. echo $width.' x '.$height.' <br />';
  423. echo round((filesize($image)/1024), 2).' KB';
  424. echo ' - '.$ext;
  425. echo '</td>';
  426. echo '</tr>';
  427. echo '<tr>';
  428. echo '<td align="center">';
  429. if ($_SESSION['image_resizing'] == 'resizing') {
  430. $resize_info = get_lang('Resizing').'<br />';
  431. $resize_widht = $_SESSION["image_resizing_width"].' x ';
  432. $resize_height = $_SESSION['image_resizing_height'];
  433. }
  434. elseif($_SESSION['image_resizing'] != 'noresizing'){
  435. $resize_info = get_lang('Resizing').'<br />';
  436. $resize_widht = get_lang('Auto').' x ';
  437. $resize_height = get_lang('Auto');
  438. } else {
  439. $resize_info = get_lang('NoResizing').'<br />';
  440. }
  441. echo $resize_info;
  442. echo $resize_widht;
  443. echo $resize_height;
  444. echo '</td>';
  445. echo '</tr>';
  446. }
  447. echo '</table>';
  448. } else {
  449. Display::display_warning_message(get_lang('FileNotFound'));
  450. }
  451. }
  452. Display :: display_footer();