Image thumbnail in page view list - set only max-width NO height

How to set only the max width in order to have a variable height and avoid cropping the image → for example for a list in masonry?

Here is my code, but if I set a height in auto it displays an error.
Thanks for your tips.

<?php if (is_object($thumbnail)): ?>
  <?php
  
  $img = Core::make('html/image', array('f' => $thumbnail));
  $tag = $img->getTag();
  $tag->addClass('img-responsive');

// print $tag;

  $maxHeight = 580;
  $maxWidth = 800;
$im = Core::make('helper/image');
$thumb = $im->getThumbnail(
    $thumbnail,
    $maxWidth,
    $maxHeight,
    true
); //<-- set these 2 numbers to max width and height of thumbnails
$tag = new \HtmlObject\Image();
print $tag->src($thumb->src);
//  print $thumb->src;
  ?>
<?php endif; ?>

:::::::::::::::::::: OR ::::::::::::::::

<?php if (is_object($thumbnail)): ?>
    <?php
    $img = Core::make('html/image', array('f' => $thumbnail));
    $tag = $img->getTag();
    $tag->addClass('img-responsive');
    echo $tag;
    ?>
<?php endif; ?>

But with this last it displays the source image which is not what I want