1 // ======================================================================== 2 // $Id: Image.java,v 1.8 2005/08/13 00:01:23 gregwilkins Exp $ 3 // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // ======================================================================== 15 16 package org.mortbay.html; 17 import java.io.File; 18 import java.io.FileInputStream; 19 import java.io.IOException; 20 21 import org.mortbay.log.Log; 22 import org.mortbay.util.IO; 23 24 25 /* ---------------------------------------------------------------- */ 26 /** HTML Image Tag. 27 * @see org.mortbay.html.Block 28 * @version $Id: Image.java,v 1.8 2005/08/13 00:01:23 gregwilkins Exp $ 29 * @author Greg Wilkins 30 */ 31 public class Image extends Tag 32 { 33 34 /* ------------------------------------------------------------ */ 35 public Image(String src) 36 { 37 super("img"); 38 attribute("src",src); 39 } 40 41 /* ------------------------------------------------------------ */ 42 /** Construct from GIF file. 43 */ 44 public Image(String dirname, String src) 45 { 46 super("img"); 47 attribute("src",src); 48 setSizeFromGif(dirname,src); 49 } 50 51 /* ------------------------------------------------------------ */ 52 /** Construct from GIF file. 53 */ 54 public Image(File gif) 55 { 56 super("img"); 57 attribute("src",gif.getName()); 58 setSizeFromGif(gif); 59 } 60 61 /* ------------------------------------------------------------ */ 62 public Image(String src,int width, int height, int border) 63 { 64 this(src); 65 width(width); 66 height(height); 67 border(border); 68 } 69 70 /* ------------------------------------------------------------ */ 71 public Image border(int b) 72 { 73 attribute("border",b); 74 return this; 75 } 76 77 /* ------------------------------------------------------------ */ 78 public Image alt(String alt) 79 { 80 attribute("alt",alt); 81 return this; 82 } 83 84 /* ------------------------------------------------------------ */ 85 /** Set the image size from the header of a GIF file. 86 * @param dirname The directory name, expected to be in OS format 87 * @param pathname The image path name relative to the directory. 88 * Expected to be in WWW format (i.e. with slashes) 89 * and will be converted to OS format. 90 */ 91 public Image setSizeFromGif(String dirname, 92 String pathname) 93 { 94 String filename =dirname + pathname.replace('/',File.separatorChar); 95 return setSizeFromGif(filename); 96 } 97 98 /* ------------------------------------------------------------ */ 99 /** Set the image size from the header of a GIF file. 100 */ 101 public Image setSizeFromGif(String filename) 102 { 103 return setSizeFromGif(new File(filename)); 104 } 105 106 /* ------------------------------------------------------------ */ 107 /** Set the image size from the header of a GIF file. 108 */ 109 public Image setSizeFromGif(File gif) 110 { 111 if (gif.canRead()) 112 { 113 FileInputStream in = null; 114 try{ 115 byte [] buf = new byte[10]; 116 in = new FileInputStream(gif); 117 if (in.read(buf,0,10)==10) 118 { 119 if(Log.isDebugEnabled())Log.debug("Image "+gif.getName()+ 120 " is " + 121 ((0x00ff&buf[7])*256+(0x00ff&buf[6])) + 122 " x " + 123 (((0x00ff&buf[9])*256+(0x00ff&buf[8])))); 124 width((0x00ff&buf[7])*256+(0x00ff&buf[6])); 125 height(((0x00ff&buf[9])*256+(0x00ff&buf[8]))); 126 } 127 } 128 catch (IOException e){ 129 Log.ignore(e); 130 } 131 finally { 132 IO.close(in); 133 } 134 } 135 136 return this; 137 } 138 139 } 140 141 142