Ejemplo n.º 1
0
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;
}