答案1
Martin Scharrer 的回答略有修改这里应该可以解决问题:
\documentclass{article}
\usepackage{graphicx}
\newlength{\imgwidth}
\newlength{\imgtocenter}
\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{%
\def\imgcmd{%
\settowidth{\imgwidth}{\includegraphics[width=\wd\mysavebox,height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax,#1]{#2}}%
\setlength{\imgtocenter}{\dimexpr(0.5\wd\mysavebox-0.5\imgwidth)\relax}%
\hspace*{\imgtocenter}%
\includegraphics[width=\wd\mysavebox,height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}%
}{%
\end{minipage}%
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{-\dp\mysavebox}{\imgcmd}}\usebox\mysavebox}%
}
\begin{document}
\noindent
\begin{imgminipage}[keepaspectratio]{example-image-a}{0.5\linewidth}
Some text\\
Hello world!
\end{imgminipage}
\noindent
\begin{imgminipage}{example-image-b}{\linewidth}
Some more text\\
Hello world!
\end{imgminipage}
\end{document}