diff --git a/src/helma/image/ImageWrapper.java b/src/helma/image/ImageWrapper.java index 5226e35a..4633645a 100644 --- a/src/helma/image/ImageWrapper.java +++ b/src/helma/image/ImageWrapper.java @@ -577,7 +577,7 @@ public class ImageWrapper { */ public void saveAs(String filename, float quality, boolean alpha) throws IOException { - generator.write(this, filename, quality, alpha); + generator.write(this, checkFilename(filename), quality, alpha); } /** @@ -660,4 +660,20 @@ public class ImageWrapper { else return bi.getRGB(x, y); } + + /** + * Utility method to be used by write(). + * Converts file name to absolute path and creates parent directories. + * @param filename the file name + * @return the absolute path for the file name + * @throws IOException if missing directories could not be created + */ + String checkFilename(String filename) throws IOException { + File file = new File(filename).getAbsoluteFile(); + File parent = file.getParentFile(); + if (parent != null && !parent.exists() && !parent.mkdirs()) { + throw new IOException("Error creating directories for " + filename); + } + return file.getPath(); + } } \ No newline at end of file