Resize image and get dimensions


This might be simple, but it’s not very clear to me yet:
I’m resizing an image only by a given width value (set in the config options) to keep its aspect ratio.
I also want to insert the dimensions of the resized image to the data-attribute of the img tag.

Currently I’m doing it like this:

$img_resize = $image->resize(option('imgs.medium')); 

<img src="<?= $img_resize->url() ?>" data-dimension="<?= $img_resize->width() . 'x' . $img_resize->height() ?>">

Is this the most performant way to get the dimensions of the new image or does this code resize the image 3 times?

No, perfectly fine. The resized image is stored in your variable. Then you call the methods on it.

You are making sure you got an image object in the if-statement that I don’t see here, are you?

Ok, great!
Yes everything else is clear, I just reduced the code to the relevant part of my question.
Thank You Sonja! :v:t2:

1 Like