Skip to content

Commit

Permalink
[5.x] Fix handle dimensions of rotated videos (#11479)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
grischaerbe and jasonvarga authored Feb 21, 2025
1 parent 9142ed6 commit d6ab488
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
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

0 comments on commit d6ab488

Please sign in to comment.