Crop an image to exact measurements

I know that with \includegraphics there is the option to trim images, but this is only practical for images I know the size of, so I can trim them the way it fits perfectly for the given image.

Assuming I want to add a random image to my document (I am using XeLaTex). I want to resize it to exact measurements - say 250x300px - without knowing the actual size of the image (but assuming it is at least as big as these measurements).

Is there a package for an automatical cropping of images like described above?

EDIT: For further visualization, I will add a little sketch of what I aim to achieve.

Assume I have a document in which I want to place an image with specific height AND width (my placeholder). Now I want to add the image with the palm trees that obviously has a size not fitting to the placeholder.

What I want is the image to be cropped somewhere (not necessarily beautifully) to fit the given size:

In reality, the given images will be actually of a similar size as the placeholder, so a "random" cropping wouldn't be that ugly. But it is necessary for my design's sake that every picture fulfils the given height and width properties exactly.

• Is there a reason you don't want to just shrink a large image? I don't like to crop images at "random" places, only where I know the image subject won't be compromised. The graphicx package provides quite a few methods for resizing. – barbara beeton Jun 2 '20 at 22:53
• I also do not understand the question. You do not want \includegraphics[width=...,height=..]{graphics}. Is that correct? If so, why, i.e. what is wrong with this option? – user194703 Jun 2 '20 at 23:00
• @barbara beeton In my case, I actually want to "crop images at 'random' places", because I am currently writing a class file where the user should be able to drop any picture (preferably in a similar size), so any excess should be cut away automatically. When using [width=..., height=...] the image isn't cropped but just resized. I instead want it to be actually cropped to these given width and height. – TiMauzi Jun 2 '20 at 23:38
• @barbarabeeton I added an sketch of what I want to achieve and why! Hope this will explain my question better. – TiMauzi Jun 2 '20 at 23:57

\picdims[...]{<x-dim>}{<ydim>}{<image-file>} is provided, where the user can specify the x and y dimensions of the cropped image. Cropping is done symmetrically.

First, I store a copy of the uncropped image in the temporary \box0. This allows me to establish its total width \wd0 and height \ht0. The desired x,y image dimensions are provided by the user in #2 and #3. Therefore, a total of \wd0-#2 must be horizontally clipped and \ht0-#3 must be vertically clipped to achieve the final clipped dimension. I cut these dimensions in half via .5\dimexpr...\relax and apply those clipping amounts symmetrically to the left/right and the top/bottom respectively. This clipping is done via \clipbox in which the <left bottom right top> clipping amounts are provided as a space-separated list in the first argument to the \clipbox.

\documentclass{article}
\usepackage{graphicx,trimclip}
\newcommand\picdims[4][]{%
\setbox0=\hbox{\includegraphics[#1]{#4}}%
\clipbox{.5\dimexpr\wd0-#2\relax{} %
.5\dimexpr\ht0-#3\relax{} %
.5\dimexpr\wd0-#2\relax{} %
.5\dimexpr\ht0-#3\relax}{\includegraphics[#1]{#4}}}
\parskip 1ex
\begin{document}
\includegraphics[width=2in]{example-image}\par
\picdims[width=2in]{1in}{1in}{example-image}\par
\picdims[width=2in]{1.5in}{1in}{example-image}\par
\picdims[width=2in]{1in}{.5in}{example-image}\par
\end{document}


• This worked perfectly. Would you like to explain a bit of what exactly you did there? Especially in the \clipbox part? – TiMauzi Jun 3 '20 at 22:40
• @TiMauzi I have added some explanation. – Steven B. Segletes Jun 4 '20 at 2:33

As an alternative to the already mentioned way, I found out a similar approach which only uses graphicx without trimclip. It also starts off with a \box0 but uses the trim option of includegraphics. The rest is similar to Steven B. Segletes' solution before:

\usepackage{graphicx}

\newcommand\picdims[4][]{%
\setbox0=\hbox{\includegraphics[#1]{#4}}%
\includegraphics[trim={.5\dimexpr\wd0-#2\relax{},
.5\dimexpr\ht0-#3\relax{},
.5\dimexpr\wd0-#2\relax{},
.5\dimexpr\ht0-#3\relax{}},
clip,#1]{#4}%
}