Skip to content

Commit

Permalink
ImageTransform::getConfig()
Browse files Browse the repository at this point in the history
Fixes #12879
  • Loading branch information
brandonkelly committed Mar 13, 2023
1 parent 15ae781 commit 25b08ee
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Fixed a bug where it wasn’t always possible to access entry or category edit pages if the `slugWordSeparator` config setting was set to `/`. ([#12871](https://github.com/craftcms/cms/issues/12871))
- Fixed a bug where multi-value field inputs would be considered modified even if they weren’t, if the field type’s `isValueEmpty()` method returned `true`. ([#12858](https://github.com/craftcms/cms/issues/12858))
- Fixed a bug where asset thumbnails within secondary slideout tabs weren’t loading immediately. ([#12859](https://github.com/craftcms/cms/issues/12859))
- Fixed a bug where rebuilding the project config would lose track of image transforms’ `fill` and `upscale` settings. ([#12879](https://github.com/craftcms/cms/issues/12879))
- Added `craft\models\ImageTransform::getConfig()`.

## 4.4.1 - 2023-03-09

Expand Down
41 changes: 41 additions & 0 deletions src/models/ImageTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ class ImageTransform extends Model
*/
protected string $transformer = self::DEFAULT_TRANSFORMER;

/**
* @inheritdoc
*/
public function __construct($config = [])
{
if (isset($config['width']) && !$config['width']) {
unset($config['width']);
}
if (isset($config['height']) && !$config['height']) {
unset($config['height']);
}
if (isset($config['quality']) && !$config['quality']) {
unset($config['quality']);
}

parent::__construct($config);
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -277,4 +295,27 @@ public function setTransformer(string $transformer): void

$this->transformer = $transformer;
}

/**
* Returns the transform’s config.
*
* @return array
* @since 4.4.2
*/
public function getConfig(): array
{
return [
'fill' => $this->fill,
'format' => $this->format,
'handle' => $this->handle,
'height' => $this->height,
'interlace' => $this->interlace,
'mode' => $this->mode,
'name' => $this->name,
'position' => $this->position,
'quality' => $this->quality,
'upscale' => $this->upscale,
'width' => $this->width,
];
}
}
17 changes: 1 addition & 16 deletions src/services/ImageTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,8 @@ public function saveTransform(ImageTransform $transform, bool $runValidation = t
}

$projectConfig = Craft::$app->getProjectConfig();

$configData = [
'format' => $transform->format,
'handle' => $transform->handle,
'height' => (int)$transform->height ?: null,
'interlace' => $transform->interlace,
'mode' => $transform->mode,
'name' => $transform->name,
'position' => $transform->position,
'quality' => (int)$transform->quality ?: null,
'width' => (int)$transform->width ?: null,
'fill' => $transform->fill,
'upscale' => $transform->upscale,
];

$configPath = ProjectConfig::PATH_IMAGE_TRANSFORMS . '.' . $transform->uid;
$projectConfig->set($configPath, $configData, "Saving transform “{$transform->handle}");
$projectConfig->set($configPath, $transform->getConfig(), "Saving transform “{$transform->handle}");

if ($isNewTransform) {
$transform->id = Db::idByUid(Table::IMAGETRANSFORMS, $transform->uid, $this->db);
Expand Down
28 changes: 4 additions & 24 deletions src/services/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2064,31 +2064,11 @@ private function _getPluginData(array $currentPluginData): array
*/
private function _getTransformData(): array
{
$transformRows = (new Query())
->select([
'name',
'handle',
'mode',
'position',
'width',
'height',
'format',
'quality',
'interlace',
'uid',
])
->from([Table::IMAGETRANSFORMS])
->indexBy('uid')
->all();

foreach ($transformRows as &$row) {
unset($row['uid']);
$row['width'] = (int)$row['width'] ?: null;
$row['height'] = (int)$row['height'] ?: null;
$row['quality'] = (int)$row['quality'] ?: null;
$data = [];
foreach (Craft::$app->getImageTransforms()->getAllTransforms() as $transform) {
$data[$transform->uid] = $transform->getConfig();
}

return $transformRows;
return $data;
}

/**
Expand Down

0 comments on commit 25b08ee

Please sign in to comment.