index.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /* Integrate svg-edit libraries with Chamilo default documents
  3. * @author Juan Carlos Raña Trabado
  4. * @since 25/september/2010
  5. */
  6. //Chamilo load libraries
  7. require_once '../../../../../../inc/global.inc.php';
  8. // Add security from Chamilo
  9. api_protect_course_script();
  10. api_block_anonymous_users();
  11. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  12. $curdirpath='/images/gallery'; //path of library directory
  13. $course_info = api_get_course_info();
  14. // Get all files and folders
  15. $docs_and_folders = DocumentManager::getAllDocumentData(
  16. $course_info,
  17. $curdirpath,
  18. 0,
  19. null,
  20. $is_allowed_to_edit,
  21. false
  22. );
  23. //get all filenames
  24. $array_to_search = !empty($docs_and_folders) ? $docs_and_folders : [];
  25. $all_files = [];
  26. if (count($array_to_search) > 0) {
  27. foreach ($array_to_search as $key => $value) {
  28. $all_files[] = basename($array_to_search[$key]['path']);
  29. }
  30. }
  31. //get all svg and png files
  32. $accepted_extensions = array('.svg', '.png');
  33. $png_svg_files = [];
  34. if (is_array($all_files) && count($all_files) > 0) {
  35. foreach ($all_files as & $file) {
  36. $slideshow_extension = strrchr($file, '.');
  37. $slideshow_extension = strtolower($slideshow_extension);
  38. if (in_array($slideshow_extension, $accepted_extensions)) {
  39. $png_svg_files[] =$file;
  40. }
  41. }
  42. }
  43. $disk_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document/images/gallery/';
  44. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document/images/gallery/';
  45. $style = '<style>';
  46. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'base.css";';
  47. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'themes/'.api_get_visual_theme().'/default.css";';
  48. $style .='</style>';
  49. ?>
  50. <!doctype html>
  51. <?php echo api_get_jquery_js(); ?>
  52. <?php echo $style ?>
  53. <body>
  54. <?php
  55. echo '<h2>'.get_lang('Course').': '.$course_info['name'].'</h2>';
  56. if (!empty($png_svg_files)) {
  57. echo '<h3>'.get_lang('SelectSVGEditImage').'</h3>';
  58. echo '<ul>';
  59. foreach($png_svg_files as $filename) {
  60. $image=$disk_path.$filename;
  61. if (strpos($filename, "svg")){
  62. $new_sizes['width'] = 60;
  63. $new_sizes['height'] = 60;
  64. }
  65. else {
  66. $new_sizes = api_resize_image($image, 60, 60);
  67. }
  68. echo '<li style="display:inline; padding:8px;"><a href="'.$web_path.$filename.'" alt "'.$filename.'" title="'.$filename.'"><img src="'.$web_path.$filename.'" width="'.$new_sizes['width'].'" height="'.$new_sizes['height'].'" border="0"></a></li>';
  69. }
  70. echo '</ul>';
  71. } else {
  72. echo Display::return_message(get_lang('NoSVGImagesInImagesGalleryPath'), 'warning');
  73. }
  74. ?>
  75. </body>
  76. <script>
  77. $('a').click(function() {
  78. var href = this.href;
  79. // Convert Non-SVG images to data URL first
  80. // (this could also have been done server-side by the library)
  81. if(this.href.indexOf('.svg') === -1) {
  82. var meta_str = JSON.stringify({
  83. name: $(this).text(),
  84. id: href
  85. });
  86. window.top.postMessage(meta_str, "*");
  87. var img = new Image();
  88. img.onload = function() {
  89. var canvas = document.createElement("canvas");
  90. canvas.width = this.width;
  91. canvas.height = this.height;
  92. // load the raster image into the canvas
  93. canvas.getContext("2d").drawImage(this,0,0);
  94. // retrieve the data: URL
  95. try {
  96. var dataurl = canvas.toDataURL();
  97. } catch(err) {
  98. // This fails in Firefox with file:// URLs :(
  99. alert("Data URL conversion failed: " + err);
  100. var dataurl = "";
  101. }
  102. window.top.postMessage('|' + href + '|' + dataurl, "*");
  103. }
  104. img.src = href;
  105. } else {
  106. // Send metadata (also indicates file is about to be sent)
  107. var meta_str = JSON.stringify({
  108. name: $(this).text(),
  109. id: href
  110. });
  111. window.top.postMessage(meta_str, "*");
  112. // Do ajax request for image's href value
  113. $.get(href, function(data) {
  114. data = '|' + href + '|' + data;
  115. // This is where the magic happens!
  116. window.top.postMessage(data, "*");
  117. }, 'html'); // 'html' is necessary to keep returned data as a string
  118. }
  119. return false;
  120. });
  121. </script>