Skip to content

Commit

Permalink
Merge pull request gthomas2#8 from catalyst/file-storage-convert-image
Browse files Browse the repository at this point in the history
Refactor to use core file_storage->convert_image()
  • Loading branch information
cameron1729 committed Jan 11, 2022
2 parents 75e9e3a + 299fa6d commit 5a766ac
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 260 deletions.
238 changes: 0 additions & 238 deletions classes/image.php

This file was deleted.

12 changes: 11 additions & 1 deletion classes/local.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
*/
class local {

/**
* Always preserve a width attribute already in an image tag.
*/
const WIDTH_ATT_PRESERVE = 0;

/**
* Only preserve a width attribute if it's less than the maximum width.
*/
const WIDTH_ATT_PRESERVE_MAX = 1;

/**
* @var string REGEXP_IMGSRC The regular expression used to see if it is an image.
*/
Expand Down Expand Up @@ -200,7 +210,7 @@ public static function url_decode_path($pluginfilepath) {
/**
* Get's an image file from the plugin file path.
*
* @param str $pluginfilepath pluginfile.php/
* @param string $pluginfilepath pluginfile.php/
* @return \stored_file
*/
public static function get_img_file($pluginfilepath) {
Expand Down
5 changes: 2 additions & 3 deletions filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

defined('MOODLE_INTERNAL') || die();

use filter_imageopt\image;
use filter_imageopt\local;

/**
Expand Down Expand Up @@ -54,7 +53,7 @@ public function __construct(context $context, array $localconfig) {

$this->config = get_config('filter_imageopt');
if (!isset($this->config->widthattribute)) {
$this->config->widthattribute = image::WIDTHATTPRSERVELTMAX;
$this->config->widthattribute = local::WIDTH_ATT_PRESERVE_MAX;
}
$this->config->widthattribute = intval($this->config->widthattribute);
if (!isset($this->config->maxwidth)) {
Expand Down Expand Up @@ -98,7 +97,7 @@ private function img_add_width_height($img, $width, $height) {
$maxwidth = $this->config->maxwidth;

if (stripos($img, ' width') !== false) {
if ($this->config->widthattribute === image::WIDTHATTPRSERVELTMAX) {
if ($this->config->widthattribute === local::WIDTH_ATT_PRESERVE_MAX) {
// Note - we cannot check for percentage widths as they are responsively variable.
$regex = '/(?<=\<img)(?:.*)width(?:\s|)=(?:"|\')(\d*)(?:px|)(?:"|\')/';
$matches = [];
Expand Down
24 changes: 11 additions & 13 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use filter_imageopt\image;
use filter_imageopt\local;

defined('MOODLE_INTERNAL') || die();
Expand All @@ -40,16 +39,11 @@
* @return bool
*/
function filter_imageopt_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
global $CFG;

$urlpathid = clean_param($args[0], PARAM_INT);
$originalimgpath = local::get_url_path_by_id($urlpathid);
$originalfile = local::get_img_file($originalimgpath);
$optimisedpath = local::get_optimised_path($originalimgpath);
$optimisedurlpath = local::get_optimised_path($originalimgpath, false);

$fs = get_file_storage();

$optimisedfile = local::get_img_file($optimisedpath);

if ($optimisedfile) {
Expand All @@ -61,11 +55,6 @@ function filter_imageopt_pluginfile($course, $cm, $context, $filearea, $args, $f
$matches = [];
preg_match($regex, $optimisedpath, $matches);
$maxwidth = ($matches[1]);
$item = $originalfile->get_itemid();
$component = $originalfile->get_component();
$filename = $originalfile->get_filename();
$filearea = $originalfile->get_filearea();
$pathinfo = pathinfo($filename);

$originalts = $originalfile->get_timemodified();

Expand All @@ -92,12 +81,21 @@ function filter_imageopt_pluginfile($course, $cm, $context, $filearea, $args, $f
die;
}

$filepos = array_search($filename, $pathcomps, true);
$filepos = array_search($originalfile->get_filename(), $pathcomps, true);
$length = $filepos - $imageoptpos;

$optimiseddirpath = '/'.implode('/', array_slice($pathcomps, $imageoptpos, $length)).'/';

$optimisedfile = image::resize($originalfile, $optimiseddirpath, $filename, $maxwidth);
$filerecord = [
'contextid' => $originalfile->get_contextid(),
'component' => $originalfile->get_component(),
'filearea' => $originalfile->get_filearea(),
'itemid' => $originalfile->get_itemid(),
'filepath' => $optimiseddirpath
];

$fs = new file_storage();
$optimisedfile = $fs->convert_image($filerecord, $originalfile, $maxwidth);
}

if (!$optimisedfile) {
Expand Down
10 changes: 5 additions & 5 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
defined('MOODLE_INTERNAL') || die;

// You can never trust autoloading in settings.php!
require_once(__DIR__.'/classes/image.php');
require_once(__DIR__.'/classes/local.php');

use filter_imageopt\image;
use filter_imageopt\local;

if ($ADMIN->fulltree) {
$choices = [
Expand All @@ -49,10 +49,10 @@
get_string('loadonvisibledesc', 'filter_imageopt'), 5, $choices));

$choices = [
image::WIDTHATTPRSERVELTMAX => get_string('widthattpreserveltmax', 'filter_imageopt'),
image::WIDTHATTPRESERVE => get_string('widthattpreserve', 'filter_imageopt')
local::WIDTH_ATT_PRESERVE_MAX => get_string('widthattpreserveltmax', 'filter_imageopt'),
local::WIDTH_ATT_PRESERVE => 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));
get_string('widthattributedesc', 'filter_imageopt'), local::WIDTH_ATT_PRESERVE_MAX, $choices));
}

0 comments on commit 5a766ac

Please sign in to comment.