Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getPresetsByRatio #7

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/Responsive.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,11 @@ public static function getPresets(Asset $image): array

protected static function getPresetsByRatio(Asset $image, array $config): array
{
$ratio = $image->width() / $image->height();
$presets = collect($config);

// filter config based on aspect ratio
// if ratio < 1 get all presets with a height bigger than the width, else get all presets with width equal or bigger than the height.
if ($ratio < 0.999) {
$presets = $presets->filter(fn ($preset, $key) => $preset['h'] > $preset['w']);
if ($presets->isNotEmpty() && isset($config['placeholder'])) {
$presets->prepend($config['placeholder'], 'placeholder');
}
} else {
$presets = $presets->filter(fn ($preset, $key) => $key === 'placeholder' || $preset['w'] >= $preset['h']);
}
$vertical = $image->height() > $image->width();
$presets = $presets->filter(fn ($preset, $key) => $key === 'placeholder' || (($preset['h'] > $preset['w']) === $vertical));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also write this check out fully, like:

$presets = $presets->filter(function ($preset, $key) use ($vertical) {
    if ($key === 'placeholder') {
        return true;
    }
    $presetVertical = $preset['h'] > $preset['w']);
    if ($presetVertical === $vertical) {
         return true;
    }
    return false;
});

Not sure if that's nicer.


return $presets->isNotEmpty() ? $presets->toArray() : $config;
}
Expand Down