Skip to content

Instantly share code, notes, and snippets.

@vikaskanani
Created August 17, 2013 09:27
Show Gist options
  • Save vikaskanani/6256084 to your computer and use it in GitHub Desktop.
Save vikaskanani/6256084 to your computer and use it in GitHub Desktop.
ColdFusion Image reading function
<cfscript>
public function readImage(fullpath){
try {
return ImageNew(fullpath);
} catch (any e) {
//trying java imageio
var imageFile = createObject("java", "java.io.File").init(fullpath);
// read the image into a BufferedImage
var ImageIO = createObject("java", "javax.imageio.ImageIO");
try {
var bi = ImageIO.read(imageFile);
return ImageNew(bi);
} catch(any e) {
//try for bad formatted images
//create java file object, passing in path to image
var imageFile = createObject("java","java.io.File").init(fullpath);
//create a FileSeekableStream, passing in the image file we created
var fss = createObject("java","com.sun.media.jai.codec.FileSeekableStream").init(imageFile);
//create ParameterBlock object and initialize it (call constructor)
var pb = createObject("java","java.awt.image.renderable.ParameterBlock").init();
//create JAI object that will ultimately do the magic we need
var JAI = createObject("java","javax.media.jai.JAI");
try {
//pass in FileSeekableStream
pb.add(fss);
//use the JAI object to create a buffered jpeg image using the parameter block we just created
var buffImage = local.JAI.create("jpeg", pb).getAsBufferedImage();
//pass the buffered image to the ColdFusion imagenew() method.
var New_Image = imagenew(buffImage);
//make sure we close the stream, or you'll pay for it later
fss.close();
return New_Image;
} catch (any e) {
if (isDefined("fss")) {
fss.close();
}
rethrow;
}
}
}
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment