E
E
eshran2020-06-07 13:14:20
In contact with
eshran, 2020-06-07 13:14:20

How to get a photo in a frame, like in VK?

Hi all!
I wondered how I can get an avatar in this form? https://imgur.com/a/Fqr3Ype
I tried a lot of things, it doesn't work:

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor, 2020-06-07
@eshran

In VK, the picture from the server comes square, it becomes round with the help of CSS.
Judging by the tags, you need to get a round picture in nodeJS. You will need the canvas module , install it with the command: npm install canvas
Next, use the familiar Canvas API with some additions. Here is an example:

const { createCanvas, loadImage } = require('canvas')


const canvas = createCanvas(50, 50)
const ctx = canvas.getContext('2d')

loadImage('https://habrastorage.org/r/w60/webt/5d/fc/84/5dfc84d27a4a2949249978.jpeg')
  .then((image) => {
    ctx.drawImage(image, 0, 0, 50, 50);
    ctx.globalCompositeOperation = 'destination-in';
    ctx.beginPath();
    ctx.arc(25, 25, 25, 0, Math.PI * 2);
    ctx.closePath();
    ctx.fill();

    console.log(canvas.toDataURL()) // Выводим в консоль Data URL
  })

Result: 5edcdd6758dd6509301326.png
PS Dubolom One-celled , stole your ava for demonstration, do not be offended)

D
Dubolom Unicellular, 2020-06-07
@duboloms

Just read the file (picture) that the user sent you.
And in css:

селектор элемента, где будет эта картинка {
  border-radius: 50%; // обрезаю картинку
}

D
Developer, 2020-06-07
@samodum

The original image is square.
Round it is already obtained on the client side in the browser using css

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question