From 36428e47354d63c7f24b514ecfb5a35208319940 Mon Sep 17 00:00:00 2001 From: Fredrik Ekelund Date: Fri, 2 Dec 2016 18:21:59 +0100 Subject: [PATCH] Added ability to pass an array of explicit widths to ImageMedium#derivatives (#1133) * Added ability to pass an array of explicit widths to ImageMedium#derivatives Allows for more precise control than the min-width, max-width and step parameters. * ImageMedium#derivatives can now be called with an array from Markdown as well Previously it was only possible from Twig code or PHP code --- system/src/Grav/Common/Helpers/Excerpts.php | 11 +++++- .../Grav/Common/Page/Medium/ImageMedium.php | 37 +++++++++++++------ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/system/src/Grav/Common/Helpers/Excerpts.php b/system/src/Grav/Common/Helpers/Excerpts.php index 090dd11218..3ab86ce038 100644 --- a/system/src/Grav/Common/Helpers/Excerpts.php +++ b/system/src/Grav/Common/Helpers/Excerpts.php @@ -282,8 +282,15 @@ public static function processMediaActions($medium, $url) // loop through actions for the image and call them foreach ($actions as $action) { - $medium = call_user_func_array([$medium, $action['method']], - explode(',', $action['params'])); + $matches = []; + + if (preg_match('/\[(.*)\]/', $action['params'], $matches)) { + $args = [explode(',', $matches[1])]; + } else { + $args = explode(',', $action['params']); + } + + $medium = call_user_func_array([$medium, $action['method']], $args); } if (isset($url_parts['fragment'])) { diff --git a/system/src/Grav/Common/Page/Medium/ImageMedium.php b/system/src/Grav/Common/Page/Medium/ImageMedium.php index 94602e336d..2e92ccfd8e 100644 --- a/system/src/Grav/Common/Page/Medium/ImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/ImageMedium.php @@ -236,14 +236,16 @@ public function getImagePrettyName() } /** - * Generate derivatives + * Generate alternative image widths, using either an array of integers, or + * a min width, a max width, and a step parameter to fill out the necessary + * widths. Existing image alternatives won't be overwritten. * - * @param int $min_width - * @param int $max_width - * @param int $step + * @param int|int[] $min_width + * @param int [$max_width=2500] + * @param int [$step=200] * @return $this */ - public function derivatives($min_width, $max_width, $step = 200) { + public function derivatives($min_width, $max_width = 2500, $step = 200) { if (!empty($this->alternatives)) { $max = max(array_keys($this->alternatives)); $base = $this->alternatives[$max]; @@ -251,10 +253,23 @@ public function derivatives($min_width, $max_width, $step = 200) { $base = $this; } - // Do not upscale images. - $max_width = min($max_width, $base->get('width')); + $widths = []; - for ($width = $min_width; $width < $max_width; $width = $width + $step) { + if (func_num_args() === 1) { + foreach ((array) func_get_arg(0) as $width) { + if ($width < $base->get('width')) { + $widths[] = $width; + } + } + } else { + $max_width = min($max_width, $base->get('width')); + + for ($width = $min_width; $width < $max_width; $width = $width + $step) { + $widths[] = $width; + } + } + + foreach ($widths as $width) { // Only generate image alternatives that don't already exist if (array_key_exists((int) $width, $this->alternatives)) { continue; @@ -267,10 +282,10 @@ public function derivatives($min_width, $max_width, $step = 200) { // retrieved from the page cache if (isset($derivative)) { $index = 2; - $widths = array_keys($this->alternatives); - sort($widths); + $alt_widths = array_keys($this->alternatives); + sort($alt_widths); - foreach ($widths as $i => $key) { + foreach ($alt_widths as $i => $key) { if ($width > $key) { $index += max($i, 1); }