From d310cd499914c48172687e61fd5109dddcfcdc31 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 10 Jan 2022 13:52:30 +0800 Subject: [PATCH 1/2] Fix PHPDocs --- classes/local.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/local.php b/classes/local.php index 69b3c39..ad96e02 100644 --- a/classes/local.php +++ b/classes/local.php @@ -200,7 +200,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) { From 299fa6d51491b208f4954f972906f59e297a3ad5 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 10 Jan 2022 14:15:54 +0800 Subject: [PATCH 2/2] Refactor to use core file_storage->convert_image() (#15) --- classes/image.php | 238 ---------------------------------------------- classes/local.php | 10 ++ filter.php | 5 +- lib.php | 24 +++-- settings.php | 10 +- 5 files changed, 28 insertions(+), 259 deletions(-) delete mode 100644 classes/image.php diff --git a/classes/image.php b/classes/image.php deleted file mode 100644 index da2d8bd..0000000 --- a/classes/image.php +++ /dev/null @@ -1,238 +0,0 @@ -. - -/** - * Image processing. - * - * Provides image resizing functionality. - * - * @package filter_imageopt - * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -namespace filter_imageopt; - -defined('MOODLE_INTERNAL') || die(); - -use stored_file; - -require_once($CFG->libdir.'/gdlib.php'); - -/** - * Image processing class. - * - * Provides image resizing functionality. - * - * @package filter_imageopt - * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com) - * @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 - * - * @param stored_file $originalfile - * @param string $resizefilepath - * @param bool|string $resizefilename - * @param int $newwidth - * @param int $newheight - * @param int $jpgquality - * @return stored_file - */ - public static function resize ( - stored_file $originalfile, - $resizefilepath, - $resizefilename = false, - $newwidth = false, - $newheight = false, - $jpgquality = 90 - ) { - - raise_memory_limit(MEMORY_EXTRA); - - if (substr($resizefilepath, -1) !== '/') { - $resizefilepath .= '/'; - } - - if ($resizefilename === false) { - $resizefilename = $originalfile->get_filename(); - } - - if (!$newwidth && !$newheight) { - return false; - } - - $contextid = $originalfile->get_contextid(); - $component = $originalfile->get_component(); - $filearea = $originalfile->get_filearea(); - $itemid = $originalfile->get_itemid(); - - $imageinfo = (object) $originalfile->get_imageinfo(); - $imagefnc = ''; - if (empty($imageinfo)) { - return false; - } - - // Create temporary image for processing. - $tmpimage = tempnam(sys_get_temp_dir(), 'tmpimg'); - file_put_contents($tmpimage, $originalfile->get_content()); - - if (!$newheight) { - $m = $imageinfo->height / $imageinfo->width; // Multiplier to work out $newheight. - $newheight = $newwidth * $m; - } else if (!$newwidth) { - $m = $imageinfo->width / $imageinfo->height; // Multiplier to work out $newwidth. - $newwidth = $newheight * $m; - } - $t = null; - switch ($imageinfo->mimetype) { - case 'image/gif': - if (function_exists('imagecreatefromgif')) { - $im = imagecreatefromgif($tmpimage); - } else { - debugging('GIF not supported on this server'); - unlink ($tmpimage); - return false; - } - // Guess transparent colour from GIF. - $transparent = imagecolortransparent($im); - if ($transparent != -1) { - $t = imagecolorsforindex($im, $transparent); - } - break; - case 'image/jpeg': - if (function_exists('imagecreatefromjpeg')) { - $im = imagecreatefromjpeg($tmpimage); - } else { - debugging('JPEG not supported on this server'); - unlink ($tmpimage); - return false; - } - // If the user uploads a jpeg them we should process as a jpeg if possible. - if (function_exists('imagejpeg')) { - $imagefnc = 'imagejpeg'; - $filters = null; // Not used. - $quality = $jpgquality; - } else if (function_exists('imagepng')) { - $imagefnc = 'imagepng'; - $filters = PNG_NO_FILTER; - $quality = 1; - } else { - debugging('Jpeg and png not supported on this server, please fix server configuration'); - unlink ($tmpimage); - return false; - } - break; - case 'image/png': - if (function_exists('imagecreatefrompng')) { - $im = imagecreatefrompng($tmpimage); - } else { - debugging('PNG not supported on this server'); - unlink ($tmpimage); - return false; - } - break; - default: - unlink ($tmpimage); - return false; - } - unlink ($tmpimage); - - // The default for all images other than jpegs is to try imagepng first. - if (empty($imagefnc)) { - if (function_exists('imagepng')) { - $imagefnc = 'imagepng'; - $filters = PNG_NO_FILTER; - $quality = 1; - } else if (function_exists('imagejpeg')) { - $imagefnc = 'imagejpeg'; - $filters = null; // Not used. - $quality = $jpgquality; - } else { - debugging('Jpeg and png not supported on this server, please fix server configuration'); - return false; - } - } - - if (function_exists('imagecreatetruecolor')) { - $newimage = imagecreatetruecolor($newwidth, $newheight); - if ($imageinfo->mimetype != 'image/jpeg' and $imagefnc === 'imagepng') { - if ($t) { - // Transparent GIF hacking... - $transparentcolour = imagecolorallocate($newimage , $t['red'] , $t['green'] , $t['blue']); - imagecolortransparent($newimage , $transparentcolour); - } - - imagealphablending($newimage, false); - $color = imagecolorallocatealpha($newimage, 0, 0, 0, 127); - imagefill($newimage, 0, 0, $color); - imagesavealpha($newimage, true); - - } - } else { - $newimage = imagecreate($newwidth, $newheight); - } - - imagecopybicubic($newimage, $im, 0, 0, 0, 0, $newwidth, $newheight, $imageinfo->width, $imageinfo->height); - - $fs = get_file_storage(); - $newimageparams = array( - 'contextid' => $contextid, - 'component' => $component, - 'filearea' => $filearea, - 'itemid' => $itemid, - 'filepath' => $resizefilepath - ); - - $dirs = explode('/', $resizefilepath); - $dirpath = '/'; - - foreach ($dirs as $dir) { - if (empty($dir)) { - continue; - } - $dirpath .= $dir.'/'; - $fs->create_directory($contextid, $component, $filearea, $itemid, $dirpath); - } - - ob_start(); - if (!$imagefnc($newimage, null, $quality, $filters)) { - return false; - } - - $data = ob_get_clean(); - imagedestroy($newimage); - $newimageparams['filename'] = $resizefilename; - if ($resizefilename === $originalfile->get_filename() && $resizefilepath === $originalfile->get_filepath()) { - $originalfile->delete(); - } - $file1 = $fs->create_file_from_string($newimageparams, $data); - return $file1; - } -} diff --git a/classes/local.php b/classes/local.php index ad96e02..c46476c 100644 --- a/classes/local.php +++ b/classes/local.php @@ -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. */ diff --git a/filter.php b/filter.php index 39ba74f..24a57a0 100644 --- a/filter.php +++ b/filter.php @@ -24,7 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -use filter_imageopt\image; use filter_imageopt\local; /** @@ -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)) { @@ -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 = '/(?<=\get_itemid(); - $component = $originalfile->get_component(); - $filename = $originalfile->get_filename(); - $filearea = $originalfile->get_filearea(); - $pathinfo = pathinfo($filename); $originalts = $originalfile->get_timemodified(); @@ -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) { diff --git a/settings.php b/settings.php index bbbe756..6648c59 100644 --- a/settings.php +++ b/settings.php @@ -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 = [ @@ -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)); }