一般情况下,网站的后台的图片要是在网站指定的位置和大小显示,是否网站后台先根据原始图片的大小生成缩略图之后在传输到浏览器之显示。
因此,我有两个疑问。
1,比如服务器(程序或者插件)有没有那种图片生成技术,能直接根据图片要显示的大小直接生成图片的工具,比如说根据html标签中<img width="*" height="*">根据width,height的属性直接生成相应的图片大小。
2,另外如果要是自己独立编写这样一个生成缩略图的程序(Java实现),需要注意些什么和具体的流程是什么。
希望大侠不吝赐教,先谢谢了。

解决方案 »

  1.   

     Image getScaledInstance(int width, int height, int hints) 
              创建此图像的缩放版本。 
      

  2.   

    http://topic.csdn.net/t/20040405/11/2926009.html你访问下,有两个答案,你看下合不合适。
      

  3.   

    Image getScaledInstance(int width, int height, int hints) 用这个,百试不爽。祝楼主好运
      

  4.   

    /**
     * 对图片进行压缩
     * @param srcfile 本地图片路径
     * @param imgdist 上传服务器路径
     * @param widthdist 压缩宽度
     * @param heightdist 压缩高度
     */
    public void reduceImg(File srcfile, File imgdist, int widthdist,int heightdist) {
    try {
    Image src = javax.imageio.ImageIO.read(srcfile);
    int width = src.getWidth(null);
    int height = src.getHeight(null);
    // 宽度除以高度的比例
    float wh = (float) width / (float) height; if (1 < wh) {
    float tmp_heigth = (float) widthdist / wh;
    BufferedImage tag = new BufferedImage(widthdist,(int) tmp_heigth, BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src, 0, 0, widthdist,(int) tmp_heigth, null);
    FileOutputStream out = new FileOutputStream(imgdist);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(tag);
    out.close();
    } else {
    float tmp_width = (float) heightdist * wh;
    BufferedImage tag = new BufferedImage((int) tmp_width,heightdist, BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width,heightdist, null);
    FileOutputStream out = new FileOutputStream(imgdist);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(tag);
    out.close();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
      

  5.   

    感谢大家给我的回复。
    我下午想了一会。想出了一个图片过滤器的思路。
    例如<img width='200' height='200' src="dfd.jpg"/>
    当然,这个jpg文件不一定是200*200大小的,那么怎么办呢。
    因为我的后台是用SSH框架。我想到了使用struts2的拦截器(现在只是想法阶段,还要看看拦截器的一些细节)。
    就是在服务器准备发送生成好的html文件的时候,将html文件拦截下来。然后遍历该文件下的所有<img标签>包括(<img></img>,<img/>)然后,根据src的地址,从数据库中获取图片的信息,如果图片的大小正好等于width,height属性的话。则不作处理。如果不等于的话,就根据width,height属性生成指定大小的图片。同时修改<img/>标签中的内容。程序结束。
    大致的流程是:
    浏览器请求--->后台程序--->生成html页面--->拦截器拦截--->修改html中<img/>标签的内容(生成图片)---->返回给客户端浏览器.