[Tex/LaTex] Side-by-side figures with caption, specify height but use whole textwidth

captionsfloatsgraphicsresizescaling

I found this answer which does exactly what I want except the fact that I can't use a caption for my figures:

Forcing subfigures to have same height and take overall X% of linewidth in LaTeX

I want two figures side by side. The problem I usually have is that they are in different sizes and that it takes alot of time to scale them manually to the same height, use the whole text width and still keep the ratio.

I tried to use this command, but it wont work as I get the error "Missing \endgroup inserted".

\resizebox{\textwidth}{!}{%
\begin{figure}[H]
    \includegraphics[height=4cm,fbox]{testfigure_1.jpg}%
    \caption{My figure 1}
\end{figure}
\begin{figure}[H]
    \includegraphics[height=4cm,fbox]{testfigure_2.jpg}%
    \caption{My figure 1}
\end{figure}
}

I have realised that I of some reason cannot put an figure enviroment within resizebox. Still I cannot solve this.

I don't know if this matter to your answers but in my code I use this to center all captions for my figures:
\usepackage[justification=centering]{caption}

Best Answer

Slightly adopted @egreg answer in given link (that now you have two figures and not sub-figures):

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}% added for test in parallel with @egreg solution

\usepackage{showframe}% only for show page layout. in real use it had to be omitted!

% new lengths and save boxes
\newlength{\twosubht}
    \newsavebox{\twosubbox}
\newlength{\firstfig}
    \newsavebox{\firstfigbox}
\newlength{\secondfig}
    \newsavebox{\secondfigbox}

    \begin{document}
\begin{figure}[htp]
% measurement of height
\sbox\twosubbox{\resizebox{\dimexpr.9\textwidth-1em}{!}{%
    \includegraphics[height=3cm]{example-image-a}%
    \includegraphics[height=3cm]{example-image-16x9}%
    }%
}
\setlength{\twosubht}{\ht\twosubbox}
% measurement of width
\sbox\firstfigbox{\includegraphics[height=\twosubht]{example-image-a}}%
\setlength{\firstfig}{\wd\firstfigbox}
\sbox\secondfigbox{\includegraphics[height=\twosubht]{example-image-16x9}}%
\setlength{\secondfig}{\wd\secondfigbox}
% figure
\centering
    \begin{tabular}{p{\firstfig}p{\secondfig}}
\usebox\firstfigbox
\caption{The first}
    &   \usebox\secondfigbox
        \caption{The second}
    \end{tabular}
\end{figure}

% original @egreg solution

\noindent\hrulefill The text width\hrulefill
\begin{center}
\makebox[.9\textwidth]{\hrulefill 90\% of text width\hrulefill}
\end{center}

\begin{figure}[htp]
% preliminary
\sbox\twosubbox{%
  \resizebox{\dimexpr.9\textwidth-1em}{!}{%
    \includegraphics[height=3cm]{example-image-a}%
    \includegraphics[height=3cm]{example-image-16x9}%
  }%
}
\setlength{\twosubht}{\ht\twosubbox}

% typeset

\centering

\subcaptionbox{First\label{f}}{%
  \includegraphics[height=\twosubht]{example-image-a}%
}\quad
\subcaptionbox{Second\label{s}}{%
  \includegraphics[height=\twosubht]{example-image-16x9}%
}

\caption{The caption}
\end{figure}
    \end{document}

First compiling is slow due to figure size adjustment and figure size measurements.

enter image description here

Edit: For comparison the size of the figures, I copy @egreg solution from given link into my answer. As expected, the size of figures are the same (this is response to comment, that figures are not the same ...).

I also recommended to use geometry package for determining page layout (no used in above MWE).

enter image description here