Skip to content

Instantly share code, notes, and snippets.

@monteiro
Last active December 11, 2015 15:19
Show Gist options
  • Save monteiro/4620423 to your computer and use it in GitHub Desktop.
Save monteiro/4620423 to your computer and use it in GitHub Desktop.
Handle File upload and cut Image using ImageUtilities
private static Boolean handleProfilePictureUpload(User user, Request request) {
Boolean isChanged = Boolean.FALSE;
Http.MultipartFormData body = request.body().asMultipartFormData();
Http.MultipartFormData.FilePart uploadFilePart = body
.getFile("picture");
if (uploadFilePart != null) {
File file = uploadFilePart.getFile();
ImageUtilities.scale(file, 50, 50); // x = 50px y = 50px
S3File s3File = new S3File();
s3File.setName(uploadFilePart.getFilename());
s3File.setFile(file);
s3File.save();
user.picture = s3File;
isChanged = Boolean.TRUE;
}
return isChanged;
}
}
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import play.Logger;
public class ImageUtilities {
public static File scale(File in, Integer scaleX, Integer scaleY) {
BufferedImage image = null;
try {
image = ImageIO.read(in);
Image newImg = image.getScaledInstance(scaleX, scaleY,
Image.SCALE_SMOOTH);
BufferedImage outScaled = toBufferedImage(newImg);
ImageIO.write(outScaled, "jpg", in);
} catch (IOException e) {
Logger.error("Error scaling image");
e.printStackTrace();
}
return in;
}
public static File resize(File in, Integer x, Integer y, Integer width, Integer height) {
BufferedImage image = null;
try {
image = ImageIO.read(in);
BufferedImage outResized = image.getSubimage(x, y, width, height);
ImageIO.write(outResized, "jpg", in);
} catch (IOException e) {
Logger.error("Error scaling image");
e.printStackTrace();
}
return in;
}
/**
*
* @param image
* @return
*/
// This method returns a buffered image with the contents of an image
private static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels; for this method's
// implementation, see Determining If an Image Has Transparent Pixels
// Create a buffered image with a format that's compatible with the
// screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null),
image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
}
package controllers;
import models.files.S3File;
import be.objectify.deadbolt.actions.RoleHolderPresent;
import play.mvc.Controller;
import play.mvc.Result;
import static actions.CurrentUser.currentUser;
public class RemoteAssets extends Controller {
public static Result getProfilePicture() {
Result result = null;
S3File file = currentUser().picture; // attribute picture from model User
if (file != null) {
result = ok(file.getObjectContent());
} else {
result = null;
}
return result;
}
}
@(currentUser: User)
@if(currentUser.picture != null) {
<img src="@{routes.RemoteAssets.getProfilePicture}" />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment