A few people asked for this to play with, so here goes..

/*
 * Thumbnail.java (requires Java 1.2+)
 */

import com.allaire.cfx.*;
import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class Thumbnail implements CustomTag{
   public void processRequest( Request request, Response response )
throws Exception
   {
                if ( !request.attributeExists("Infile") ||
!request.attributeExists("Outfile") || !request.attributeExists("Width")
|| !request.attributeExists("Height") ||
!request.attributeExists("Quality") )
                {
                   throw new Exception("Missing attribute ('infile',
'outfile', 'width', 'height' and quality are all required attributes for
this tag)");
                }  
  
    String Infile = request.getAttribute("Infile"); 
    String Outfile = request.getAttribute("Outfile");

    Image image = Toolkit.getDefaultToolkit().getImage(Infile);
    MediaTracker mediaTracker = new MediaTracker(new Frame());
    mediaTracker.addImage(image, 0);
    mediaTracker.waitForID(0);
        
    // determine thumbnail size from WIDTH and HEIGHT
    int thumbWidth = Integer.parseInt(request.getAttribute("Width"));
    int thumbHeight = Integer.parseInt(request.getAttribute("Height"));
    double thumbRatio = (double)thumbWidth / (double)thumbHeight;
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double)imageWidth / (double)imageHeight;
    if (thumbRatio < imageRatio) {
      thumbHeight = (int)(thumbWidth / imageRatio);
    } else {
      thumbWidth = (int)(thumbHeight * imageRatio);
    }
        
    // draw original image to thumbnail image object and
    // scale it to the new size on-the-fly
        
    BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
        
    // save thumbnail image to OUTFILE
    BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(Outfile));
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(thumbImage);
    int quality = Integer.parseInt(request.getAttribute("Quality"));
    quality = Math.max(0, Math.min(quality, 100));
    param.setQuality((float)quality / 100.0f, false);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(thumbImage);
        out.close();
        
        response.setVariable( "Width", Integer.toString(thumbWidth));
        response.setVariable( "Height", Integer.toString(thumbHeight));

        }
}


I didn't write the original java, just converted it to run as a cfx and
pass out the new width/height, as I said previously, it is fast even
when compared to cfx_image but only does jpg's.

Have fun, 

Craig.


-----Original Message-----
From: Craig Dudley 
Sent: 03 December 2002 16:38
To: CF-Talk
Subject: RE: CFX_Image - Quality of images not good when rotating

I'd stick to cfx_image then, it works well, can read and convert many,
many formats. It's fast enough as well, as long as you don't run it on
the front end of your site.

However, don't be scared of java, I'm still learning it myself but I can
already do some nice things that really augment my cf code well, extra
speed is always nice after all.

Craig.

-----Original Message-----
From: Rick Faircloth [mailto:[EMAIL PROTECTED]] 
Sent: 03 December 2002 16:32
To: CF-Talk
Subject: RE: CFX_Image - Quality of images not good when rotating

Hi, Craig.

I'm a complete java novice.  I've changed java applet parameters before,
but that's as much java or javascript as I've used!
I've never compiled anything, either!  So I wouldn't know what to do
with
that!

All I use are jpg's, so that would work for me, but a client uploading
a file to the site could use any format, so I guess it would have to be
more flexible, unless I made jpg's a requirement for clients.  But then,
some of them might have another format and wouldn't know how to convert
it!

Rick


-----Original Message-----
From: Craig Dudley [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 03, 2002 11:00 AM
To: CF-Talk
Subject: RE: CFX_Image - Quality of images not good when rotating


I have a java cfx that does this, but so far it only works with jpg's.
It's very fast though.

I'll gladly post the code on here should you want it. I think if you're
going to use java tags in cf you should at least learn how to compile it
yourself, you may understand how the tag works better that way. So I
wont be sending anyone the actual cfx.

Craig.

-----Original Message-----
From: Rick Faircloth [mailto:[EMAIL PROTECTED]]
Sent: 03 December 2002 15:52
To: CF-Talk
Subject: RE: CFX_Image - Quality of images not good when rotating

Hi, Kay.

The cfx_pwimageproc tag looks very interesting.

I've reviewed it's description on your site.
So the tag, when resizing an image, actually creates
a separate file, "outfile", when executed, right?
And the smaller, thumbnail, is actually a new, smaller size (disk space
size)
than the original.

If so, then this would be a good solution for creating
thumbnail images of larger images, also.

I'm looking for a solution that will allow me to create thumbnails on
the
fly
for things like employee directories and church directories which users
can upload images into themselves.  I'd like to setup the site so that
the users, who might not know how to use a graphics program to resize
photos,
could just upload a single photo, and let the site create thumbnails as
needed.

Am I understanding the tag correctly?

Thanks,

Rick

Rick Faircloth
Prism Productions


-----Original Message-----
From: Kay Smoljak [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 03, 2002 10:12 AM
To: CF-Talk
Subject: Re: CFX_Image - Quality of images not good when rotating


Mark Smeets <[EMAIL PROTECTED]> wrote:
> Has anyone experienced this when rotating images using this tag? The
moment
> I rotate an image, the quality is degraded. I checked through the IML
readme
> file that the tag comes with and after adding the quality command,
there's
> been no improvement.

Are you having the problem with 16 or 24 bit colour images? If you're
using Jukka Manner's original version, it was designed for GIFs and uses
an internal 256 bit palette, which is why JPEGs don't look so good.
Here's a plug... We wrote an alternative for use on our own projects -
cfx_pwimageproc - and we also sell it for US $99 at
http://developer.perthweb.com.au. There's a demo you can try to see if
the quality suits your purpose.

HTH,
Kay.

Kay Smoljak
----------------
http://kay.smoljak.com






~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
This list and all House of Fusion resources hosted by CFHosting.com. The place for 
dependable ColdFusion Hosting.

Reply via email to