function emarking_create_anonymous_page_from_storedfile(stored_file $file, $student) { if (!$file) { throw new Exception('Stored file does not exist'); } // Get file storage and copy file to temp folder. $fs = get_file_storage(); $tmppath = $file->copy_content_to_temp('emarking', 'anonymous'); // Treat the file as an image, get its size and draw a white rectangle. $size = getimagesize($tmppath, $info); $imagemime = exif_imagetype($tmppath); if ($imagemime == IMAGETYPE_PNG) { $image = imagecreatefrompng($tmppath); } elseif ($imagemime == IMAGETYPE_JPEG) { $image = imagecreatefromjpeg($tmppath); } else { throw new Exception('Unsupported file type for pages'); } if (!$image) { throw new Exception('Could not read image from ' . $info[0] . ' ' . $tmppath); } $white = imagecolorallocate($image, 255, 255, 255); $y2 = round($size[1] / 10, 0); imagefilledrectangle($image, 0, 0, $size[0], $y2, $white); // Save the new image to replace the file. if ($imagemime == IMAGETYPE_PNG) { if (!imagepng($image, $tmppath)) { throw new Exception('Could not save image as PNG to ' . $tmppath); } } elseif ($imagemime == IMAGETYPE_JPEG) { if (!imagejpeg($image, $tmppath, 100)) { throw new Exception('Could not save image as JPEG to ' . $tmppath); } } else { throw new Exception('Unsupported file type for saving page'); } clearstatcache(); $filenameanonymous = emarking_get_anonymous_filename($file->get_filename()); // Copy file from temp folder to Moodle's filesystem. $filerecordanonymous = array('contextid' => $file->get_contextid(), 'component' => 'mod_emarking', 'filearea' => 'pages', 'itemid' => $file->get_itemid(), 'filepath' => '/', 'filename' => $filenameanonymous, 'timecreated' => $file->get_timecreated(), 'timemodified' => time(), 'userid' => $student->id, 'author' => $student->firstname . ' ' . $student->lastname, 'license' => 'allrightsreserved'); $previousfile = $fs->get_file($file->get_contextid(), 'mod_emarking', 'pages', $file->get_itemid(), '/', $filenameanonymous); if ($previousfile) { $previousfile->delete(); } $fileinfo = $fs->create_file_from_pathname($filerecordanonymous, $tmppath); if (!$fileinfo) { throw new Exception('Could not create anonymous version of a stored file'); } return $fileinfo; }
function emarking_create_anonymous_page_from_storedfile(stored_file $file) { if (!$file) { throw new Exception('Stored file does not exist'); } // Get file storage and copy file to temp folder. $fs = get_file_storage(); $tmppath = $file->copy_content_to_temp('emarking', 'anonymous'); // Treat the file as an image, get its size and draw a white rectangle. $size = getimagesize($tmppath); $image = imagecreatefrompng($tmppath); $white = imagecolorallocate($image, 255, 255, 255); $y2 = round($size[1] / 10, 0); imagefilledrectangle($image, 0, 0, $size[0], $y2, $white); // Save the new image to replace the file. if (!imagepng($image, $tmppath)) { return false; } clearstatcache(); $filenameanonymous = emarking_get_anonymous_filename($file->get_filename()); // Copy file from temp folder to Moodle's filesystem. $filerecordanonymous = array('contextid' => $file->get_contextid(), 'component' => 'mod_emarking', 'filearea' => 'pages', 'itemid' => $file->get_itemid(), 'filepath' => '/', 'filename' => $filenameanonymous, 'timecreated' => $file->get_timecreated(), 'timemodified' => time(), 'userid' => $file->get_userid(), 'author' => $file->get_author(), 'license' => 'allrightsreserved'); $previousfile = $fs->get_file($file->get_contextid(), 'mod_emarking', 'pages', $file->get_itemid(), '/', $filenameanonymous); if ($previousfile) { $previousfile->delete(); } $fileinfo = $fs->create_file_from_pathname($filerecordanonymous, $tmppath); return $fileinfo; }