diff --git a/mod/label/lang/en/label.php b/mod/label/lang/en/label.php index b99b6ddced6bc..b8178f68be84a 100644 --- a/mod/label/lang/en/label.php +++ b/mod/label/lang/en/label.php @@ -25,8 +25,12 @@ */ $string['configdndmedia'] = 'Offer to create a label when media files are dragged & dropped onto a course'; +$string['configdndresizeheight'] = 'When a label is created from a dragged & dropped image, resize it if it is higher than this many pixels (0 for no resize)'; +$string['configdndresizewidth'] = 'When a label is created from a dragged & dropped image, resize it if it is wider than this many pixels (0 for no resize)'; $string['dndmedia'] = 'Media drag and drop'; -$string['dnduploadlabel'] = 'Create a label'; +$string['dndresizeheight'] = 'Resize drag and drop height'; +$string['dndresizewidth'] = 'Resize drag and drop width'; +$string['dnduploadlabel'] = 'Add image to course page'; $string['label:addinstance'] = 'Add a new label'; $string['labeltext'] = 'Label text'; $string['modulename'] = 'Label'; diff --git a/mod/label/lib.php b/mod/label/lib.php index e8fd3f315f2d6..7ab153ce52828 100644 --- a/mod/label/lib.php +++ b/mod/label/lib.php @@ -237,20 +237,100 @@ function label_dndupload_handle($uploadinfo) { $data->introformat = FORMAT_HTML; $data->coursemodule = $uploadinfo->coursemodule; + // Extract the first (and only) file from the file area and add it to the label as an img tag. if (!empty($uploadinfo->draftitemid)) { $fs = get_file_storage(); $draftcontext = context_user::instance($USER->id); $context = context_module::instance($uploadinfo->coursemodule); $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false); if ($file = reset($files)) { - $filelink = moodle_url::make_draftfile_url($uploadinfo->draftitemid, $file->get_filepath(), $file->get_filename()); if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) { - $data->intro = html_writer::empty_tag('img', array('src' => $filelink, 'alt' => $file->get_filename())); - $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0, - null, $data->intro); + // It is an image - resize it, if too big, then insert the img tag. + $config = get_config('label'); + $data->intro = label_generate_resized_image($file, $config->dndresizewidth, $config->dndresizeheight); + } else { + // We aren't supposed to be supporting non-image types here, but fallback to adding a link, just in case. + $url = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()); + $data->intro = html_writer::link($url, $file->get_filename()); } + $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0, + null, $data->intro); } } return label_add_instance($data, null); } + +/** + * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image + * @param stored_file $file the image file to process + * @param int $maxwidth the maximum width allowed for the image + * @param int $maxheight the maximum height allowed for the image + * @return string HTML fragment to add to the label + */ +function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) { + global $CFG; + + $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()); + $link = null; + $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl); + + if ($imginfo = $file->get_imageinfo()) { + // Work out the new width / height, bounded by maxwidth / maxheight + $width = $imginfo['width']; + $height = $imginfo['height']; + if (!empty($maxwidth) && $width > $maxwidth) { + $height *= (float)$maxwidth / $width; + $width = $maxwidth; + } + if (!empty($maxheight) && $height > $maxheight) { + $width *= (float)$maxheight / $height; + $height = $maxheight; + } + + $attrib['width'] = $width; + $attrib['height'] = $height; + + // If the size has changed and the image is of a suitable mime type, generate a smaller version + if ($width != $imginfo['width']) { + $mimetype = $file->get_mimetype(); + if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') { + require_once($CFG->libdir.'/gdlib.php'); + $tmproot = make_temp_directory('mod_label'); + $tmpfilepath = $tmproot.'/'.$file->get_contenthash(); + $file->copy_content_to($tmpfilepath); + $data = generate_image_thumbnail($tmpfilepath, $width, $height); + unlink($tmpfilepath); + + if (!empty($data)) { + $fs = get_file_storage(); + $record = array( + 'contextid' => $file->get_contextid(), + 'component' => $file->get_component(), + 'filearea' => $file->get_filearea(), + 'itemid' => $file->get_itemid(), + 'filepath' => '/', + 'filename' => 's_'.$file->get_filename(), + ); + $smallfile = $fs->create_file_from_string($record, $data); + + // Replace the image 'src' with the resized file and link to the original + $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(), + $smallfile->get_filename()); + $link = $fullurl; + } + } + } + + } else { + // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG) + $attrib['width'] = $maxwidth; + } + + $img = html_writer::empty_tag('img', $attrib); + if ($link) { + return html_writer::link($link, $img); + } else { + return $img; + } +} \ No newline at end of file diff --git a/mod/label/settings.php b/mod/label/settings.php index 7ef8d7526a272..b705ca0ebc1ed 100644 --- a/mod/label/settings.php +++ b/mod/label/settings.php @@ -28,4 +28,10 @@ if ($ADMIN->fulltree) { $settings->add(new admin_setting_configcheckbox('label/dndmedia', get_string('dndmedia', 'mod_label'), get_string('configdndmedia', 'mod_label'), 1)); + + $settings->add(new admin_setting_configtext('label/dndresizewidth', + get_string('dndresizewidth', 'mod_label'), get_string('configdndresizewidth', 'mod_label'), 400, PARAM_INT, 6)); + + $settings->add(new admin_setting_configtext('label/dndresizeheight', + get_string('dndresizeheight', 'mod_label'), get_string('configdndresizeheight', 'mod_label'), 400, PARAM_INT, 6)); }