Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions config/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@
|--------------------------------------------------------------------------
|
| Configure the aspect ratio presets available in the Control Panel image
| crop editor. Ratios may be provided as "W:H" strings, keyed values, or
| arrays with custom labels and ratio values.
| crop editor. Each entry may be a "W:H" string (e.g. "16:9") or an array
| with a custom label and ratio: ['label' => 'Wide', 'ratio' => '16:9'].
|
*/

Expand Down
9 changes: 5 additions & 4 deletions src/Assets/CropAspectRatios.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Support\Facades\Log;

use function Statamic\trans as __;

class CropAspectRatios
{
/**
Expand Down Expand Up @@ -39,7 +41,6 @@ private static function parse(mixed $entry, mixed $key): ?array
return null;
}

$label = is_string($key) ? $key : (string) $entry;
$value = self::ratioToFloat($entry);

if ($value === null) {
Expand All @@ -49,7 +50,7 @@ private static function parse(mixed $entry, mixed $key): ?array
}

return [
'label' => $label,
'label' => __((string) $entry),
'value' => $value,
];
}
Expand All @@ -60,7 +61,7 @@ private static function parse(mixed $entry, mixed $key): ?array
*/
private static function parseArrayEntry(array $entry, mixed $key): ?array
{
$label = $entry['label'] ?? (is_string($key) ? $key : null);
$label = $entry['label'] ?? null;
$ratio = $entry['ratio'] ?? null;

$labelIsStringable = is_string($label) || is_numeric($label) || $label instanceof \Stringable;
Expand All @@ -80,7 +81,7 @@ private static function parseArrayEntry(array $entry, mixed $key): ?array
}

return [
'label' => (string) $label,
'label' => __((string) $label),
'value' => $value,
];
}
Expand Down
14 changes: 9 additions & 5 deletions tests/Assets/CropAspectRatiosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ public function it_supports_custom_labels_and_fractional_ratio_strings()
}

#[Test]
public function it_supports_keyed_ratio_entries()
public function it_translates_labels()
{
app('translator')->addLines([
'crop.wide' => 'Wide Screen',
], 'en');

config()->set('statamic.assets.crop_aspect_ratios', [
'Portrait' => '9:16',
'A4' => '210:297',
['label' => 'crop.wide', 'ratio' => '16:9'],
['label' => 'Square', 'ratio' => '1:1'],
]);

$this->assertSame([
['label' => 'Portrait', 'value' => 9 / 16],
['label' => 'A4', 'value' => 210 / 297],
['label' => 'Wide Screen', 'value' => 16 / 9],
['label' => 'Square', 'value' => 1.0],
], CropAspectRatios::all());
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Http/View/Composers/JavascriptComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class JavascriptComposerTest extends TestCase
public function it_provides_crop_aspect_ratios_to_script_config()
{
config()->set('statamic.assets.crop_aspect_ratios', [
'Portrait' => '9:16',
'9:16',
['label' => 'US Letter', 'ratio' => '8.5:11'],
]);

Expand All @@ -33,7 +33,7 @@ public function it_provides_crop_aspect_ratios_to_script_config()
$json = Statamic::jsonVariables(request());

$this->assertSame([
['label' => 'Portrait', 'value' => 9 / 16],
['label' => '9:16', 'value' => 9 / 16],
['label' => 'US Letter', 'value' => 8.5 / 11],
], $json['cropAspectRatios']);
}
Expand Down
Loading