slideshow.php 18 KB

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