日期:2014-05-17  浏览次数:20799 次

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


------解决方案--------------------
Image getScaledInstance(int width, int height, int hints) 
创建此图像的缩放版本。
------解决方案--------------------
http://topic.csdn.net/t/20040405/11/2926009.html你访问下,有两个答案,你看下合不合适。
------解决方案--------------------
Image getScaledInstance(int width, int height, int hints) 

用这个,百试不爽。祝楼主好运
------解决方案--------------------
Java code
/**
     * 对图片进行压缩
     * @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();
        }
    }

------解决方案--------------------
探讨
Image getScaledInstance(int width, int height, int hints)