[Tex/LaTex] How to get the size (width, height) of an image

graphicspositioning

I want to insert an image at the bottom of a page, using the full \paperwidth. I found the textblock environment from the textpos package and defined a command in this way:

\newcommand*{\grafikunten}[1]{
    \begin{textblock*}{\paperwidth}(0mm, 80mm)
        \includegraphics[width=\paperwidth, keepaspectratio]{#1}
    \end{textblock*}
}

This is working so far, assuming that the vertical starting position of the picture is always "80mm", which of course is not. It depends on the height of the image.

How can I get the height of the image to calculate the exact vertical position?

Or is there another way to position images at the bottom of a page, or at every other side, even in a corner?

Best Answer

Environment textblock has also an optional parameter that specifies the anchor point of the text block: [0,1] means, the left bottom corner. Then you do not need to know the height of the image:

\documentclass{article}
\usepackage[a6paper,hmargin=10mm,vmargin={10mm,20mm}]{geometry}
\usepackage[absolute]{textpos}
\usepackage{graphicx}
\usepackage{lipsum}

\newcommand*{\grafikunten}[1]{
    \begin{textblock*}{\paperwidth}[0,1](0mm, \paperheight)
        \noindent
        \includegraphics[width=\paperwidth, keepaspectratio]{#1}
    \end{textblock*}
}

\begin{document}
\grafikunten{redline}
\lipsum[1-2]
\end{document}

Result

Measurements

In this case the height of the image does not need to be known. In other circumstances it can be useful to get the height.

The height of the image can be measured by \settoheight:

\newdimen\imageheight % goes into the preamble

\settoheight{\imageheight}{%
  \includegraphics[width=\paperwidth,keepaspectratio]{myimage}%
}

Or the image can be put into a box:

\newsavebox\imagebox % goes into the preamble

\sbox{\imagebox}{%
  \includegraphics[width=\paperwidth,keepaspectratio]{myimage}%
}%

The height, depth, and width of the box are available via: \ht\imagebox, \dp\imagebox or \wd\imagebox. (LaTeX purists can use package settobox instead.)