How to scale and then trim an image?
Here a \PrintImage
macro that clips the bottom of the included image if its height is too large.
\documentclass{article}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\newlength{\oH}
\newlength{\oW}
\newlength{\rH}
\newlength{\cH}
\newcommand\PrintImage[3]{% width, height, image
\settototalheight{\oH}{\includegraphics{#3}}%
\settowidth{\oW}{\includegraphics{#3}}%
\setlength{\rH}{\oH * \ratio{#1}{\oW}}
\ifthenelse{\lengthtest{\rH < #2}}{
\includegraphics[width=#1]{#3}%
}{%
\setlength{\cH}{(\rH-#2)*\ratio{\oW}{#1}}%
\includegraphics[width=#1,clip,trim=0 \cH{} 0 0]{#3}%
}%
}
\begin{document}
\PrintImage{6cm}{2cm}{yourimage}
\end{document}
Here is the generic ClipImage
command, based on PolGab answer:
\documentclass[11pt,article]{memoir}
\usepackage{graphicx}
\usepackage{adjustbox}
\usepackage{calc}
\usepackage{ifthen}
\newlength{\oH}
\newlength{\oW}
\newlength{\rH}
\newlength{\rW}
\newlength{\cH}
\newlength{\cW}
\newcommand\ClipImage[3]{% width, height, image
\settototalheight{\oH}{\includegraphics{#3}}%
\settowidth{\oW}{\includegraphics{#3}}%
\setlength{\rH}{\oH * \ratio{#1}{\oW}}%
\setlength{\rW}{\oW * \ratio{#2}{\oH}}%
\ifthenelse{\lengthtest{\rH < #2}}{%
\setlength{\cW}{(\rW-#1)*\ratio{\oH}{#2}}%
\adjincludegraphics[height=#2,clip,trim=0 0 \cW{} 0]{#3}%
}{%
\setlength{\cH}{(\rH-#2)*\ratio{\oW}{#1}}%
\adjincludegraphics[width=#1,clip,trim=0 \cH{} 0 0]{#3}%
}%
}
\begin{document}
\centering
\ClipImage{0.5in}{3in}{myimage}
\end{document}
Thank you very much again.