Skip to content

Commit

Permalink
Added ability to pass an array of explicit widths to ImageMedium#deri…
Browse files Browse the repository at this point in the history
…vatives (#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
  • Loading branch information
fredrikekelund authored and rhukster committed Dec 2, 2016
1 parent 10da784 commit 36428e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
11 changes: 9 additions & 2 deletions system/src/Grav/Common/Helpers/Excerpts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
37 changes: 26 additions & 11 deletions system/src/Grav/Common/Page/Medium/ImageMedium.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,25 +236,40 @@ 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];
} else {
$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;
Expand All @@ -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);
}
Expand Down

0 comments on commit 36428e4

Please sign in to comment.