Skip to content

Instantly share code, notes, and snippets.

@mikewadhera
Created September 18, 2009 02:05
Show Gist options
  • Save mikewadhera/188838 to your computer and use it in GitHub Desktop.
Save mikewadhera/188838 to your computer and use it in GitHub Desktop.
public MagickImage resized(int width, int height){
MagickImage result = new MagickImage(width, height);
int originalWidth = this.getWidth();
int originalHeight = this.getHeight();
int maxSize = Math.max(width, height);
float scaleFactor = 1.0f;
if (originalWidth > originalHeight)
scaleFactor = ((float) maxSize / (float) originalWidth);
else
scaleFactor = ((float) maxSize / (float) originalHeight);
originalWidth = (int)(originalWidth * scaleFactor);
originalHeight = (int)(originalHeight * scaleFactor);
if (originalWidth > 0 && originalHeight > 0) {
Image i = image.getScaledInstance(originalWidth, originalHeight, Image.SCALE_SMOOTH);
image = new BufferedImage(originalWidth, originalHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.drawImage(i, null, null);
g.dispose();
i.flush();
result.setImage(image);
return result;
} else {
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment