You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ab...@apache.org on 2004/04/21 15:17:46 UTC

cvs commit: jakarta-taglibs-sandbox/image/src/org/apache/taglibs/image ResizeTag.java

abey        2004/04/21 06:17:46

  Modified:    image/src/org/apache/taglibs/image ResizeTag.java
  Log:
  Added a scale to bestfit option - Abey
  
  Revision  Changes    Path
  1.3       +95 -51    jakarta-taglibs-sandbox/image/src/org/apache/taglibs/image/ResizeTag.java
  
  Index: ResizeTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs-sandbox/image/src/org/apache/taglibs/image/ResizeTag.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ResizeTag.java	7 Oct 2003 06:33:54 -0000	1.2
  +++ ResizeTag.java	21 Apr 2004 13:17:46 -0000	1.3
  @@ -52,7 +52,7 @@
    * <http://www.apache.org/>.
    *
    */
  - 
  +
   package org.apache.taglibs.image;
   
   import javax.servlet.jsp.JspException;
  @@ -62,58 +62,102 @@
   import com.mullassery.imaging.util.Util;
   
   /**
  - * <p>Scales/ Resizes an image.</p>
  - *
  - * @author Abey Mullassery
  + * <p>
  + * Scales/ Resizes an image.
  + * </p>
    * 
  + * @author Abey Mullassery
  + *  
    */
   public class ResizeTag extends TagSupport {
   
  -	private String scale;
  -	private String width;
  -	private String height;
  -
  -	public int doEndTag() throws JspException {
  -		Tag pTag = getParent();
  -		if (scale == null && width == null & height == null)
  -			throw new JspException("Specify atleast one of \"scale\", \"width\" or \"height\".");
  -		if (pTag == null || !(pTag instanceof ImageHolder))
  -			throw new JspException("Resize must be a nested tag of Image Tag!");
  -		ImageHolder par = (ImageHolder)getParent();
  -
  -		if (scale != null && scale.length() != 0) {
  -			par.setImage(par.getImaging().scale(par.getImage(), Util.convertToFraction(scale, 1)));
  -		} else if (width == null || height == null) {
  -			if (width == null) {
  -				par.setImage(
  -					par.getImaging().scale(
  -						par.getImage(),
  -						Util.convertToFraction(height, par.getImage().getHeight())));
  -			} else {
  -				par.setImage(
  -					par.getImaging().scale(
  -						par.getImage(),
  -						Util.convertToFraction(width, par.getImage().getWidth())));
  -			}
  -		} else {
  -			par.setImage(
  -				par.getImaging().scale(
  -					par.getImage(),
  -					Util.convertToFraction(width, par.getImage().getWidth()),
  -					Util.convertToFraction(height, par.getImage().getHeight())));
  -		}
  -		return EVAL_PAGE;
  -	}
  -
  -	public void setScale(String scale) {
  -		this.scale = scale;
  -	}
  -
  -	public void setHeight(String height) {
  -		this.height = height;
  -	}
  -
  -	public void setWidth(String width) {
  -		this.width = width;
  -	}
  +    private String scale;
  +
  +    private String width;
  +
  +    private String height;
  +
  +    private boolean bestfit;
  +
  +    public int doEndTag() throws JspException {
  +        Tag pTag = getParent();
  +
  +        if (pTag == null || !(pTag instanceof ImageHolder))
  +                throw new JspException(
  +                        "Resize must be a nested tag of Image Tag!");
  +
  +        if (scale == null && width == null && height == null)
  +                throw new JspException(
  +                        "Specify atleast one of \"scale\", \"width\" or "
  +                                + "\"height\".");
  +
  +        if (bestfit) {
  +            if (width == null || height == null)
  +                    throw new JspException(
  +                            "Specify both the \"width\" and \"height\" with "
  +                                    + "the \"bestfit\" option.");
  +            if (width.indexOf('%') != -1 || height.indexOf('%') != -1)
  +                    throw new JspException(
  +                            "Specify both the \"width\" and \"height\" in "
  +                                    + "absolute pixels (not in percentage) with the "
  +                                    + "\"bestfit\" option.");
  +        }
  +
  +        ImageHolder par = (ImageHolder) getParent();
  +
  +        if (bestfit) {// width and height is specified
  +            float ht = Float.parseFloat(height);
  +            float wt = Float.parseFloat(width);
  +            float wScale = (wt / par.getImage().getWidth());
  +            float hScale = (ht / par.getImage().getHeight());
  +            System.out.println(wt + ", " + ht + ", " + wScale + ", " + hScale);
  +            
  +            if (wScale <= hScale) {
  +                par.setImage(par.getImaging().scale(par.getImage(), wScale));
  +            } else {
  +                par.setImage(par.getImaging().scale(par.getImage(), hScale));
  +            }            
  +        } else if (scale != null && scale.length() != 0) {
  +            par.setImage(par.getImaging().scale(par.getImage(),
  +                    Util.convertToFraction(scale, 1)));
  +        } else if (width == null || height == null) {
  +            if (width == null) {
  +                par.setImage(par.getImaging().scale(
  +                        par.getImage(),
  +                        Util.convertToFraction(height, par.getImage()
  +                                .getHeight())));
  +            } else {
  +                par.setImage(par.getImaging().scale(
  +                        par.getImage(),
  +                        Util
  +                                .convertToFraction(width, par.getImage()
  +                                        .getWidth())));
  +            }
  +        } else {
  +            par
  +                    .setImage(par.getImaging().scale(
  +                            par.getImage(),
  +                            Util.convertToFraction(width, par.getImage()
  +                                    .getWidth()),
  +                            Util.convertToFraction(height, par.getImage()
  +                                    .getHeight())));
  +        }
  +        return EVAL_PAGE;
  +    }
  +
  +    public void setScale(String scale) {
  +        this.scale = scale;
  +    }
  +
  +    public void setHeight(String height) {
  +        this.height = height;
  +    }
  +
  +    public void setWidth(String width) {
  +        this.width = width;
  +    }
  +
  +    public void setBestfit(boolean bestfit) {
  +        this.bestfit = bestfit;
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org