Skip to main content
Participant
July 6, 2009
Question

CFimage and JPG

  • July 6, 2009
  • 1 reply
  • 2445 views

I've run into an interesting situation with cfimage and jpgs.

Our site only allows jpgs to be uploaded.  So it seems clients are renaming image files such as gif, png, etc and renaming them jpg.

They are not converting the image properly, just renaming and uploading these images.

There is an error when the image is used by cfimage.

"ColdFusion was unable to create an image from the specified source file.
Ensure that the file is a vaild image file."

Has anyone ran into this error and is there a workaround or fix for this?  cfimage seems to be a little touchy with proper files.

We are running CF 8.0.1. w/Apache & Linux

    This topic has been closed for replies.

    1 reply

    Inspiring
    July 6, 2009

    My suspicion is that cfimage seems to check the file extension first, before the checking the file headers. If the extension is known, then it uses that information to determine the image type. Otherwise it checks the file headers.

    There may be better methods, but one option is to use a different method to read in the image. Then use cfimage to convert it to jpg.

    <cfscript>
        // this file is really a gif, not a jpeg
        pathToImage = ExpandPath("someGifRenamedAsJpeg.jpg");
        imageFile = createObject("java", "java.io.File").init(pathToImage);
        // read the image into a BufferedImage
        ImageIO = createObject("java", "javax.imageio.ImageIO");
        bi = ImageIO.read(imageFile);
        img = ImageNew(bi);
    </cfscript>

    <!---- Next use cfimage to convert it .... --->

    Another is to write your own routine to check the image headers

    http://www.exampledepot.com/egs/javax.imageio/DiscType.html