diff --git a/lang/en/pdfannotator.php b/lang/en/pdfannotator.php index e237efc..44b13d9 100644 --- a/lang/en/pdfannotator.php +++ b/lang/en/pdfannotator.php @@ -102,12 +102,15 @@ $string['error:deleteAnnotation'] = 'An error has occured while deleting an annotation.'; $string['error:editAnnotation'] = 'An error has occurred while editing an annotation.'; $string['error:editcomment'] = 'An error has occured while trying to edit a comment.'; +$string['error:findimage'] = 'An error occured while trying to find image {$a}.'; $string['error:forwardquestion'] = 'An error has occured while forwarding the question.'; $string['error:forwardquestionnorecipient'] = 'An error has occured while forwarding the question.: No person in this course has the capability to receive forwarded questions.'; $string['error:getAllQuestions'] = 'An error has occured while getting the questions of this document.'; $string['error:getAnnotation'] = 'An error has occured while getting the annotation.'; $string['error:getAnnotations'] = 'An error has occured while getting all annotations.'; $string['error:getComments'] = 'An error has occured while getting the comments.'; +$string['error:getimageheight'] = 'An error has occured while getting image height of {$a}.'; +$string['error:getimageheight'] = 'An error has occured while getting image width of {$a}.'; $string['error:getQuestions'] = 'An error has occured while getting the questions for this page.'; $string['error:printComments'] = 'Error with data from server.'; $string['error:hideComment'] = "An error has occured while trying to hide the comment from participants' view."; diff --git a/locallib.php b/locallib.php index fba0514..ed52858 100644 --- a/locallib.php +++ b/locallib.php @@ -64,7 +64,7 @@ function pdfannotator_display_embed($pdfannotator, $cm, $course, $file, $page = // Load and execute the javascript files. $PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/pdf.js?ver=00002")); $PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/textclipper.js")); - $PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/index.js?ver=00035")); + $PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/index.js?ver=00036")); $PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/locallib.js?ver=00006")); // Pass parameters from PHP to JavaScript. @@ -210,42 +210,65 @@ function pdfannotator_split_content_image($content, $res, $itemid, $context=null } $tempinfo = []; + $encodedurl = urldecode($url[0]); foreach($fileinfo as $file) { - $count = substr_count(urldecode($url[0]), $file['filename']); + $count = substr_count($encodedurl, $file['filename']); if($count) { $tempinfo = $file; break; } } - if($tempinfo) { - $imagedata = 'data:' . $tempinfo['filemimetype'] . ';base64,' . base64_encode($tempinfo['filecontent']); - $data['image'] = $imagedata; - $data['format'] = $tempinfo['filemimetype']; - $data['fileid'] = $tempinfo['fileid']; - $data['filename'] = $tempinfo['filename']; - $data['filepath'] = $tempinfo['filepath']; - $data['filesize'] = $tempinfo['filesize']; - $data['imagestorage'] = 'intern'; + try { + if($tempinfo) { + $imagedata = 'data:' . $tempinfo['filemimetype'] . ';base64,' . base64_encode($tempinfo['filecontent']); + $data['image'] = $imagedata; + $data['format'] = $tempinfo['filemimetype']; + $data['fileid'] = $tempinfo['fileid']; + $data['filename'] = $tempinfo['filename']; + $data['filepath'] = $tempinfo['filepath']; + $data['filesize'] = $tempinfo['filesize']; + $data['imagestorage'] = 'intern'; + } else if (!str_contains($CFG->wwwroot, $url[0])){ + $data['imagestorage'] = 'extern'; + $data['format'] = $format[0]; + $imgcontent = @file_get_contents($url[0]); + if ($imgcontent) { + $data['image'] = 'data:image/' . $format[0] . ";base64," . base64_encode($imgcontent); + } else { + throw new Exception(get_string('error:findimage', 'pdfannotator', $encodedurl)); + } + } else { + throw new Exception(get_string('error:findimage', 'pdfannotator', $encodedurl)); + } + preg_match('/height=[0-9]+/', $imgstr, $height); - $data['imageheight'] = str_replace("\"", "", explode('=', $height[0])[1]); + if ($height) { + $data['imageheight'] = str_replace("\"", "", explode('=', $height[0])[1]); + } else if (!$height && $data['imagestorage'] === 'extern') { + $imagemetadata = getimagesize($url[0]); + $data['imageheight'] = $imagemetadata[1]; + } else { + throw new Exception(get_string('error:getimageheight', 'pdfannotator', $encodedurl)); + } preg_match('/width=[0-9]+/', $imgstr, $width); - $data['imagewidth'] = str_replace("\"", "", explode('=', $width[0])[1]); - } else if (!str_contains($CFG->wwwroot, $url[0])){ - $data['imagestorage'] = 'extern'; - $data['format'] = $format[0]; - $imagemetadata = getimagesize($url[0]); - $data['image'] = 'data:image/' . $format[0] . ";base64," . base64_encode(file_get_contents($url[0])); - $data['imagewidth'] = $imagemetadata[0]; - $data['imageheight'] = $imagemetadata[1]; - } else { - $data['success'] = "error"; - $data['message'] = "cannot load image"; + if ($width) { + $data['imagewidth'] = str_replace("\"", "", explode('=', $width[0])[1]); + } else if (!$width && $data['imagestorage'] === 'extern') { + $imagemetadata = getimagesize($url[0]); + $data['imagewidth'] = $imagemetadata[0]; + } else { + throw new Exception(get_string('error:getimagewidth', 'pdfannotator', $encodedurl)); + } + } catch (Exception $ex) { + $data['image'] = "error"; + $data['message'] = $ex->getMessage(); + } finally { + $res[] = $firststr; + $res[] = $data; + $content = $laststr; } - $res[] = $firststr; - $res[] = $data; - $content = $laststr; } $res[] = $content; diff --git a/shared/index.js b/shared/index.js index 0b885d0..d5143a8 100644 --- a/shared/index.js +++ b/shared/index.js @@ -815,7 +815,7 @@ function startIndex(Y,_cm,_documentObject,_contextId, _userid,_capabilities, _to * Take a text block, split it into pieces no larger than 130 characters * and print one piece per line */ - function printTextblock(author=null, timemodified=null, text, characters) { + function printTextblock(author=null, timemodified=null, text, characters = 130) { // In the comments linebreaks are represented by
-Tags. Sometimes there is an additional \n // jsPDF needs \n-linebreaks so we replace
with \n. But first we remove all \n that already exist. text = text.replace(/\n/g, ""); @@ -858,7 +858,7 @@ function startIndex(Y,_cm,_documentObject,_contextId, _userid,_capabilities, _to var url; var image; - if (data['success'] !== 'error') { + if (data['image'] !== 'error') { image = data['image']; var height = data['imageheight'] * 0.264583333333334; // Convert pixel into mm. // Reduce height and witdh if its size more than a4height. diff --git a/version.php b/version.php index 1790e6a..fc42443 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'mod_pdfannotator'; -$plugin->version = 2022110900; +$plugin->version = 2022110902; $plugin->release = 'PDF Annotator v1.4 release 11'; $plugin->requires = 2021051700; $plugin->maturity = MATURITY_STABLE;