Skip to content

Commit

Permalink
Merge pull request #504 from pinkary-project/fix/image-ratio
Browse files Browse the repository at this point in the history
Fix: Add addition validation rules for max size & ratio
  • Loading branch information
nunomaduro authored Aug 18, 2024
2 parents 07f2af6 + cbd8b4d commit ecaa0d2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
23 changes: 22 additions & 1 deletion app/Livewire/Questions/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\User;
use App\Rules\MaxUploads;
use App\Rules\NoBlankCharacters;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -94,13 +95,33 @@ public function runImageValidation(): void
'images.*' => [
File::image()
->types(['jpeg', 'png', 'gif', 'webp', 'jpg'])
->max($this->maxFileSize),
->max($this->maxFileSize)
->dimensions(
Rule::dimensions()->maxWidth(4000)->maxHeight(4000)
),

static function (string $attribute, mixed $value, Closure $fail): void {
/** @var UploadedFile $value */
$dimensions = $value->dimensions();
if (is_array($dimensions)) {
[$width, $height] = $dimensions;
$aspectRatio = $width / $height;
$maxAspectRatio = 2 / 5;
if ($aspectRatio < $maxAspectRatio) {
$fail('The image aspect ratio must be less than 2/5.');
}
} else {
$fail('The image aspect ratio could not be determined.');
}
},

],
],
messages: [
'images.*.image' => 'The file must be an image.',
'images.*.mimes' => 'The image must be a file of type: :values.',
'images.*.max' => 'The image may not be greater than :max kilobytes.',
'images.*.dimensions' => 'The image must be less than :max_width x :max_height pixels.',
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static function (array $match) use ($content): string {

$url = $disk->url($match[2]);

return "<img class='object-cover mx-auto max-h-[52rem] w-full max-w-[26rem] rounded-lg' src=\"{$url}\" alt=\"image\">";
return "<img class='object-contain mx-auto max-h-[52rem] w-full max-w-[26rem] rounded-lg' src=\"{$url}\" alt=\"image\">";
},
$content
);
Expand Down
4 changes: 1 addition & 3 deletions resources/views/components/image-lightbox.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!-- Lightbox Modal -->
<x-modal name="image-lightbox" close-button-outside-modal should-center-modal-content>
<div x-data="lightBox" class="relative md:flex md:items-center">
<div class="overflow-y-auto max-h-[85vh]">
<img :src="imgSrc" alt="image" class="max-w-full rounded-lg"/>
</div>
<img :src="imgSrc" alt="image" class="object-contain max-h-[85vh] rounded-lg"/>
<button x-show="shouldShowPrevButton" class="absolute left-0 md:-ml-8 text-white cursor-pointer text-2xl" @click="prevImage">&larr;</button>
<button x-show="shouldShowNextButton" class="absolute right-0 md:-mr-8 text-white cursor-pointer text-2xl" @click="nextImage">&rarr;</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<img class='object-cover mx-auto max-h-[52rem] w-full max-w-[26rem] rounded-lg' src="/storage/images/pathtoimage.png" alt="image">
<img class='object-contain mx-auto max-h-[52rem] w-full max-w-[26rem] rounded-lg' src="/storage/images/pathtoimage.png" alt="image">
28 changes: 28 additions & 0 deletions tests/Unit/Livewire/Questions/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,31 @@
'images.0' => "The image may not be greater than {$maxFileSize} kilobytes.",
]);
});

test('max size & ratio validation', function () {
$user = User::factory()->create([
'is_verified' => true,
]);

$component = Livewire::actingAs($user)->test(Create::class, [
'toId' => $user->id,
]);

$component->set('images', [
UploadedFile::fake()->image('test.jpg', '4005', '4005'),
]);
$component->call('runImageValidation');

$component->assertHasErrors([
'images.0' => 'The image must be less than 4000 x 4000 pixels.',
]);

$component->set('images', [
UploadedFile::fake()->image('test.jpg', '429', '1100'),
]);
$component->call('runImageValidation');

$component->assertHasErrors([
'images.0' => 'The image aspect ratio must be less than 2/5.',
]);
});

0 comments on commit ecaa0d2

Please sign in to comment.