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

How could i crop an image to circle? #214

Closed
lanlin opened this issue May 8, 2020 · 6 comments
Closed

How could i crop an image to circle? #214

lanlin opened this issue May 8, 2020 · 6 comments

Comments

@lanlin
Copy link

lanlin commented May 8, 2020

I want to crop an image to the circle so that I can use it as an avatar, how to do this?

@beomseoklee
Copy link
Member

Thanks for contacting us @lanlin
Currently, the solution does not support that, so you might need to implement this one by yourself.

For example, this is a very simple example which only supports square images making circle.

if (key === 'circle') {
    let { width, height } = metadata;
    if (edits.resize) {
        width = edits.resize.width ? edits.resize.width : width;
        height = edits.resize.height ? edits.resize.height : height;
    }

    const rect = Buffer.from(`<svg><rect x="0" y="0" width="${width}" height="${height}" rx="100" ry="100"/></svg>`);
    const params = [{ input: rect, blend: 'dest-in' }];
    image.composite(params);
}

And you can create encoded URL with:

const j = JSON.stringify({
    bucket: 'your-bucket',
    key: 'your-image',
    edits: {
        circle: ''
    }
});
const encoded = Buffer.from(j).toString('base64');

This will provide you something like this.
image

@lanlin
Copy link
Author

lanlin commented May 9, 2020

@beomseoklee thanks very much. Would you please add this feature in the next release?

@lanlin lanlin closed this as completed May 11, 2020
@haoujey
Copy link

haoujey commented May 26, 2020

Thanks for contacting us @lanlin
Currently, the solution does not support that, so you might need to implement this one by yourself.

For example, this is a very simple example which only supports square images making circle.

if (key === 'circle') {
    let { width, height } = metadata;
    if (edits.resize) {
        width = edits.resize.width ? edits.resize.width : width;
        height = edits.resize.height ? edits.resize.height : height;
    }

    const rect = Buffer.from(`<svg><rect x="0" y="0" width="${width}" height="${height}" rx="100" ry="100"/></svg>`);
    const params = [{ input: rect, blend: 'dest-in' }];
    image.composite(params);
}

And you can create encoded URL with:

const j = JSON.stringify({
    bucket: 'your-bucket',
    key: 'your-image',
    edits: {
        circle: ''
    }
});
const encoded = Buffer.from(j).toString('base64');

This will provide you something like this.
image

Where this code should be implemented ?

@beomseoklee
Copy link
Member

@haoujey You can implement that part in image-handler.js after smartCrop if statement.

@haoujey
Copy link

haoujey commented May 27, 2020

thanks ! :)

@G-Lenz
Copy link
Contributor

G-Lenz commented Jan 29, 2021

We've added support for circular cropping in v5.2.0.

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

4 participants