Skip to content

Commit

Permalink
Merge pull request #5231 from octobercms/wip/image-resizing
Browse files Browse the repository at this point in the history
Implement core support for `| resize(width, height, options)` filter
  • Loading branch information
Luke Towers authored Aug 22, 2020
2 parents 3864e5b + 779a879 commit c1c728e
Show file tree
Hide file tree
Showing 12 changed files with 1,315 additions and 22 deletions.
7 changes: 7 additions & 0 deletions config/cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
|
| media - generated by the media manager.
| uploads - generated by attachment model relationships.
| resized - generated by System\Classes\ImageResizer or the resize() Twig filter
|
| For each resource you can specify:
|
Expand Down Expand Up @@ -325,6 +326,12 @@
'path' => '/storage/app/media',
],

'resized' => [
'disk' => 'local',
'folder' => 'resized',
'path' => '/storage/app/resized',
],

],

/*
Expand Down
38 changes: 38 additions & 0 deletions modules/backend/widgets/Lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use October\Rain\Router\Helper as RouterHelper;
use System\Helpers\DateTime as DateTimeHelper;
use System\Classes\PluginManager;
use System\Classes\MediaLibrary;
use System\Classes\ImageResizer;
use Backend\Classes\ListColumn;
use Backend\Classes\WidgetBase;
use October\Rain\Database\Model;
Expand Down Expand Up @@ -1187,6 +1189,42 @@ protected function evalTextTypeValue($record, $column, $value)
return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
}

/**
* Process an image value
* @return string
*/
protected function evalImageTypeValue($record, $column, $value)
{
$config = $column->config;

// Get config options with defaults
$width = isset($config['width']) ? $config['width'] : 50;
$height = isset($config['height']) ? $config['height'] : 50;
$options = isset($config['options']) ? $config['options'] : [];

// Handle attachMany relationships
if (isset($record->attachMany[$column->columnName])) {
$image = $value->first();

// Handle attachOne relationships
} elseif (isset($record->attachOne[$column->columnName])) {
$image = $value;

// Handle absolute URLs
} elseif (str_contains($value, '://')) {
$value = $value;

// Assume all other values to be from the media library
} else {
$value = MediaLibrary::url($value);
}

if (!is_null($value)) {
$imageUrl = ImageResizer::filterGetUrl($image, $width, $height, $options);
return "<img src='$imageUrl' width='$width' height='$height' />";
}
}

/**
* Process as number, proxy to text
* @return string
Expand Down
Loading

0 comments on commit c1c728e

Please sign in to comment.