Hi Joshua,

could you please add this to the wiki and attach the file. That would make it much easier for people to find it / check it out.

Also, I can give you an account for Subversion and you can add this to https://svn.rifers.org/rife-contrib/trunk/. This would make it eventually part of the rife-contrib jar.

What do you think?

Best regards,

Geert

On 09 Dec 2006, at 17:02, Joshua Hansen wrote:

Whoops, typo: "'long-edge-width' should be 'long-edge-length'".

Josh
--
Joshua Hansen
Up Bear Enterprises
(541) 760-7685



Joshua Hansen wrote:
Hi all,
Included is a ImageContentTransformer which uses attributes 'short- edge-length' and 'long-edge-length' to scale an image. If the aspect ratio of all of your images is consistent (i.e. 4:3), the scaling factor should also be consistent. Yay! :) This is useful where you want to, say, create thumbnails of all your photos. For example, when uploading a photo and setting the 'long-edge-width' to 1024, both 1600x1200 images and 1200x1600 images will have a scaling factor of .64. If the existing 'width' attribute were used instead, these images would have scaling factors of .64 and .85, respectively -- probably not what you expected. An example of a bean and corresponding meta data is included in the javadoc.
Also, I'm open to alternate class name suggestions...! :)
Shalom (complete peace),
Josh
--------------------------------------------------------------------- ---
package com.upbear.rife.cmf.transform;
import java.awt.Image;
import java.util.Map;
import java.util.logging.Logger;
import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
import com.uwyn.rife.cmf.format.exceptions.FormatException;
import com.uwyn.rife.cmf.transform.ImageContentTransformer;
import com.uwyn.rife.tools.ImageWaiter;
/**
 * Transforms an image.
 * <p>The following attributes are supported:
 * <table>
 * <tr>
 * <td valign="top"><code>long-edge-length</code>
* <td>Changes an image so the longest edge is the indicated size. The short edge * will be scaled appropriately.</td>
 * </tr>
 *  * <tr>
 * <td valign="top"><code>short-edge-length</code>
* <td>Changes an image so the shortest edge is the indicated size. The long edge * will be scaled appropriately.</td>
 * </tr>
 * </table>
 *
 * <p>Example:</p>
 * <pre><code>
 * public class MyBean {
 *      
 *      private int     id = -1;
 *      private byte[]  image = null;
 *  *   public int getId() { return id; }
 *      public void setId(int id) { this.id = id; }
 *  *   public byte[]   getImage() { return image; }
 *      public void     setImage(byte[] image) { this.image = image; }
* public void setImageThumbnail(byte[] imageThumbnail) { } // dummy setter * public byte[] getImageThumbnail() { return image; }
 * }
 *  *   * public class MyBeanMetaData extends MetaData {
* private static final PhotoScalingImageTransformer IMG_TRANSFORMER = new PhotoScalingImageTransformer();
 *      public void activateMetaData()
 *      {
 *              // Store the full-size image
 *              addConstraint(new ConstrainedProperty("image")
 *                              .notNull(true)
 *                              .file(true)
 *                              .mimeType(MimeType.IMAGE_JPEG));
* * // Store the image scaled down to have it's shortest edge be 74 pixels
 *              addConstraint(new ConstrainedProperty("imageThumbnail")
 *                              .notNull(true)
 *                              .file(true)
 *                              .listed(true)
 *                              .position(1)
 *                              .editable(false)
 *                              .mimeType(MimeType.IMAGE_JPEG)
 *                              .contentAttribute("short-edge-length", 74)
 *                              .transformer(IMG_TRANSFORMER)
 *               );
 *      }
 * }
 *  * </code></pre>
 *  * @author Joshua Hansen (joshuah[remove] at up-bear dot net)
 * @see ConstrainedProperty, ContentTransformer
 */
/* * Slight modification of the width/height scaling code com.uwyn.rife.cmf.format.ImageFormatter.
*/
public class PhotoScalingImageTransformer implements ImageContentTransformer {
        public Image transform(Image image, Map<String, String> attr)
                        throws ContentManagerException {
// perform additional conversions according to the provided attributes //Logger.getLogger(this.getClass().getName()).info("Inside transform()"); //Logger.getLogger(this.getClass().getName()).info ("attr.containsKey(\"long-edge-length\"=["+attr.containsKey("long- edge-length")+"]"); //Logger.getLogger(this.getClass().getName()).info ("attr.containsKey(\"short-edge-length\"=["+attr.containsKey ("short-edge-length")+"]");
                if (attr != null)
                {
if (attr.containsKey("long-edge-length") || attr.containsKey ("short-edge-length") )
                        {
                                // retrieve the attributes
                                String lEL_value = attr.get("long-edge-length");
                                String sEL_value = 
attr.get("short-edge-length");
                                int lEL = -1;
                                int sEL = -1;
                                if (lEL_value != null)
                                {
                                        try
                                        {
                                                lEL = 
Integer.parseInt(lEL_value);
                                        }
                                        catch (NumberFormatException e)
                                        {
                                                new ContentManagerException(new 
FormatException(e));
                                        }
                                }
                                if (sEL_value != null)
                                {
                                        try
                                        {
                                                sEL = 
Integer.parseInt(sEL_value);
                                        }
                                        catch (NumberFormatException e)
                                        {
                                                new ContentManagerException(new 
FormatException(e));
                                        }
                                }
                                int width = -1;
                                int height = -1;
                                
                                if (lEL >= 0 || sEL >= 0)
                                {
                                        int orig_width = image.getWidth(null);
                                        int orig_height = image.getHeight(null);
                                        
                                        // If the width is the longer side, set 
it to the lEL.
                                        if( orig_width > orig_height ) {
                                                if( lEL >= 0 ) {
                                                        width = lEL;
                                                } else if ( sEL >= 0 ) {
                                                        height = sEL;
                                                }
                                        } else {
                                                if( lEL >= 0 ) {
                                                        height = lEL;
                                                } else if ( sEL >= 0 ) {
                                                        width = sEL;
                                                }
                                        }
                                        
                                        // ensure that the aspect is preserved 
at all times
                                        if (width >= 0 && height >= 0)
                                        {
                                                double width_ratio = 
((double)orig_width)/width;
                                                double height_ratio = 
((double)orig_height)/height;
                                                if (width_ratio > height_ratio)
                                                {
                                                        height = -1;
                                                }
                                                else if (width_ratio < 
height_ratio)
                                                {
                                                        width = -1;
                                                }
                                        }
                                        
                                        // only do a rescale when the 
dimensions are actually different
                                        if ((width >= 0 && width != orig_width) 
||
                                                (height >= 0 && height != 
orig_height))
                                        {
image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
                                                if (image.getWidth(null) < 0 ||
                                                        image.getHeight(null) < 
0)
                                                {
                                                        ImageWaiter.wait(image);
                                                }
                                        }
                                }
                        }
                }
                return image;
        }
}
--------------------------------------------------------------------- ---
_______________________________________________
Rife-users mailing list
Rife-users@uwyn.com
http://lists.uwyn.com/mailman/listinfo/rife-users
_______________________________________________
Rife-users mailing list
Rife-users@uwyn.com
http://lists.uwyn.com/mailman/listinfo/rife-users


--
Geert Bevin
Uwyn "Use what you need" - http://uwyn.com
RIFE Java application framework - http://rifers.org
Music and words - http://gbevin.com


_______________________________________________
Rife-users mailing list
Rife-users@uwyn.com
http://lists.uwyn.com/mailman/listinfo/rife-users

Reply via email to