Skip to content

Commit

Permalink
Issue #6: Preserve width attributes
Browse files Browse the repository at this point in the history
Conflicts:
	version.php
  • Loading branch information
gthomas2 committed Nov 6, 2017
1 parent ee071f6 commit d38d3d7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
11 changes: 11 additions & 0 deletions classes/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 = '/(?<=\<img)(?:.*)width(?:\s|)=(?:"|\')(\d*)(?:px|)(?:"|\')/';
$matches = [];
preg_match($regex, $img, $matches);
if (!empty($matches[1])) {
$checkwidth = $matches[1];
if ($checkwidth < $maxwidth) {
// This image already has a width attribute and that width is less than the max width.
return $img;
}
}
} else {
// Return img tag as is with width preserved.
return $img;
}
}

if ($width > $maxwidth) {
$ratio = $height / $width;
$width = $maxwidth;
Expand Down
4 changes: 4 additions & 0 deletions lang/en/filter_imageopt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
13 changes: 13 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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));
}
6 changes: 3 additions & 3 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit d38d3d7

Please sign in to comment.