Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetImageDimensions can crash Mendix app when processing high resolution images #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@

package communitycommons.actions;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.webui.CustomJavaAction;
import communitycommons.proxies.ImageDimensions;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

public class GetImageDimensions extends CustomJavaAction<IMendixObject>
{
Expand All @@ -32,22 +35,39 @@ public GetImageDimensions(IContext context, IMendixObject ImageParameter)
@java.lang.Override
public IMendixObject executeAction() throws Exception
{
this.ImageParameter = __ImageParameter == null ? null : system.proxies.Image.initialize(getContext(), __ImageParameter);
this.ImageParameter = this.__ImageParameter == null ? null : system.proxies.Image.initialize(getContext(), __ImageParameter);

// BEGIN USER CODE
ImageDimensions imageDimensions = new ImageDimensions(getContext());
try (InputStream inputStream = Core.getImage(getContext(), this.ImageParameter.getMendixObject(), false)) {
BufferedImage bimg = ImageIO.read(inputStream);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ImageIo.read method reads the whole inputstream.

imageDimensions.setHeight(bimg.getHeight());
imageDimensions.setWidth(bimg.getWidth());
}

return imageDimensions.getMendixObject();
try (InputStream inputStream = Core.getImage(getContext(), this.ImageParameter.getMendixObject(), false);
ImageInputStream imageStream = ImageIO.createImageInputStream(inputStream)) {
if(imageStream != null) {
Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
if (!readers.hasNext()) throw new IOException("Can't find a suitable imagereader for this image");;

ImageReader reader = readers.next();

try {
reader.setInput(imageStream);
int height = reader.getHeight(reader.getMinIndex());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PNG, JPG and BMP ImageReader implementations only read the imageheader to determine the height and width.

int width = reader.getWidth(reader.getMinIndex());
imageDimensions.setHeight(height);
imageDimensions.setWidth(width);
return imageDimensions.getMendixObject();
} finally {
reader.dispose();
}
} else {
throw new IOException("Can't open a stream for this image");
}
}
// END USER CODE
}

/**
* Returns a string representation of this action
* @return a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
Expand Down