Image Resize

Asagida image lari resize eden bir servlet bulabilirsiniz. Buna gore gonderdiginiz parametreler uzerinden resize islemi yapilabilir.
Kod cagirimi asagidaki gibi olacaktir
www.melihsakarya.com/ImageResize?file=demo.gif&width=200


Servlet tanimi
 

    <servlet>
     <description></description>
     <display-name>ImageResizer</display-name>
     <servlet-name>ImageResizer</servlet-name>
     <servlet-class>
      com.mergeCons.util.image.ImageResizer
     </servlet-class>
    </servlet>
    <servlet-mapping>
     <servlet-name>ImageResizer</servlet-name>
     <url-pattern>/ImageResizer</url-pattern>
    </servlet-mapping>

Java kodu

    package com.mergeCons.utils.image;
     
    import java.awt.AlphaComposite;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
     
    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    /**
     * Servlet implementation class for Servlet: ImageResizer
     *
     */
    public class ImageResizer extends HttpServlet {
        private static final long serialVersionUID = -8285774993751841288L;
        public void doGet(HttpServletRequest request, HttpServletResponse response) {
            String imageOutput = getParam(request, "output", "jpg");
            String imageRoot = getParam(request, "root", "c:/upl/");
            String imageFile = getParam(request, "file", "mergeCons.gif");
            int width = Integer.parseInt(getParam(request, "width", "250"));
            int height = Integer.parseInt(getParam(request, "height", "0"));
            if ("png".equals(imageOutput)) {
                response.setContentType("image/png");
            } else {
                response.setContentType("image/jpeg");
            }
            //String imageLoc = request.getSession().getServletContext().getRealPath(imageRoot) +"\\"+ imageFile;
            String imageLoc = imageRoot+imageFile;
            System.out.println("imageLoc = " + imageLoc);
            if((new File(imageRoot+width+"x"+imageFile)).exists()){
             System.out.println("Dosya var = " + imageRoot+width+"x"+imageFile);
             try {
              BufferedImage bufferedImage = ImageIO.read(new File(imageRoot+width+"x"+imageFile));
              ImageIO.write(bufferedImage, imageOutput, response.getOutputStream());
     
             } catch (Exception e) {
                 System.out.println("Problem with image: " + imageLoc + e);
             }
         }
            else{
             System.out.println("Dosya yok ve yaratiliyor = " + imageRoot+width+"x"+imageFile);
             try {
                 BufferedImage bufferedImage = ImageIO.read(new File(imageLoc));
                 int calcHeight = height &gt; 0 ? height : (width * bufferedImage.getHeight() / bufferedImage.getWidth());
                 ImageIO.write(createResizedCopy(bufferedImage, width, calcHeight), imageOutput
    , response.getOutputStream());
     
                 BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(imageRoot+width+"x"+imageFile));
                 ImageIO.write(createResizedCopy(bufferedImage, width, calcHeight), imageOutput, buf);
                 buf.flush();
                 buf.close();
     
             } catch (Exception e) {
                 System.out.println("Problem with image: " + imageLoc + e);
             }
            }
        }
        BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight) {
            BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = scaledBI.createGraphics();
            g.setComposite(AlphaComposite.Src);
            g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
            g.drawString("<a href="http://www.melihsakarya.com">www.melihsakarya.com</a>", 20, 20);
            g.dispose();
            return scaledBI;
        }
     
        private String getParam(HttpServletRequest request, String param, String def) {
            String parameter = request.getParameter(param);
            if (parameter == null || "".equals(parameter)) {
                return def;
            } else {
                return parameter;
            }
        }
    }
Nov 6th, 2008
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>