I don't think that there is any other way.
The problem is that you need to perform the whole process of loading and decoding to be sure that the image file is valid, even sanity checks are not enough because you are using JPEG compressed images. In such images a wrong byte can be detected only during decompression.
The only suggestion I can give you is to make two fast sanity check before trying to load the image:
1. check file dimension, if it is shorter than a reasonable length you can discard it
2. Read the first 2 bytes of the image file, they contain a JPEG SOI marker = 0xFFD8
3. Then a JFIF-APP0 marker segment:
Field Size (bytes) Description
APP0 marker 2 FF E0
Length 2 Length of segment excluding APP0 marker
Identifier 5 4A 46 49 46 00 = "JFIF" in ASCII, terminated by a null byte
JFIF version 2 First byte for major version, second byte for minor version (01 02 for 1.02)
Density units 1 Units for the following pixel density fields
00 : No units; width:height pixel aspect ratio = Xdensity:Ydensity
01 : Pixels per inch (2.54 cm)
02 : Pixels per centimeter
Xdensity 2 Horizontal pixel density. Must not be zero.
Ydensity 2 Vertical pixel density. Must not be zero.
Xthumbnail 1 Horizontal pixel count of the following embedded RGB thumbnail. May be zero.
Ythumbnail 1 Vertical pixel count of the following embedded RGB thumbnail. May be zero.
Thumbnail data 3 × n Uncompressed 24 bit RGB (8 bits per color channel) raster thumbnail data in the order R0, G0, B0, ... Rn, Gn, Bn; with n = Xthumbnail × Ythumbnail.
Also here you can make a check for reasonable values.
For more detail look
here[
^].