jwagner / smartcrop-sharp

Node module for using smartcrop via sharp/libvips
MIT License
115 stars 14 forks source link

Error: Invalid URI "/" #8

Closed Nau077 closed 4 years ago

Nau077 commented 4 years ago

I have valid url for src variable: https://rixtrema.net/exeimages/exeimages_200/ba/6b/ba6b3e99300519d912511ea36c687f77.jpg

use it inside your code

const makeImgCrop = async (req, res) => {

    function applySmartCrop(src, dest, width, height) {
      request(src, { encoding: null }, function process(error, response, body) {
        if (error) return console.error(error);
        smartcrop.crop(body, { width: width, height: height }).then(function(result) {
          var crop = result.topCrop;
          sharp(body)
            .extract({ width: crop.width, height: crop.height, left: crop.x, top: crop.y })
            .resize(width, height)
            .toFile(dest);
        });
      });
    }

    const src = req.body
    const img_crop = applySmartCrop(src, 'flower-square.jpg', 128, 128);
    await res.status(200).json(img_crop)
}

and get

Error: Invalid URI "/" at Request.init (/home/roman/web/img-neiro-full/server/node_modules/request/request.js:273:31) at new Request (/home/roman/web/img-neiro-full/server/node_modules/request/request.js:127:8) at request (/home/roman/web/img-neiro-full/server/node_modules/request/index.js:53:10) at applySmartCrop (/home/roman/web/img-neiro-full/server/controller/cropImages.js:9:7) at makeImgCrop (/home/roman/web/img-neiro-full/server/controller/cropImages.js:22:22) at Layer.handle [as handle_request] (/home/roman/web/img-neiro-full/server/node_modules/express/lib/router/layer.js:95:5) at next (/home/roman/web/img-neiro-full/server/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/roman/web/img-neiro-full/server/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/roman/web/img-neiro-full/server/node_modules/express/lib/router/layer.js:95:5) at /home/roman/web/img-neiro-full/server/node_modules/express/lib/router/index.js:281:22

==== Finally I dont understand at all how to use your library, lack of examples - only nice images dont help understand how to use it. And I dont understand what output from function applySmartCrop() I will get, if all go with success. base64/something else?

Is it possible to use smarcrop only on client side or it is espessially server desision with this module (smartcrop-sharp)?

jwagner commented 4 years ago

This module is explicitly for use with node.js. It does not work in the browser. If you want to use smartcrop in the browser, just use the base library. Based on your code it's impossible to say what is going wrong. What is fairly clear from the stack trace is that you don't even reach smartcrop-sharp. You seem to pass request an argument which it doesn't like. Also img_crop will always be undefined since applySmartCrop doesn't return any value. I'm not sure what you want to achieve with img_crop.