[Tex/LaTex] How to check that image has valid size before `\includegraphics` is called

graphics

Related to this question Dimension too large. error from pdflatex when using \includegraphics

I run large computation in Mathematica, which generates thousands of image file in pdf. Few of them are too large to include in Latex and my build can fail in the middle.

Is there a way to check, in Latex, using the image file name, that its size and dimensions \maxdimen will not cause \includegraphics to fail? This way I can add a conditional in Latex and avoid loading these image files. Here is a MWE

\documentclass[10pt,notitlepage]{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{image}
\end{document}

The above will fail on this file since the file size is too large.

What Latex code do I need to add to check the image size is valid before calling \includegraphics?

TL 2015, Linux

Update:

Thanks to cfr comment, using pdfinfo -box image.pdf gives

>pdfinfo -box image.pdf
Creator:        Wolfram Mathematica 10.1.0.0 for Microsoft Windows (64-bit) (March 23, 2015) Student Edition - Personal Use Only
Producer:       
CreationDate:   Tue Jul 21 13:15:48 2015
ModDate:        Tue Jul 21 13:15:48 2015
Tagged:         no
Form:           none
Pages:          1
Encrypted:      no
Page size:      504 x 18988 pts
Page rot:       0
MediaBox:           0.00     0.00   504.00 18988.00
CropBox:            0.00     0.00   504.00 18988.00
BleedBox:           0.00     0.00   504.00 18988.00
TrimBox:            0.00     0.00   504.00 18988.00
ArtBox:             0.00     0.00   504.00 18988.00
File size:      337034 bytes
Optimized:      no
PDF version:    1.5
>

I need to figure how to do this inside the Latex document and get the dimension to check against \maxdimen and add Latex conditional to avoid loading this particular image. This has to be done in Latex, since the Latex files are also generated automatically and the image files as well, all by running scripts. So this is all have to be automated.

It will be nice if Latex provides a macro/package that will obtain the image dimensions before loading.

Best Answer

It seems rather pointless to include an image like this but anyway tex can allow boxes bigger than \maxdimen so long as you never do any arithmetic on the lengths:

enter image description here

\documentclass[10pt,notitlepage]{article}
\usepackage{graphicx}

\begin{document}


\centering
\mbox{\pdfimage height \textheight{image.pdf}}

\end{document}