cstefanache / angular2-img-cropper

Angular 2 Image Cropper
MIT License
364 stars 135 forks source link

Cropped Image with dynamicSizing=true not respecting croppedWidth/croppedHeight #162

Closed Theophabis closed 7 years ago

Theophabis commented 7 years ago

Hi there, thanks for the great lib!

I'm running in some problems with the following config:

this.cropperSettings = new CropperSettings();
this.cropperSettings.noFileInput = true;
this.cropperSettings.width = 150;
this.cropperSettings.height = 150;
this.cropperSettings.croppedWidth = 150;
this.cropperSettings.croppedHeight = 150;
this.cropperSettings.dynamicSizing = true;
this.cropperSettings.cropperClass = "custom-class";

css

.custom-class {
  width: 100%;
}

The cropped image seems dependent on the canvas size it has available and not fixed to my settings of 150/150. Am I doing something wrong? Tried a lot of different settings but always get squeezed images as output. Some are much bigger 750/300ish, depending on the available size for the canvas. When i remove the dynamicSizing everything is okey (except for not being responsive).

edit: using angular 4.0.0 and 0.8.4 of your lib

cstefanache commented 7 years ago

So ... not sure that I really understand your problem but there might be the chance that you stumble upon a bug.

If you need to preserve size of the final output image you can set: this.cropperSettings.preserveSize = true;

and you will get (as output) a cropper image with 1-1 pixel ratio with the original. there was a bug there and I am going to do a release on 0.8.5 today for you to check.

if still reproducible - can you please send me 2 image samples with the bug?

thank you!

Theophabis commented 7 years ago

Thanks for the answer, i will check that out. What i expected was that the cropped image has the size I specified in the settings. Let me show you how it behaves.

Original Image: gremlin

With the following settings I get this image

this.cropperSettings = new CropperSettings();
this.cropperSettings.noFileInput = true;
this.cropperSettings.width = 150;
this.cropperSettings.height = 150;
this.cropperSettings.croppedWidth = 150;
this.cropperSettings.croppedHeight = 150;

095924c7-2752-40d5-a496-9fbf1a7fe75e

And with those new settings i get that image

this.cropperSettings = new CropperSettings();
this.cropperSettings.noFileInput = true;
this.cropperSettings.width = 150;
this.cropperSettings.height = 150;
this.cropperSettings.croppedWidth = 150;
this.cropperSettings.croppedHeight = 150;
this.cropperSettings.dynamicSizing = true;
this.cropperSettings.cropperClass = "custom-class";

bba052d2-6795-4762-8305-4658f7399eca

cstefanache commented 7 years ago

is .custom-class sets the image width to 100%?

Theophabis commented 7 years ago

yes, that seems to be necessary that the control is responsive

cstefanache commented 7 years ago

That seems to be the output image not the cropper - so custom-class does not apply here

cstefanache commented 7 years ago

I can reproduce this when: this.cropperSettings1.keepAspect = false;

but I am trying to enforce a fixed width/height on the cropped result this.cropperSettings1.croppedWidth = 200; this.cropperSettings1.croppedHeight = 200;

image

The settings: this.cropperSettings.dynamicSizing = true; this.cropperSettings.cropperClass = "custom-class";

apply only on the cropper canvas component and hidden canvas used as buffer

Theophabis commented 7 years ago

Its also reproducable with keepAspect=true. I guess you set the preview image similar to this:

<img [src]="data.image" [width]="cropperSettings.croppedWidth"
                                     [height]="cropperSettings.croppedHeight">

Which in turn corrects the wrong sized cropped image to show as if its correct. But you can see in the dev-tools that the image is out of proportion (or in my case i can also see the uploaded result has resolution 756x378 with dynamicSizing and custom-class instead of 150x150 without dynamicSizing and custom-class).

Is there a way to force the cropped result to be a fixed size with dynamic sizing or is it a bug or am I doing something wrong?

Theophabis commented 7 years ago

here's what i mean: screenshot from 2017-03-30 09 04 27

limpep commented 7 years ago

@Theophabis have you tried the following

<img [src]="data.image" [width]="croppedWidth" [height]="croppedHeight"> ?

Theophabis commented 7 years ago

@limpep yes thats what I do. You can see in the screenshot above that the preview image from the img-tag looks okey because it has fixed width/height set that corrects the wrong sized data.image. On the right side of the screenshot you see the chrome dev tools that show the real data.image with wrong resolution. The problem is not the preview image, its that data.image has the wrong resolution. The first images that i postet in answer 2 are the actual images that are in data.image and not from a preview or something.

zbarbuto commented 7 years ago

This is definitely a bug, as preserveSize: true with dynamicSizing: true causes the output image to be cropped with the correct ratio but the wrong dimensions (the dimensions use the croppedWidth/croppedHeight ratio but scaled to the original image size).