简体   繁体   English

HTML图像裁剪+显示图像代码

[英]Html image crop + showing image code

I am developing an Cordova mobile App. 我正在开发Cordova移动应用程序。 I want to add profile pictures, so i have to add a croping tool. 我想添加个人资料图片,所以我必须添加裁剪工具。

I created an example 我创建了一个例子

 function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(e) { $('#vorschau').attr('src', e.target.result); $('#bild_code').html(e.target.result); } reader.readAsDataURL(input.files[0]); } } $("#imgInp").change(function() { readURL(this); }); $('#box').draggable({ containment: '#main' }); 
 body { margin: 0px; padding: 0px; } #main { position: absolute; top: 30px; min-height: 100px; min-width: 100px; } #box { position: absolute; top: 0px; left: 0px; width: 100px; height: 100px; background: black; opacity: 0.5; } #bild_code { position: absolute; top: 400px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <div id="container"> <input type='file' id="imgInp" /> <div id="main"> <img id="vorschau" src="#" alt="your image" /> <div id="box"></div> </div> <div id="bild_code"></div> </div> 

Thats my basic idea. 那是我的基本想法。 When you chose an image you see the code, which i want to upload later, but thats not the problem. 当您选择图片时,您会看到代码,我想稍后再上传,但这不是问题。 When you are moving the black box and then for example click on a button that code should change, so that i am able to upload the croped-image-code. 当您移动黑匣子,然后例如单击一个按钮时,代码应该更改,这样我就可以上传裁剪图像代码。 Is there an easy solution? 有一个简单的解决方案吗?

Hope you can help ;) 希望你能提供帮助;)

I suggest you take a look at https://github.com/RubaXa/jquery.fileapi/ . 我建议您看看https://github.com/RubaXa/jquery.fileapi/ Even though the plugin itself is no longer updated, it's underlying piece of code ( https://github.com/mailru/FileAPI/ ) is. 即使插件本身不再更新,它的底层代码( https://github.com/mailru/FileAPI/ )也是。

The examples "userpic + crop" here seem to be exactly what you requested. 例子“userpic +种植” 在这里似乎是你要求什么。

See if this works. 看看是否可行。 I added a 100x100 (same size as the box) hidden canvas to the page. 我在页面上添加了100x100(与框大小相同)的隐藏画布。 When you drag the box I take the top and left of the box and use context.drawImage using the box's position and using an area of 100x100, which crops the image where the box is and draws it inside the hidden canvas. 当您拖动该框时,我将其放在框的顶部和左侧,并使用context.drawImage使用框的位置并使用100x100的区域,该区域会裁剪框所在的图像并将其绘制在隐藏的画布内。 Then I get the cropped image's source from the canvas using toDataUrl() 然后我使用toDataUrl()从画布上获取裁剪图像的源

 function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(e) { $('#vorschau').attr('src', e.target.result); $('#bild_code').html(e.target.result); } reader.readAsDataURL(input.files[0]); } } $("#imgInp").change(function() { readURL(this); }); $('#box').draggable({ containment: '#main' ,drag: function() { crop(); }, }); function crop(){ var crop_canvas = document.getElementById('canvas'); var left = $('#box').position().left; var top = $('#box').position().top; crop_canvas.getContext('2d').drawImage($('#vorschau')[0], left, top, 100, 100, 0, 0, 100, 100); $('#bild_code').html(crop_canvas.toDataURL()); } 
 body { margin: 0px; padding: 0px; } #main { position: absolute; top: 30px; min-height: 100px; min-width: 100px; } #box { position: absolute; top: 0px; left: 0px; width: 100px; height: 100px; background: black; opacity: 0.5; } #bild_code { position: absolute; top: 400px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <div id="container"> <input type='file' id="imgInp" /> <div id="main"> <img id="vorschau" src="#" alt="your image" /> <div id="box"></div> </div> <div id="bild_code"></div> </div> <canvas id="canvas" width="100" height="100" style="display:none;"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM