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

[5.x] Fix handle dimensions of rotated videos #11479

Merged
merged 3 commits into from
Feb 21, 2025
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
13 changes: 11 additions & 2 deletions src/Assets/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ private function getVideoAttributes()
{
$id3 = ExtractInfo::fromAsset($this->asset);

$width = Arr::get($id3, 'video.resolution_x');
$height = Arr::get($id3, 'video.resolution_y');
$rotate = Arr::get($id3, 'video.rotate', 0);

// Adjust width and height if the video is rotated
if (in_array($rotate, [90, 270, -90, -270])) {
[$width, $height] = [$height, $width];
}

return [
'width' => Arr::get($id3, 'video.resolution_x'),
'height' => Arr::get($id3, 'video.resolution_y'),
'width' => $width,
'height' => $height,
'duration' => Arr::get($id3, 'playtime_seconds'),
];
}
Expand Down
25 changes: 20 additions & 5 deletions tests/Assets/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Assets\Asset;
use Statamic\Assets\Attributes;
Expand Down Expand Up @@ -78,23 +79,37 @@ public function it_gets_the_attributes_of_audio_file()
}

#[Test]
public function it_gets_the_attributes_of_video_file()
#[DataProvider('videoProvider')]
public function it_gets_the_attributes_of_video_file($playtimeSeconds, $resolutionX, $resolutionY, $rotate, $expected)
{
$asset = (new Asset)
->container(AssetContainer::make('test-container')->disk('test'))
->path('path/to/asset.mp4');

ExtractInfo::shouldReceive('fromAsset')->with($asset)->andReturn([
'playtime_seconds' => 13,
'playtime_seconds' => $playtimeSeconds,
'video' => [
'resolution_x' => 1920,
'resolution_y' => 1080,
'resolution_x' => $resolutionX,
'resolution_y' => $resolutionY,
'rotate' => $rotate,
],
]);

$attributes = $this->attributes->asset($asset);

$this->assertEquals(['duration' => 13, 'width' => 1920, 'height' => 1080], $attributes->get());
$this->assertEquals($expected, $attributes->get());
}

public static function videoProvider()
{
return [
'not rotated' => [13, 1920, 1080, null, ['duration' => 13, 'width' => 1920, 'height' => 1080]],
'rotated 90' => [13, 1920, 1080, 90, ['duration' => 13, 'width' => 1080, 'height' => 1920]],
'rotated -90' => [13, 1920, 1080, -90, ['duration' => 13, 'width' => 1080, 'height' => 1920]],
'rotated 270' => [13, 1920, 1080, 270, ['duration' => 13, 'width' => 1080, 'height' => 1920]],
'rotated -270' => [13, 1920, 1080, -270, ['duration' => 13, 'width' => 1080, 'height' => 1920]],
'rotated 180' => [13, 1920, 1080, 180, ['duration' => 13, 'width' => 1920, 'height' => 1080]],
];
}

#[Test]
Expand Down
Loading