Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1px black line added when cropping #551

Open
lapinvert opened this issue Jul 23, 2019 · 22 comments
Open

1px black line added when cropping #551

lapinvert opened this issue Jul 23, 2019 · 22 comments
Labels

Comments

@lapinvert
Copy link

I tried last 1.5.4 version as well as 2.0.0-alpha, but the problem is always the same.

Used settings:

    (...)
     cropper = new Cropper(img,{
          viewMode: 2,
          aspectRatio: targetWidth/targetHeight, // for 300/400 = 0.75
          zoomable: false,
     });

Image used to test with the above settings:

18-test2

Result cropped image:

18-test2-result

@ghost
Copy link

ghost commented Nov 26, 2019

Same problem, any progress?

@YuriTu
Copy link

YuriTu commented Nov 27, 2019

Same problem, add transparent line

@fengyuanchen
Copy link
Owner

This may cause by flooring float numbers to integer numbers, may need to refactor the cropper on numbers normalizing.

@mgntrn
Copy link

mgntrn commented Jan 23, 2020

+1

@karolkrupa
Copy link

Same problem :(. Anyone have any solution?

@ghost
Copy link

ghost commented Feb 3, 2020

I dealt with this manually. This phenomenon occurs if the width of the picture is odd. If the width of an image is odd, then manually reduce the width of the image by 1 and reduce the height by the number of pixels.

@OptimizeCode
Copy link

OptimizeCode commented Feb 27, 2020

What I did is set a bounding box for the cropbox to move in. That leaves some space around the edges. If it comes 1 pixel short it doesn't cause directly the black line to appear but still has a piece of the image to use.

P.S. I didn't use this crop tool but my crop tool has the same issue some time, hope my method can help some one out.

@daiyam
Copy link

daiyam commented Dec 2, 2020

The PR #300 does fix the issue...

@BunnyMelody
Copy link

I dealt with this manually. This phenomenon occurs if the width of the picture is odd. If the width of an image is odd, then manually reduce the width of the image by 1 and reduce the height by the number of pixels.

I tried to short 1px of the width, and 1px of height, but It seemed to not solve the problem, It still remains 1px black line

@junzizaixi
Copy link

Same problem, any progress?

1 similar comment
@chinawhlg
Copy link

Same problem, any progress?

@collinph
Copy link

collinph commented Apr 1, 2021

Can we get some more information on the "odd number of pixels" fix. Apparently nobody is going to take action on this and I'd like to resolve it. In my case, I export the files as PNG images with transparency, then detect if any pixels are transparent before saving as either png or jpg.... and it often saves as png inadvertantly because of the extra transparent pixels

@collinph
Copy link

collinph commented Apr 1, 2021

This may cause by flooring float numbers to integer numbers, may need to refactor the cropper on numbers normalizing.

Which numbers specifically, and what would a good fix be. I'm almost to the point of writing a "post crop" function to go through all the pixels on the edges to detect if this happened (all transparent pixels on the edges) and simply remove them. That's a bad solution, but it's about all I've got without an update

@daiyam
Copy link

daiyam commented Apr 1, 2021

@collinph You can take a look at the PR #300 which is fixing the current issue (with precisePreview: true)

@collinph
Copy link

collinph commented Apr 2, 2021

@collinph You can take a look at the PR #300 which is fixing the current issue (with precisePreview: true)

Is there a built version of this PR implemented where I could implement and do QA testing? I normally use the built JS files. It appears the PR hasn't been pulled and implemented in source yet... Any idea when/if this is going to happen?

@daiyam
Copy link

daiyam commented Apr 2, 2021

@collinph You can test with my build on npm (@daiyam/cropperjs, source) which include the PR.
I will make a new PR since @lexiv0re didn't reply to my question.

@collinph
Copy link

collinph commented Apr 5, 2021

@collinph You can test with my build on npm (@daiyam/cropperjs, source) which include the PR.
I will make a new PR since @lexiv0re didn't reply to my question.

Do I need to enable the precisePreview option to fix the current bug?

@daiyam
Copy link

daiyam commented Apr 5, 2021

@collinph yes

@7anshuai
Copy link
Contributor

7anshuai commented Mar 1, 2023

The PR #1047 fix the issue for me.
The getCroppedCanvas method use the getData method to get the cropped area position and size data. Set getData optional param rounded true to get rounded values to fix the 1px black line issue.

@wvell
Copy link

wvell commented Jul 19, 2023

For those who are looking for a "solution". I'm removing 1 pixel from height or width when i'm hitting an uneven pixel value.

// If the width or height is uneven remove 1 pixel. Otherwise a black line will appear on the right or bottom.
// https://github.com/fengyuanchen/cropperjs/issues/551
if (cropper.getCroppedCanvas().width % 2 !== 0) {
  cropper.setData({
      width: cropper.getCroppedCanvas().width - 1,
  })
}

if (cropper.getCroppedCanvas().height % 2 !== 0) {
  cropper.setData({
      height: cropper.getCroppedCanvas().height - 1,
  })
}

@fengyuanchen
Copy link
Owner

fengyuanchen commented Aug 26, 2023

As of v1.6.0, you can use the rounded option of the getCroppedCanvas method to fix it:

const canvas = cropper.getCroppedCanvas({
  rounded: true,
});

@lakesare
Copy link

Updating cropperjs to "cropperjs": "1.6.0", and using cropper.getCroppedCanvas({ rounded: true }) doesn't fix it for me with the following image.

217108766-b8531e40-4046-4ba0-83ae-0c1c8df93868

kraktus added a commit to kraktus/lila that referenced this issue Jul 28, 2024
fix lichess-org#15782

There are various bug/issues related to the cropperjs lib with transparency

fengyuanchen/cropperjs#551
fengyuanchen/cropperjs#1072

so had to bypass it and keep the original image.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests