gpt4 book ai didi

javascript - 如何获取上传图片的DataUrl?

转载 作者:太空宇宙 更新时间:2023-11-04 15:41:09 26 4
gpt4 key购买 nike

上传.html

<input type="hidden" name="forearmleft" id="forearm" value="">
<input type="hidden" name="elbotxt" id="elbotxt" value="">
<input type="hidden" name="rightall" id="rightall" value="">
<!--<img src="" id="elbotxt">-->
</form>
<canvas id="c"></canvas>
<canvas id="mr_rotator_can" width="436" height="567"></canvas>
<canvas id="f6258182013" width="436" height="567"></canvas>

用于上传并将 blob url 转换为 dataurl 的脚本文件

var source, source_ctx, sleeve, sleeve_ctx, finalsleeve, finalsleeve_ctx, temp_can1, temp_can1_ctx;

function forearmleftcurve(event){

var getImagePath = URL.createObjectURL(event.target.files[0]);
console.log(getImagePath)

temp_can1 = document.getElementById('f6258182013');
temp_can1_ctx = temp_can1.getContext('2d');

forearmleftrotator(getImagePath, '77');
// draw_on_cloth("f6258182013", '/image/1j8Dw.png', "mr_rotator_can", "img_fastening1a");

};

function forearmleftrotator(var2, var3) //var2=image aka pattern var3= angle for rotate
{
var mr_rotator_var = document.getElementById('mr_rotator_can');
var mr_rotator_var_ctx = mr_rotator_var.getContext("2d");
mr_rotator_var.width = mr_rotator_var.width;
var imageObj_rotator = new Image();
console.log(mr_rotator_var.toDataURL());

imageObj_rotator.onload = function () {
var pattern_rotator = mr_rotator_var_ctx.createPattern(imageObj_rotator, "repeat");
mr_rotator_var_ctx.fillStyle = pattern_rotator;
mr_rotator_var_ctx.rect(0, 0, mr_rotator_var.width, mr_rotator_var.height);
mr_rotator_var_ctx.rotate(var3 * Math.PI / 180);
mr_rotator_var_ctx.fill();

};
imageObj_rotator.src = var2;

//**blob to dataURL**
function blobToDataURL(blob, callback) {
var a = new FileReader();
a.onload = function(e) {callback(e.target.result);}
a.readAsDataURL(event.target.files[0]);
console.log(event.target.files[0])
}

test:
blobToDataURL(var2, function(dataurl){
console.log(dataurl);
});

我面临的问题是我正在获取 dataUrl,但它是原始图像而不是上传的图像。上传的图像比原始图像旋转一个。

请帮助我。旋转图像位于 var2 中。

最佳答案

您可以使用toDataURL()方法从canvas获取数据url。我稍微修改了您的代码,添加了一个将数据 url 呈现为图像的示例。

var source, source_ctx, sleeve, sleeve_ctx, finalsleeve, finalsleeve_ctx, temp_can1, temp_can1_ctx;
var imageHolder = document.getElementById('imageHolder');
var fileInput = document.getElementById('forearm');

fileInput.addEventListener('change', forearmleftcurve);

function forearmleftcurve(event){
var getImagePath = URL.createObjectURL(event.target.files[0]);
temp_can1 = document.getElementById('f6258182013');
temp_can1_ctx = temp_can1.getContext('2d');
forearmleftrotator(getImagePath, '77');
}

function forearmleftrotator(var2, var3){
var mr_rotator_var = document.getElementById('mr_rotator_can');
var mr_rotator_var_ctx = mr_rotator_var.getContext("2d");
mr_rotator_var.width = mr_rotator_var.width;
var imageObj_rotator = new Image();
imageObj_rotator.src = var2;

imageObj_rotator.onload = function () {
var pattern_rotator = mr_rotator_var_ctx.createPattern(imageObj_rotator, "repeat");
mr_rotator_var_ctx.fillStyle = pattern_rotator;
mr_rotator_var_ctx.rect(0, 0, mr_rotator_var.width, mr_rotator_var.height);
mr_rotator_var_ctx.rotate(var3 * Math.PI / 180);
mr_rotator_var_ctx.fill();
imageHolder.src = mr_rotator_var.toDataURL();

};

}
//**blob to dataURL**
function blobToDataURL(blob, callback) {
var a = new FileReader();
a.onload = function(e){
callback(e.target.result);
}
a.readAsDataURL(event.target.files[0]);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Canvas</title>
</head>
<body>
<img id="imageHolder" src="" alt="">
<form>
<input type="file" name="forearmleft" id="forearm" value="">
<input type="hidden" name="elbotxt" id="elbotxt" value="">
<input type="hidden" name="rightall" id="rightall" value="">
</form>
<canvas id="c"></canvas>
<canvas id="mr_rotator_can" width="436" height="567"></canvas>
<canvas id="f6258182013" width="436" height="567"></canvas>
</body>
</html>

关于javascript - 如何获取上传图片的DataUrl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43862153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com