GithubHelp home page GithubHelp logo

Comments (4)

lovell avatar lovell commented on May 28, 2024 1

Did you see the gravity and position options of resize()?

https://sharp.pixelplumbing.com/api-resize

You'll probably want either position: 'left top' or gravity: 'northwest'.

(attention is a "smartcrop" strategy and is content-dependent, so probably won't do what you want consistently.)

from sharp.

rocc-o avatar rocc-o commented on May 28, 2024 1

Perhaps I managed to obtain the desired result with position: 'left top' even with remote images over 4,000 pixels wide. Maybe then around 6,000 pixels everything falls apart, I'll know when I get there. Thank you.

async function cropImage(imageData, width, height) {
    try {
        console.log('Cropping image with width:', width, 'and height:', height);

        return await sharp(imageData)
            .resize({ width, height, fit: 'cover', position: 'left top' })
            .toBuffer();
    } catch (error) {
        console.error('Error cropping image:', error);
        throw new Error('Error cropping image:', error);
    }
}

from sharp.

rocc-o avatar rocc-o commented on May 28, 2024

Solved, I guess. If somenone know a better and more polished way please tell me.

// Function to crop the image
async function cropImage(imageData, width, height) {
    try {
        console.log('Cropping image with width:', width, 'and height:', height);

        return await sharp(imageData)
            .resize({ width, height, fit: 'cover', position: sharp.strategy.attention })  // Crop from top-left corner
            .toBuffer();
    } catch (error) {
        console.error('Error cropping image:', error);
        throw new Error('Error cropping image:', error);
    }
}

from sharp.

rocc-o avatar rocc-o commented on May 28, 2024

Yes, I tried both, but the result wasn't very satisfactory, perhaps because the remote URLs I'm processing aren't all the same size. I've noticed that remote images up to 2557 pixels wide are cropped from the top, and that's the desired result, but as it starts to reach about 4,000 pixels wide the crop starts to move towards the centre.

from sharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.