Skip to main content

ColdFusion Invalid Image Format Solution

For those who have gotten the following error:
"java.awt.color.CMMException: Invalid image format" and tried
the solutions posted here with no avail.

We are going to use the power of Java's JAI (Java Advanced Imaging) library to tackle this one.

<cfscript>
//path to image
imagePath = "pathToImage";

//create java file object, passing in path to image
imageFile = createObject("java","java.io.File").init(imagePath);

//create a FileSeekableStream, passing in the image file we created
fss = createObject("java","com.sun.media.jai.codec.FileSeekableStream").init(imageFile);

//create ParameterBlock object and initialize it (call constructor)
pb = createObject("java","java.awt.image.renderable.ParameterBlock").init();

//pass in FileSeekableStream
pb.add(fss);

//create JAI object that will ultimately do the magic we need
JAI = createObject("java","javax.media.jai.JAI");

//use the JAI object to create a buffered jpeg image using the parameter block we just created
buffImage = JAI.create("jpeg", pb).getAsBufferedImage();

//pass the buffered image to the ColdFusion imagenew() method.
New_Image = imagenew(buffImage);

//make sure we close the stream, or you'll pay for it later
fss.close();
</cfscript>

From what I could find CF/Java chokes on some jpeg's due to their format.
Specifically speaking, the EXIF format vs. the JFIF format.
Read this for more information on these two standards.

Some cameras only add the EXIF segment to the file header of the jpeg and images from those cameras are the ones I see issues with.
To see some samples of this, check out the images on this website.
Download some of those images and see if your image processing code can handle them!

To get more information on your images check out this page and it will tell you all about it, including if the image has JFIF or EXIF segments added.

If anyone has questions about the above code, please don't hesitate to ask.

Hope this helps some of you, since I could not find any solution out there.

Comments

  1. We use imagemagik -strip http://www.imagemagick.org/script/command-line-options.php?#strip

    ReplyDelete
    Replies
    1. Thanks Henry, I have heard of others using this.
      Do you have more information on how this can be used as another option when processing images with ColdFusion?
      Also, does this work on both Windows and Linux?

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. thanks man!
    you saved me :D
    best regards from brazil

    ReplyDelete
  4. I wanted to add that it's important to use ImageNew() vs the cfimage tag.

    The cfimage tag will not accept a buffered file object as an argument for the "source" attribute (you will get an "The cfimage tag accepts only those ColdFusion variables that contain Base64 strings, BLOBs, Byte arrays or other images as inputs." error).

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Wanted to edit my previous comment. Thanks for positing this, helped me out today. Have you noticed any performance issues using this?

    ReplyDelete
  7. I added try/catch code around the initial CF imageNew() and will execute the JAI stuff on exception. CPU use was really high when running JAI, so I will only fire that code when needed. Thanks again!

    ReplyDelete

Post a Comment

Popular posts from this blog

Dyson AM09 Fan & Heater H2 Error

No idea what the actual error is and I couldn't find anything useful on the web, so hopefully this will help someone else. I assumed that the H2 error meant that something was dirty, clogged up or that it was overheating because it was dirty or clogged up because the error only showed up when it was in Heater mode. The heater would run for about 30 seconds, then it would show the error and switch over to the high speed fan , I assume to try to blow out the dust. I proceeded to open it up to give it a deep clean because the Dyson instructions for cleaning this thing are ridiculous and don't help at all. Gently wiping down the outside and vacuuming the intake holes....really Dyson, really?! I used cotton swabs and 91% alcohol to clean everything I could get to, starting at the base (in hindsight, this part may not be necessary at all). Then I got to the top of the device where the actual air comes out of and noticed that there was a lot of build up on the heater coils. I could

Replacing your HDD with a SSD in a Mid-2009 Mac

What you'll need: T6 screwdriver PH000 philips screwdriver SATA to USB connector ( or SATA to Thunderbolt if you feel like spending the cash) Apples DiskUtility Application 2.5 inch SATA SSD (e.g. Samsung 850 EVO), size is up to you Before we swap the drives out we want to test our speeds so that we can compare with what we get afterwards. You can run the below commands a few times, then take an average if you want a better representation. Write Speed (16GB file, lower the values of the bs and count params for a smaller file) 52.872 MB/sec write speed Read Speed 50.6726 MB/sec read speed Also, in the Disk tab of the ActivityMonitor you can see a real-time measurement of reads/writes per second, which where an approximate match to the values I got above. Hook up the new drive The next thing to do is clone the current disk over to the SDD. I used a SATA to USB connector I found online to connect it to the Mac and I used the DiskUtility app to clone the disk. Onc

ColdFusion to Node.js Conversion : Setup Part 1

This multi-part post will be community comment driven, which basically means that in an effort to not spend too much time on details that might not matter to the viewers out there, I am only going to share the details which I think are important at the time of publishing the posts. If people (or bots) comment and want more details, I will add them to the original post. As always, constructive criticism is always welcome. Since I am currently using the CFWheels framework (which is inspired by Ruby on Rails or RoR), I chose to try out the Sails.js framework for Node.js (which is also similar to Rails). 1st thing I did was get Node and Apache working together by adding this to my Apache vhost(VirtualHost) block: ProxyRequests on ProxyPass / http://localhost:8124/ And adding this to my hosts file (/etc/hosts): 127.0.0.1 mynewapp.node www.mynewapp.node This allows me to setup an alias in my host file and reference the URL as you normally would without adding the portnumber o