Question

I have a form where a user can upload an image and crop it.

After uploading, the image is used in the cropper component for the crop

I want to display it in a fixed size (if the user's image is too large) and I don't manage to do it.

After uploading the cropper component display the image in its original size (so if the width is 1200px it's filling all the screen)

I can't use css because the div size is generated by primefaces script and I can't use script because it's loading before image upload

Was it helpful?

Solution

Just resize image when uploading

public String handleImageFileUpload(FileUploadEvent event){

    File file = new File("PATH_TO_UPLOAD_DIR");
    file.mkdirs();
    file = new File("PATH_TO_UPLOAD_FILE");

    try( InputStream is = event.getFile().getInputstream();
             OutputStream out = new FileOutputStream(file)  ) {

            BufferedImage img = ImageIO.read(is);
            BufferedImage scaledImg;
            if(img.getWidth() >= img.getHeight())
                scaledImg = Scalr.resize(img, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_TO_HEIGHT, 300, 400);
            else
                scaledImg = Scalr.resize(img, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_TO_WIDTH, 400, 300);
            ImageIO.write(scaledImg, "jpg", out);

        } catch (IOException e) {
            e.printStackTrace();
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top