1 16 package org.mortbay.html; 17 import java.io.File ; 18 import java.io.FileInputStream ; 19 import java.io.IOException ; 20 21 import org.apache.commons.logging.Log; 22 import org.mortbay.log.LogFactory; 23 import org.mortbay.util.IO; 24 import org.mortbay.util.LogSupport; 25 26 27 32 public class Image extends Tag 33 { 34 private static Log log = LogFactory.getLog(Image.class); 35 36 37 public Image(String src) 38 { 39 super("img"); 40 attribute("src",src); 41 } 42 43 44 46 public Image(String dirname, String src) 47 { 48 super("img"); 49 attribute("src",src); 50 setSizeFromGif(dirname,src); 51 } 52 53 54 56 public Image(File gif) 57 { 58 super("img"); 59 attribute("src",gif.getName()); 60 setSizeFromGif(gif); 61 } 62 63 64 public Image(String src,int width, int height, int border) 65 { 66 this(src); 67 width(width); 68 height(height); 69 border(border); 70 } 71 72 73 public Image border(int b) 74 { 75 attribute("border",b); 76 return this; 77 } 78 79 80 public Image alt(String alt) 81 { 82 attribute("alt",alt); 83 return this; 84 } 85 86 87 93 public Image setSizeFromGif(String dirname, 94 String pathname) 95 { 96 String filename =dirname + pathname.replace('/',File.separatorChar); 97 return setSizeFromGif(filename); 98 } 99 100 101 103 public Image setSizeFromGif(String filename) 104 { 105 return setSizeFromGif(new File (filename)); 106 } 107 108 109 111 public Image setSizeFromGif(File gif) 112 { 113 if (gif.canRead()) 114 { 115 FileInputStream in = null; 116 try{ 117 byte [] buf = new byte[10]; 118 in = new FileInputStream (gif); 119 if (in.read(buf,0,10)==10) 120 { 121 if(log.isDebugEnabled())log.debug("Image "+gif.getName()+ 122 " is " + 123 ((0x00ff&buf[7])*256+(0x00ff&buf[6])) + 124 " x " + 125 (((0x00ff&buf[9])*256+(0x00ff&buf[8])))); 126 width((0x00ff&buf[7])*256+(0x00ff&buf[6])); 127 height(((0x00ff&buf[9])*256+(0x00ff&buf[8]))); 128 } 129 } 130 catch (IOException e){ 131 LogSupport.ignore(log,e); 132 } 133 finally { 134 IO.close(in); 135 } 136 } 137 138 return this; 139 } 140 141 } 142 143 144 145 | Popular Tags |