From d38d3d7586c8989e93e8401cb54bcb91eabcd0d0 Mon Sep 17 00:00:00 2001 From: gthomas2 Date: Fri, 3 Nov 2017 16:03:07 +0000 Subject: [PATCH] Issue #6: Preserve width attributes Conflicts: version.php --- classes/image.php | 11 +++++++++++ filter.php | 24 ++++++++++++++++++++++++ lang/en/filter_imageopt.php | 4 ++++ settings.php | 13 +++++++++++++ version.php | 6 +++--- 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/classes/image.php b/classes/image.php index a1f077e..187cc93 100644 --- a/classes/image.php +++ b/classes/image.php @@ -42,6 +42,17 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class image { + + /** + * Always preserve a width attribute already in an image tag. + */ + const WIDTHATTPRESERVE = 0; + + /** + * Only preserve a width attribute if it's less than the maximum width. + */ + const WIDTHATTPRSERVELTMAX = 1; + /** * Shame that this was nicked from gdlib.php and that there isn't a function I could have used from there. * Creates a resized version of image and stores copy in file area diff --git a/filter.php b/filter.php index d1ee762..1a35579 100644 --- a/filter.php +++ b/filter.php @@ -51,6 +51,11 @@ public function __construct(context $context, array $localconfig) { require_once($CFG->libdir.'/filelib.php'); $this->config = get_config('filter_imageopt'); + if (!isset($this->config->widthattribute)) { + $this->config->widthattribute = image::WIDTHATTPRSERVELTMAX; + } + $this->config->widthattribute = intval($this->config->widthattribute); + parent::__construct($context, $localconfig); } @@ -124,6 +129,25 @@ private function imageopturl($file) { private function img_add_width_height($img, $width, $height) { $maxwidth = $this->config->maxwidth; + if (stripos($img, ' width') !== false) { + if ($this->config->widthattribute === image::WIDTHATTPRSERVELTMAX) { + // Note - we cannot check for percentage widths as they are responsively variable. + $regex = '/(?<=\ $maxwidth) { $ratio = $height / $width; $width = $maxwidth; diff --git a/lang/en/filter_imageopt.php b/lang/en/filter_imageopt.php index c12096e..a77b0c6 100644 --- a/lang/en/filter_imageopt.php +++ b/lang/en/filter_imageopt.php @@ -28,3 +28,7 @@ $string['donotloadonvisible'] = 'No placeholding, load immediately'; $string['loadonvisibilityall'] = 'All images'; $string['loadonvisibilityafter'] = 'After {$a} image(s)'; +$string['widthattribute'] = 'Image width attribute'; +$string['widthattributedesc'] = 'How should existing image width attributes be dealt with'; +$string['widthattpreserve'] = 'Preserve user width attributes in all cases'; +$string['widthattpreserveltmax'] = 'Preserve user width attributes only if less than maximum width'; diff --git a/settings.php b/settings.php index cdc11a5..b2025dd 100644 --- a/settings.php +++ b/settings.php @@ -23,6 +23,11 @@ defined('MOODLE_INTERNAL') || die; +// You can never trust autoloading in settings.php! +require_once(__DIR__.'/classes/image.php'); + +use filter_imageopt\image; + if ($ADMIN->fulltree) { $choices = [ 480 => '480', @@ -42,4 +47,12 @@ $choices[999] = get_string('donotloadonvisible', 'filter_imageopt'); $settings->add(new admin_setting_configselect('filter_imageopt/loadonvisible', get_string('loadonvisible', 'filter_imageopt'), get_string('loadonvisibledesc', 'filter_imageopt'), 5, $choices)); + + $choices = [ + image::WIDTHATTPRSERVELTMAX => get_string('widthattpreserveltmax', 'filter_imageopt'), + image::WIDTHATTPRESERVE => get_string('widthattpreserve', 'filter_imageopt') + ]; + $settings->add(new admin_setting_configselect('filter_imageopt/widthattribute', + get_string('widthattribute', 'filter_imageopt'), + get_string('widthattributedesc', 'filter_imageopt'), image::WIDTHATTPRSERVELTMAX, $choices)); } diff --git a/version.php b/version.php index c81b696..8893146 100644 --- a/version.php +++ b/version.php @@ -22,8 +22,8 @@ */ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016121301; +$plugin->version = 2017110300; $plugin->requires = 2011111500; $plugin->component = 'filter_imageopt'; -$plugin->maturity = MATURITY_ALPHA; -$plugin->release = '3.2.0.0'; +$plugin->maturity = MATURITY_BETA; +$plugin->release = '3.4.0.0';