msaad's avatar
Level 1

Laravel Size Control By Url Parameters

Hello Friends, I just want to ask a feature:

Image Url: http://127.0.0.1:8000/storage/image/61633152593.webp?w=160&h=160&fit=crop&auto=format

Can i control size of image by URL parameter w & h in Laravel?

For Example:

http://127.0.0.1:8000/storage/image/61633152593.webp?w=250&h=250&fit=crop&auto=format

Or how it's work || how can i do this in laravel. Thanks

0 likes
7 replies
bugsysha's avatar

What are you trying to do exactly here? What are you building?

jlrdw's avatar

@msaad You can use direct like: Say $w = width (50).

<img width="<?= $w; ?>"... rest of tag information

It's just a variable.

See if tailwind has dynamic resizing also.

bugsysha's avatar

@msaad I would advise that you use the suggested Intervention package and for each image generate a couple of smaller ones in generic resolutions and then use those to display them and size them with the width attribute or CSS.

Snapey's avatar

if you allow any size then you would need to have a route that resized each image on the fly and delivered it to the client. This would be very slow.

Since it's your code that's calling for the different sized images then you would be best to select a few specific sizes and generate each of these when the image is first uploaded. Spatie media library makes this easy

https://spatie.be/docs/laravel-medialibrary/v9/introduction

Don't be distracted by media library Pro - you aren't going to need that

Please or to participate in this conversation.