-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add the ability to pad
an image
#128
Comments
Would you be using this in a web context, i.e. producing an image that is part of an HTML document? If so, could CSS provide what you're looking for? |
Not quite, you could potentially read the pixel color from the image, and pad the image with this color. |
Which pixel(s) does this refer to? Would a method that returned the dominant/average colour of an image help? From what I understand of the |
Ultimately I'm just looking for the ability to
That would be great |
I am also in need of the pad functionality. I'd like to make square image tiles from rectangular images, adding transparent pixels for padding where necessary (i.e. to pad the shorter of the two dimensions). Is there a work-around with the current API? |
@bennlich you should be able to to do with the existing API via a combination of the |
Ah! Awesome, thanks a bunch for the quick reply. |
Is there a way to specify where the image is embedded in the larger image (bottom left, top right, etc.)? Kind of like a gravity option for embed? |
I'm not sure without testing, which I don't have time to do right now. The embed function however does look useful for one of my use cases, which matches @bennlich s requirements of fitting rectangles in squares. |
I'm trying to achieve something similar, where I have a "zoom" or "crop" feature that lets the user specify how to process their image. To embed a smaller image on a larger background, resorting to below. Wondering if it can be reduced to 1 pipeline instead of 2? So, starting with a 50x50 image, i want to center it in a 256x256 background:
The first resize essentially centers the image vertically on a 50x256 background, then a second pass centers it in the 256x256 background. Inefficient, but gets the job done. hoping for better alternative... ideally works like drawing on a canvas where we specify source rectangle on the image and destination rectangle on the background. |
So after testing, using |
Because of the lack of something like a "centered" gravity option? On Thu, May 7, 2015 at 7:57 AM, Sam Blowes [email protected] wrote:
|
Well, as far as I know, you can only resize, and not |
Gravity is not enough control to embed an image on a specific area of a background. For example, I want to embed a 50x50 image on the lower left corner of a 256x256 background, or any other specific position on the background. Imagine an image cropper use case where you allow the user to place an avatar in a region. |
Would also love to see a padding function. My use case involves a square avatar image that is to be overlaid with a square having a round transparent area in the center (thus creating the illusion of a round avatar). Problem is since source and dest images in overlay function need to be the same size I am losing part of the original image and would love to resize it down a few pixels and adding the same as padding to accommodate for overlay function's requirement. |
@masimakopoulos The existing embed option may provide some of what you're looking for. Assuming you have a transparent overlay image of 300x300px, the following will create a 300x300 white image with sharp('input.jpg')
.resize(300, 300)
.background({r: 255, g: 255, b: 255})
.embed()
.overlayWith('round.png')
.toFile( ... ) |
@lovell thanks for the response. I tried the above but it does not pad the image it just makes everything the size of the overlay (stretches the target image). Not sure if it make as difference but the target image ('input.jpg') is a stream passed through some other sharp manipulations. Here is what I am doing:
Note the above is a simplification of my process. The first resize can be done without an overlay being added, thus the duplication in logic in this example. I am also using the latest version 0.11.3 |
The only time stretching should occur is with the use of ignoreAspectRatio - perhaps we've unearthed a bug here - let me investigate. |
No, what I meant was it enlarged the target image, instead of adding padding to it (both axis, so not ignoring aspect ratio). I apologize for the confusion. |
@masimakopoulos No worries, thanks for confirming 😌 |
I think An example of this, when used in conjunction with existing sharp(input)
.resize(180)
.background({r: 100, g: 0, b: 0})
.extend({top: 10, left: 10, bottom: 20, right: 10})
.toBuffer( ... ) The resultant image would be 200 pixels wide (by whatever high). It would have a dark red border of 10 pixels to the top and sides and 20 pixels to the bottom. |
Commit f950294 on the |
Great news! Looking forward to this release. |
++ Many thanks! |
v0.14.0 now available via npm, thanks again for all the comments and help here. |
Thanks for this. You are a saviour! |
Why do I always read these threads from start to finish!? Because sometimes they get exciting, like little mini stories putting me on the edge of my seat. Happy to see this had a good ending :-) I needed this functionality to be able to put a css rounded border (to make the image a circle) so needed to extend it. However @lovell I think you have this in the wrong place in the documentation. It is currently under image operations 'http://sharp.pixelplumbing.com/en/stable/api-operation/#extend'. It would be really helpful to add this under resizing images where I think people are much more likely to be looking. http://sharp.pixelplumbing.com/en/stable/api-resize/ Same maybe goes for trim. |
It would be great to see something much like the implementation here
https://github.com/EyalAr/lwip#user-content-pad
The text was updated successfully, but these errors were encountered: