Following Frank and Quirijn's answers, you might be interested in resizing the image in the Cartridge Claims processor using the Ambient Data Framework. This solution will be technological agnostic and can be reused in both Java and .NET. You just need to put the modified image bytes in the Claim, and then use it in Java or .Net.
Java claims processor:
public void onRequestStart(ClaimStore claims) throws AmbientDataException { int publicationId = getPublicationId(); int binaryId = getBinaryId(); BinaryContentDAO bcDAO = (BinaryContentDAO)StorageManagerFactory.getDAO(publicationId, StorageTypeMapping.BINARY_CONTENT); BinaryContent binaryContent = bcDAO.findByPrimaryKey(publicationId, binaryId, null); byte[] binaryBuff = binaryContent.getContent(); pixelRatio = getPixelRatio(); int resizeWidth = getResizeWidth(); BufferedImage original = ImageIO.read(new ByteArrayInputStream(binaryBuff)); if (original.getWidth() < MAX_IMAGE_WIDTH) { float ratio = (resizeWidth * 1.0f) / (float)MAX_IMAGE_WIDTH; float width = original.getWidth() * ratio; float height = original.getHeight() * ratio; BufferedImage resized = new BufferedImage(Math.round(width), Math.round(height), original.getType()); Graphics2D g = resized.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(original, 0, 0, resized.getWidth(), resized.getHeight(), null); g.dispose(); ByteArrayOutputStream output = new ByteArrayOutputStream(); BinaryMeta meta = new BinaryMetaFactory().getMeta(String.format("tcm:%s-%s", publicationId, binaryId)); String suffix = meta.getPath().substring(meta.getPath().lastIndexOf('.') + 1); ImageIO.write(resized, suffix, output); binaryBuff = output.toByteArray(); } claims.put(new URI("taf:extensions:claim:resizedimage"), binaryBuff); }
.NET HTTP handler:
public void ProcessRequest(HttpContext context) { if (context != null) { HttpResponse httpResponse = HttpContext.Current.Response; ClaimStore claims = AmbientDataContext.CurrentClaimStore; if (claims != null) { Codemesh.JuggerNET.byteArray javaArray = claims.Get<Codemesh.JuggerNET.byteArray>("taf:extensions:claim:resizedimage"); byte[] resizedImage = javaArray.ToNative(javaArray); if (resizedImage != null && resizedImage.Length > 0) { httpResponse.Expires = -1; httpResponse.Flush(); httpResponse.BinaryWrite(resizedImage); } } } }
Java Filter:
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ClaimStore claims = AmbientDataContext.getCurrentClaimStore(); if (claims != null) { Object resizedImage = claims.get(new URI("taf:extensions:claim:resizedimage")); if (resizedImage != null) { byte[] binaryBuff = (byte[])resizedImage; response.getOutputStream().write(binaryBuff); } } }
source share