Resize images with preserve aspect ratio
This commit is contained in:
parent
56f9cc683a
commit
abffd98e29
4 changed files with 35 additions and 6 deletions
|
@ -284,15 +284,32 @@ gfx::Size NativeImage::GetSize() {
|
|||
return image_.Size();
|
||||
}
|
||||
|
||||
float NativeImage::GetAspectRatio() {
|
||||
gfx::Size size = GetSize();
|
||||
if (size.IsEmpty())
|
||||
return 1.f;
|
||||
else
|
||||
return static_cast<float>(size.width()) / static_cast<float>(size.height());
|
||||
}
|
||||
|
||||
mate::Handle<NativeImage> NativeImage::Resize(
|
||||
v8::Isolate* isolate, const base::DictionaryValue& options) {
|
||||
gfx::Size size = GetSize();
|
||||
int width = size.width();
|
||||
int height = size.height();
|
||||
options.GetInteger("width", &width);
|
||||
options.GetInteger("height", &height);
|
||||
size.set_width(width);
|
||||
size.set_height(height);
|
||||
bool width_set = options.GetInteger("width", &width);
|
||||
bool height_set = options.GetInteger("height", &height);
|
||||
size.SetSize(width, height);
|
||||
|
||||
if (width_set && !height_set) {
|
||||
// Scale height to preserve original aspect ratio
|
||||
size.set_height(width);
|
||||
size = gfx::ScaleToRoundedSize(size, 1.f, 1.f / GetAspectRatio());
|
||||
} else if (height_set && !width_set) {
|
||||
// Scale width to preserve original aspect ratio
|
||||
size.set_width(height);
|
||||
size = gfx::ScaleToRoundedSize(size, GetAspectRatio(), 1.f);
|
||||
}
|
||||
|
||||
skia::ImageOperations::ResizeMethod method =
|
||||
skia::ImageOperations::ResizeMethod::RESIZE_BEST;
|
||||
|
@ -420,6 +437,7 @@ void NativeImage::BuildPrototype(
|
|||
.SetMethod("isTemplateImage", &NativeImage::IsTemplateImage)
|
||||
.SetMethod("resize", &NativeImage::Resize)
|
||||
.SetMethod("crop", &NativeImage::Crop)
|
||||
.SetMethod("getAspectRatio", &NativeImage::GetAspectRatio)
|
||||
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings
|
||||
.SetMethod("toPng", &NativeImage::ToPNG)
|
||||
.SetMethod("toJpeg", &NativeImage::ToJPEG);
|
||||
|
|
|
@ -84,6 +84,7 @@ class NativeImage : public mate::Wrappable<NativeImage> {
|
|||
std::string ToDataURL();
|
||||
bool IsEmpty();
|
||||
gfx::Size GetSize();
|
||||
float GetAspectRatio();
|
||||
|
||||
// Mark the image as template image.
|
||||
void SetTemplateImage(bool setAsTemplate);
|
||||
|
|
|
@ -239,4 +239,11 @@ Returns `NativeImage` - The cropped image.
|
|||
|
||||
Returns `NativeImage` - The resized image.
|
||||
|
||||
If only the `height` or the `width` are specified then the current aspect ratio
|
||||
will be preserved in the resized image.
|
||||
|
||||
#### `image.getAspectRatio()`
|
||||
|
||||
Returns `Float` - The image's aspect ratio.
|
||||
|
||||
[buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer
|
||||
|
|
|
@ -98,9 +98,12 @@ describe('nativeImage module', () => {
|
|||
it('returns a resized image', () => {
|
||||
const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
|
||||
assert.deepEqual(image.resize({}).getSize(), {width: 538, height: 190})
|
||||
assert.deepEqual(image.resize({width: 400}).getSize(), {width: 400, height: 190})
|
||||
assert.deepEqual(image.resize({height: 123}).getSize(), {width: 538, height: 123})
|
||||
assert.deepEqual(image.resize({width: 269}).getSize(), {width: 269, height: 95})
|
||||
assert.deepEqual(image.resize({width: 600}).getSize(), {width: 600, height: 212})
|
||||
assert.deepEqual(image.resize({height: 95}).getSize(), {width: 269, height: 95})
|
||||
assert.deepEqual(image.resize({height: 200}).getSize(), {width: 566, height: 200})
|
||||
assert.deepEqual(image.resize({width: 80, height: 65}).getSize(), {width: 80, height: 65})
|
||||
assert.deepEqual(image.resize({width: 600, height: 200}).getSize(), {width: 600, height: 200})
|
||||
assert.deepEqual(image.resize({width: 0, height: 0}).getSize(), {width: 0, height: 0})
|
||||
assert.deepEqual(image.resize({width: -1, height: -1}).getSize(), {width: 0, height: 0})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue