I write a command to wrap text around tikz picture automatically (Image 1).
I want the linewidth can be expand when the text is higher than the image height (Image 2).
Please help correct below code.
\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\newdimen\widthimmini
\newdimen\heightimmini
\newdimen\textimagewidth
\newbox\imbox
\newcommand{\putimage}[3][-12pt]{
\setbox\imbox=\vbox{\hbox{#3}}
\widthimmini=\wd\imbox
\heightimmini=\ht\imbox
\textimagewidth=\dimexpr\linewidth - 1.075\widthimmini\relax
\par\vspace*{\dimexpr#1\relax}\noindent{\ignorespaces\begin{minipage}[t]{\textimagewidth}
\vspace*{0pt}\ignorespaces
\begingroup
#2
\endgroup
\end{minipage}
\begin{minipage}[t]{\widthimmini}
\vspace*{-1pt}
\begin{center}
\begingroup
#3
\endgroup
\end{center}
\end{minipage}}}
\begin{document}
\putimage{ \lipsum[30]
}
{\begin{tikzpicture}
\draw (0,0) circle(2);
\end{tikzpicture}}
\newpage
\putimage{ \lipsum[10]
}
{\begin{tikzpicture}
\draw (0,0) circle(2);
\end{tikzpicture}}
\noindent
\lipsum[5]
\end{document}