Support toDataURL without 1x representation
This commit is contained in:
parent
8eaf48e552
commit
0bbbeb307e
3 changed files with 14 additions and 6 deletions
|
@ -21,6 +21,7 @@
|
|||
#include "net/base/data_url.h"
|
||||
#include "third_party/skia/include/core/SkPixelRef.h"
|
||||
#include "ui/base/layout.h"
|
||||
#include "ui/base/webui/web_ui_util.h"
|
||||
#include "ui/gfx/codec/jpeg_codec.h"
|
||||
#include "ui/gfx/codec/png_codec.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
|
@ -257,12 +258,12 @@ v8::Local<v8::Value> NativeImage::ToJPEG(v8::Isolate* isolate, int quality) {
|
|||
}
|
||||
|
||||
std::string NativeImage::ToDataURL() {
|
||||
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
|
||||
std::string data_url;
|
||||
data_url.insert(data_url.end(), png->front(), png->front() + png->size());
|
||||
base::Base64Encode(data_url, &data_url);
|
||||
data_url.insert(0, "data:image/png;base64,");
|
||||
return data_url;
|
||||
if (HasRepresentation(1.0)) {
|
||||
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
|
||||
return webui::GetPngDataUrl(png->front(), png->size());
|
||||
} else {
|
||||
return webui::GetBitmapDataUrl(image_.AsBitmap());
|
||||
}
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> NativeImage::GetBitmap(v8::Isolate* isolate) {
|
||||
|
@ -297,6 +298,10 @@ bool NativeImage::IsEmpty() {
|
|||
return image_.IsEmpty();
|
||||
}
|
||||
|
||||
bool NativeImage::HasRepresentation(float scale_factor) {
|
||||
return image_.AsImageSkia().HasRepresentation(scale_factor);
|
||||
}
|
||||
|
||||
gfx::Size NativeImage::GetSize() {
|
||||
return image_.Size();
|
||||
}
|
||||
|
@ -468,6 +473,7 @@ void NativeImage::BuildPrototype(
|
|||
.SetMethod("resize", &NativeImage::Resize)
|
||||
.SetMethod("crop", &NativeImage::Crop)
|
||||
.SetMethod("getAspectRatio", &NativeImage::GetAspectRatio)
|
||||
.SetMethod("hasRepresentation", &NativeImage::HasRepresentation)
|
||||
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings
|
||||
.SetMethod("toPng", &NativeImage::ToPNG)
|
||||
.SetMethod("toJpeg", &NativeImage::ToJPEG);
|
||||
|
|
|
@ -83,6 +83,7 @@ class NativeImage : public mate::Wrappable<NativeImage> {
|
|||
const gfx::Rect& rect);
|
||||
std::string ToDataURL();
|
||||
bool IsEmpty();
|
||||
bool HasRepresentation(float scale_factor);
|
||||
gfx::Size GetSize();
|
||||
float GetAspectRatio();
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ describe('nativeImage module', () => {
|
|||
it('returns an empty image', () => {
|
||||
const empty = nativeImage.createEmpty()
|
||||
assert.equal(empty.isEmpty(), true)
|
||||
assert.equal(empty.hasRepresentation(1.0), false)
|
||||
assert.equal(empty.getAspectRatio(), 1)
|
||||
assert.equal(empty.toDataURL(), 'data:image/png;base64,')
|
||||
assert.deepEqual(empty.getSize(), {width: 0, height: 0})
|
||||
|
|
Loading…
Reference in a new issue