2

Is it possible to crop image proportionally using <p:imageCropper>?

<p:imageCropper value="#{registerPetForm.croppedImage}" image="#{registerPetForm.uploadedFilename}" />

I would restrict users to crop image only in square format:

  ______
 |      |  |  |
 |      |  |  |
 |______|  V  |
 -------->    |
 _____________|

So with same width and height. I want to avoid a rectangle format:

   __________
  |          |
  |__________|

   or
    _____
   |     |
   |     |      
   |     |
   |_____|

How can I achieve this with <p:imageCropper>?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Michał Ziembiński
  • 1,124
  • 2
  • 10
  • 31
  • 1
    You mean, you want to set the aspect ratio of the crop area to 1.0? Have you checked the `` [tag documentation](http://www.primefaces.org/docs/vdl/5.2/core/primefaces-p/imageCropper.html) / [user guide](http://www.primefaces.org/documentation) / [showcase](http://www.primefaces.org/showcase/ui/multimedia/cropper.xhtml) for clues? – BalusC Sep 25 '15 at 14:29
  • i really never use aspectRatio and i dont know for what is used this attribute – Michał Ziembiński Sep 25 '15 at 14:50
  • thanks mate. aspectRatio is that what i looking for :) – Michał Ziembiński Sep 25 '15 at 15:12
  • If you don't know what something is for, there is always a search engine or translator – Kukeltje Sep 25 '15 at 15:36

1 Answers1

2

For that, the aspectRatio attribute can be used which takes a double. A value of 1.0 will make it an exact square.

<p:imageCropper ... aspectRatio="1.0" />
  ______
 |      |
 |      |
 |______|

The aspect ratio represents how many times the width should be of the height. So, a value less than 1.0, e.g. 0.5, will make the width to be 0.5 times of the height.

<p:imageCropper ... aspectRatio="0.5" />
  ______
 |      |
 |      |
 |      |
 |      |
 |______|

And, a value more than 1.0, e.g. 1.5, will make the width to be 1.5 times of the height.

<p:imageCropper ... aspectRatio="1.5" />
  ___________
 |           |
 |           |
 |___________|
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555