【问题标题】:Turn a HTML image element into a base64 image whilst adopting the inline width and height将 HTML 图像元素转换为 base64 图像,同时采用内联宽度和高度
【发布时间】:2018-10-28 18:10:36
【问题描述】:

我正在使用ng2-img-cropper (https://www.npmjs.com/package/ng2-img-cropper)。它提供设置,以便在从输入中选择文件时,将 base64 url​​ 应用于 HTML 中的图像标签,同时采用“cropperSettings”,例如定义的宽度和高度。

但是,我有一个要求,我需要生成两张图像而不是一张。一张大图和一张小图。大图为 500x500,小图为 200x200。我创建了两个“cropperSettings”,以便 HTML 内联宽度和高度显示两种尺寸。但是,两者的 src 属性仍然相同。在本例中,它们都是 500x500。

是否可以使用 javascript 抓取一个 html img 元素(它具有 base64 作为 src 属性)并将其转换为 base64 图像,但使用内联高度和宽度,而不是 src 图像高度和宽度? (在这种情况下,下面的第三个图像标签 - 使用较小的高度和宽度设置)

HTML

<img-cropper #cropper [image]="data1" [settings]="cropperSettings1"></img-cropper>
<img id="largeImage" [src]="data1.image" [width]="cropperSettings1.croppedWidth" [height]="cropperSettings1.croppedHeight">
<img id="thumbnailImage" [src]="data1.image" [width]="cropperSettings2.croppedWidth" [height]="cropperSettings2.croppedHeight">

裁剪器设置

// This is for the large image (500x500)
this.cropperSettings1 = new CropperSettings();
this.cropperSettings1.width = 200;
this.cropperSettings1.height = 200;
this.cropperSettings1.croppedWidth = 500;
this.cropperSettings1.croppedHeight = 500;
this.cropperSettings1.canvasWidth = 500;
this.cropperSettings1.canvasHeight = 500;
this.cropperSettings1.minWidth = 500;
this.cropperSettings1.minHeight = 500;
this.cropperSettings1.rounded = false;
this.cropperSettings1.cropperDrawSettings.strokeColor = 'rgba(255,255,255,1)';
this.cropperSettings1.cropperDrawSettings.strokeWidth = 2;
this.cropperSettings1.noFileInput = true;
this.cropperSettings1.keepAspect = true;
this.data1 = {};

// This is for the small image
this.cropperSettings2 = new CropperSettings();
this.cropperSettings2.croppedWidth = 200;
this.cropperSettings2.croppedHeight = 200;

检测到文件时触发的函数

var image: any = new Image();
  var file: File = event.target.files[0];
  var myReader: FileReader = new FileReader();
  var that = this;
  myReader.onloadend = (loadEvent: any) => {
    image.src = loadEvent.target.result;
    that.cropper.setImage(image);
  };
 myReader.readAsDataURL(file);

【问题讨论】:

    标签: angular cropper


    【解决方案1】:

    正如documentation 中所说的(您应该集中精力阅读,而不是只是即时阅读),您可以自行处理文件上传。

    这意味着你可以上传一个文件,创建 2 个 base64 图像,并将它们影响到相应的裁剪器(或对它做任何你想做的事情。

    但我不明白你的问题,因为一旦图片上传,你可以简单地使用相同的来源并调整图片大小以方便你。
    裁剪器将创建一个新图像,因此它不会改变 base64 源。

    【讨论】:

    • 感谢您的回复。在此要求之前,我使用了一个脚本,该脚本立即基于来自 HTML 输入的文件创建了 500x500 和 300x300 图像。这很好用,我有自己的代码可以上传到 firebase。但是,这个新要求意味着我需要包含 ng-img-cropper 以允许用户选择他们想要的图像部分。目前,使用我的代码,我只能使用cropperSettings1 上传图像。理想情况下,我想同时上传两者,但找不到让 src 文件采用这两种设置的方法。根据您的评论,上传后如何再次调整图像大小?
    • “上传后再次调整图像大小”是什么意思?据我了解,您让用户裁剪图像,并将裁剪和未裁剪的图像发送到 firebase。这不是你的工作流程吗?
    • 啊。抱歉,如果我没有很好地表达出来。我有一个 HTML 输入。当用户选择图像(例如,可能是 3000 x 2320)时,图像裁剪工具将出现并允许用户选择他们想要显示的图像部分。我有一个保存按钮,然后将裁剪的图像上传到 Firebase。但是,裁剪后的图像将是 500x500。我想上传两张图片。 500x500 和 200x200,但都使用用户选择的图像部分。现在,我只能上传 500x500 的。这有帮助吗?
    • 是的,更清楚了,谢谢!我建议您只存储 500x500 图像,因为您只是在复制数据。存储最大的,当你想显示它时,只需使用标准 HTML 调整它的大小(相信我,一旦你看到 firebase 价格,你会感谢我的)
    • 不幸的是,这是一个要求。将来,大图像可能是 1000x1000,用于设计更重的页面,而 200x200 用于缩略图卷起。否则我可能会加载 200 个项目,全部加载一个 1000x1000 的“缩略图”。
    猜你喜欢
    • 1970-01-01
    • 2013-07-01
    • 2015-10-16
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多