function get_page_image($pageno, $submission) { global $CFG, $DB; $pagefilename = 'page' . $pageno . '.png'; $pdf = new MyPDFLib(); $pagecount = $submission->numfiles >> 1; // Extract the pagecount from 'numfiles' (may be 0) $fs = get_file_storage(); // If pagecount is 0, then we need to skip down to the next stage to find the real page count if ($pagecount && ($file = $fs->get_file($this->context->id, 'mod_assignment', 'image', $submission->id, '/', $pagefilename))) { if ($imageinfo = $file->get_imageinfo()) { $imgurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/mod_assignment/image/' . $submission->id . '/' . $pagefilename); // Prevent browser from caching image if it has changed if (strpos($imgurl, '?') === false) { $imgurl .= '?ts=' . $file->get_timemodified(); } else { $imgurl .= '&ts=' . $file->get_timemodified(); } return array($imgurl, $imageinfo['width'], $imageinfo['height'], $pagecount); } // If the image is bad in some way, try to create a new image instead } // Generate the image $tempfolder = $this->get_temp_folder($submission->id); $imagefolder = $tempfolder . 'img'; if (!file_exists($imagefolder)) { if (!mkdir($imagefolder, 0777, true)) { echo "Unable to create temporary image folder"; die; } } $pdffolder = $tempfolder . 'sub'; $pdffile = $pdffolder . '/submission.pdf'; if (!file_exists($pdffolder)) { if (!mkdir($pdffolder, 0777, true)) { echo "Unable to create temporary folder"; die; } } if (!($file = $fs->get_file($this->context->id, 'mod_assignment', 'submissionfinal', $submission->id, '/', 'submission.pdf'))) { print_error('Attempting to display image for non-existant submission'); } $file->copy_content_to($pdffile); // Copy the PDF out of the file storage, into the temp area $pagecount = $pdf->set_pdf($pdffile, $pagecount); // Only loads the PDF if the pagecount is unknown (0) if ($pageno > $pagecount) { unlink($pdffile); return array(null, 0, 0, $pagecount); } $pdf->set_image_folder($imagefolder); if (!($imgname = $pdf->get_image($pageno))) { // Generate the image in the temp area print_error(get_string('errorgenerateimage', 'assignment_uploadpdf')); } if (($submission->numfiles & 1) == 0) { $submission->numfiles = $pagecount << 1 | 1; // Use this as a flag that there are images to delete at some point // Maybe switch to just searching the filestorage database to find old images? $updated = new stdClass(); $updated->id = $submission->id; $updated->numfiles = $submission->numfiles; $DB->update_record('assignment_submissions', $updated); } $imginfo = array('contextid' => $this->context->id, 'component' => 'mod_assignment', 'filearea' => 'image', 'itemid' => $submission->id, 'filepath' => '/', 'filename' => $pagefilename); $file = $fs->create_file_from_pathname($imginfo, $imagefolder . '/' . $imgname); // Copy the image into the file storage //Delete the temporary files unlink($pdffile); unlink($imagefolder . '/' . $imgname); rmdir($imagefolder); rmdir($pdffolder); rmdir($tempfolder); if ($imageinfo = $file->get_imageinfo()) { $imgurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/mod_assignment/image/' . $submission->id . '/' . $pagefilename); // Prevent browser from caching image if it has changed if (strpos($imgurl, '?') === false) { $imgurl .= '?ts=' . $file->get_timemodified(); } else { $imgurl .= '&ts=' . $file->get_timemodified(); } return array($imgurl, $imageinfo['width'], $imageinfo['height'], $pagecount); } return array(null, 0, 0, $pagecount); }
function get_page_image($userid, $pageno, $submission) { global $CFG; $imagefolder = $CFG->dataroot . '/' . $this->file_area_name($userid) . '/images'; check_dir_exists($imagefolder, true, true); $pdffile = $CFG->dataroot . '/' . $this->file_area_name($userid) . '/submission/submission.pdf'; // Check folder exists + file exists if (!file_exists($pdffile)) { error('Attempting to display image for non-existing submission'); } $pdf = new MyPDFLib(); $pdf->set_image_folder($imagefolder); $pagecount = $submission->numfiles >> 1; // Extract the pagecount from 'numfiles' (may be 0) $pagecount = $pdf->set_pdf($pdffile, $pagecount); // Only loads the PDF if the pagecount is unknown (0) if ($pageno > $pagecount) { return array(null, 0, 0, $pagecount); } if (!($imgname = $pdf->get_image($pageno))) { error(get_string('errorgenerateimage', 'assignment_uploadpdf')); } if (($submission->numfiles & 1) == 0) { $submission->numfiles = $pagecount << 1 | 1; /* Use this as a flag that there are images to delete at some point */ $updated = new Object(); $updated->id = $submission->id; $updated->numfiles = $submission->numfiles; update_record('assignment_submissions', $updated); } $imageurl = $CFG->wwwroot . '/file.php?file=/' . $this->file_area_name($userid) . '/images/' . $imgname; list($imgwidth, $imgheight, $imgtype, $imgattr) = getimagesize($CFG->dataroot . '/' . $this->file_area_name($userid) . '/images/' . $imgname); return array($imageurl, $imgwidth, $imgheight, $pagecount); }