Course image file extensions — SVG not possible?

Course image file extensions — SVG not possible?

by Steven A -
Number of replies: 2
Moodle 3.8.1
Boost

Is it possible to use SVG files as course images? I can't seem to get it to work.

1. Site administration > Appearance > Courses > Course image file extensions
2. Add SVG to the list (.jpg,.gif,.png,.svg)
3. Go into a course, then "Edit course settings"
4. Confirm that "Course image" accepts the following file types:
  • Image (GIF) .gif
  • Image (JPEG) .jpg
  • Image (PNG) .png
  • Image (SVG+XML) .svg
5. Upload an SVG. Save.
6. Go to Available courses.

SVGs aren't displayed. They're linked (forcedownload). PNGs work. I'll attach a screenshot.
Attachment Screen Shot 2020-02-05 at 6.04.44 PM.png
Average of ratings: -
In reply to Steven A

Re: Course image file extensions — SVG not possible?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Ok....

'coursecat_coursebox_content()' in '/course/renderer.php' has:

        $contentimages = $contentfiles = '';
        foreach ($course->get_course_overviewfiles() as $file) {
            $isimage = $file->is_valid_image();
            $url = file_encode_url("$CFG->wwwroot/pluginfile.php",
                    '/'. $file->get_contextid(). '/'. $file->get_component(). '/'.
                    $file->get_filearea(). $file->get_filepath(). $file->get_filename(), !$isimage);
            if ($isimage) {
                $contentimages .= html_writer::tag('div',
                        html_writer::empty_tag('img', array('src' => $url)),
                        array('class' => 'courseimage'));
            } else {
                $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
                $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
                        html_writer::tag('span', $file->get_filename(), array('class' => 'fp-filename'));
                $contentfiles .= html_writer::tag('span',
                        html_writer::link($url, $filename),
                        array('class' => 'coursefile fp-filename-icon'));
            }
        }

where 'is_valid_image' determines if it is an image.  So in '/lib/filestorage/stored_file.php' that method is:

    /**
     * Verifies the file is a valid web image - gif, png and jpeg only.
     *
     * It should be ok to serve this image from server without any other security workarounds.
     *
     * @return bool true if file ok
     */
    public function is_valid_image() {
        $mimetype = $this->get_mimetype();
        if (!file_mimetype_in_typegroup($mimetype, 'web_image')) {
            return false;
        }
        if (!$info = $this->get_imageinfo()) {
            return false;
        }
        if ($info['mimetype'] !== $mimetype) {
            return false;
        }
        // ok, GD likes this image
        return true;
    }

Thus I suspect as Font's can also be svg's then SVG images are not detected at the moment.

There is MDL-55243, so please make a case there.