diff --git a/src/main/java/w2phtml/base/BinaryGraphicsDocument.java b/src/main/java/w2phtml/base/BinaryGraphicsDocument.java index 253e453..ce468b0 100644 --- a/src/main/java/w2phtml/base/BinaryGraphicsDocument.java +++ b/src/main/java/w2phtml/base/BinaryGraphicsDocument.java @@ -177,7 +177,12 @@ public class BinaryGraphicsDocument implements OutputFile { */ public void write(OutputStream os) throws IOException { if (blob!=null) { - os.write(blob, nOff, nLen); + if (cropped) { + os.write(blob); + } else { + os.write(blob, nOff, nLen); + } + } } @@ -231,16 +236,17 @@ public class BinaryGraphicsDocument implements OutputFile { int height = image.getHeight(); int width = image.getWidth(); String formatName = MIMETypes.getFormatType(blob); - int xTopLeft = offsets[3]; - int yTopLeft = offsets[0]; - int xBottomRight = width - offsets[3] - offsets[1]; - int yBottomRight = height - offsets[0] - offsets[2]; - BufferedImage croppedImage = image.getSubimage(xTopLeft, yTopLeft, xBottomRight , yBottomRight ); + int leftOffset = offsets[3]; + int topOffset = offsets[0]; + int newWidth = width - offsets[3] - offsets[1]; + int newHeight = height - offsets[0] - offsets[2]; + BufferedImage croppedImage = image.getSubimage(leftOffset, topOffset, newWidth , newHeight ); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(croppedImage, "JPG", baos); sMimeType = MIMETypes.JPEG; - blob = baos.toByteArray(); - cropped = true; + this.blob = baos.toByteArray(); + this.cropped = true; + updateFileName(offsets); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -250,6 +256,16 @@ public class BinaryGraphicsDocument implements OutputFile { } + private void updateFileName(int[] offsets) { + StringBuilder croppedFileName = new StringBuilder(); + croppedFileName.append(sFileName.substring(0, sFileName.lastIndexOf('.'))); + for (int i = 0; i < offsets.length; i++) { + croppedFileName.append("-" + offsets[i]); + } + croppedFileName.append(".jpg"); + sFileName = croppedFileName.toString(); + } + private int[] getOffsets(StyleWithProperties style) { /* top, right, bottom, left */ int[] offsets = { 0, 0, 0, 0 }; diff --git a/src/main/java/w2phtml/xhtml/content/DrawParser.java b/src/main/java/w2phtml/xhtml/content/DrawParser.java index 29ff9e9..ef2a6d3 100644 --- a/src/main/java/w2phtml/xhtml/content/DrawParser.java +++ b/src/main/java/w2phtml/xhtml/content/DrawParser.java @@ -589,7 +589,7 @@ public class DrawParser extends Parser { } else { // In all other cases, create an img element - if (bgd != null && !bgd.isLinked() && !bgd.isRecycled() && !bEmbedImg) { + if (bgd != null && !bgd.isLinked() && !bEmbedImg) { converter.addDocument(bgd); } Element image = converter.createElement("img");