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

images with transparent background - produces result with black background #6

Closed
abannsunny opened this issue May 15, 2018 · 2 comments

Comments

@abannsunny
Copy link

abannsunny commented May 15, 2018

I have images I upload with transparent background and after resizing the images have a black background.

Is there anyway to give a fill so that the resulting images wont have black background?

solved this:

https://stackoverflow.com/questions/8977369/drawing-png-to-a-canvas-element-not-showing-transparency?rq=1

NB, if you was to use canvas.toDataURL and you set the mimetype to anything other than say gif or png, the transparent parts of the image will be completely black.

options: Options = {
resize: {
maxHeight: 150,
maxWidth: 200,
type: 'image/png'
},
allowedExtensions: ['jpg', 'jpeg', 'png']
};

setting type to png solves it!

@ribizli
Copy link
Owner

ribizli commented May 16, 2018 via email

@ribizli
Copy link
Owner

ribizli commented May 21, 2018

Ok, I've checked this issue a bit more, and figured out, that canvas.drawImage supports transparency. To get the dataURL as transparent, you have to set an output format which also supports transparency. The default value is image/jpeg wich doesn't support it.
Set ResizeOptions.type to image/png (or image/gif) and your output will be transparent too.

@ribizli ribizli closed this as completed May 21, 2018
jpike88 added a commit to jpike88/ngx-image2dataurl that referenced this issue Sep 3, 2020
@ribizli this is the true fix, it sets a white background so transparent images that are forced to be drawn as JPEG can at least have a white background which 99% of the time is desired. pls merge and release
ribizli added a commit that referenced this issue Sep 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants