<tfoot id='jW56M'></tfoot>

      • <bdo id='jW56M'></bdo><ul id='jW56M'></ul>
      1. <i id='jW56M'><tr id='jW56M'><dt id='jW56M'><q id='jW56M'><span id='jW56M'><b id='jW56M'><form id='jW56M'><ins id='jW56M'></ins><ul id='jW56M'></ul><sub id='jW56M'></sub></form><legend id='jW56M'></legend><bdo id='jW56M'><pre id='jW56M'><center id='jW56M'></center></pre></bdo></b><th id='jW56M'></th></span></q></dt></tr></i><div id='jW56M'><tfoot id='jW56M'></tfoot><dl id='jW56M'><fieldset id='jW56M'></fieldset></dl></div>
      2. <legend id='jW56M'><style id='jW56M'><dir id='jW56M'><q id='jW56M'></q></dir></style></legend>

        <small id='jW56M'></small><noframes id='jW56M'>

        使用 JavaScript 裁剪图像

        Crop the image using JavaScript(使用 JavaScript 裁剪图像)
        • <bdo id='Fv5n3'></bdo><ul id='Fv5n3'></ul>

                <tbody id='Fv5n3'></tbody>
              <i id='Fv5n3'><tr id='Fv5n3'><dt id='Fv5n3'><q id='Fv5n3'><span id='Fv5n3'><b id='Fv5n3'><form id='Fv5n3'><ins id='Fv5n3'></ins><ul id='Fv5n3'></ul><sub id='Fv5n3'></sub></form><legend id='Fv5n3'></legend><bdo id='Fv5n3'><pre id='Fv5n3'><center id='Fv5n3'></center></pre></bdo></b><th id='Fv5n3'></th></span></q></dt></tr></i><div id='Fv5n3'><tfoot id='Fv5n3'></tfoot><dl id='Fv5n3'><fieldset id='Fv5n3'></fieldset></dl></div>

              • <tfoot id='Fv5n3'></tfoot>
                • <small id='Fv5n3'></small><noframes id='Fv5n3'>

                  <legend id='Fv5n3'><style id='Fv5n3'><dir id='Fv5n3'><q id='Fv5n3'></q></dir></style></legend>
                  本文介绍了使用 JavaScript 裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的 Angular 6 应用程序中,我正在创建一个文件上传选项,并且在预览中上传的文件需要显示为自动裁剪和自动调整大小.

                  In my Angular 6 application I am making a file upload option and in preview the uploaded file needs to be displayed with auto cropping and auto resizing.

                  我已经尝试了以下,

                  HTML:

                  <canvas id="canvas"></canvas>
                  <div style="display:none;">
                    <img id="source" [src]="url" width="300" height="227">
                  </div>
                  <input type='file' (change)="onSelectFile($event)">
                  

                  文件选择功能:

                    onSelectFile(event) {
                      if (event.target.files && event.target.files[0]) {
                        var reader = new FileReader();
                  
                        reader.readAsDataURL(event.target.files[0]); // read file as data url
                  
                        reader.onload = (event) => { // called once readAsDataURL is completed
                          this.url = event.target.result;
                        }
                  
                        const canvas : any =  document.getElementById('canvas');
                        const ctx = canvas.getContext('2d');
                        const image = document.getElementById('source');
                  
                        ctx.drawImage(image, 33, 71, 104, 124, 21, 20, 87, 104);
                      }
                    }
                  

                  在上面我参考链接 https://jsfiddle.net/8jwq3cs7/

                        const canvas : any =  document.getElementById('canvas');
                        const ctx = canvas.getContext('2d');
                        const image = document.getElementById('source');
                  
                        ctx.drawImage(image, 33, 71, 104, 124, 21, 20, 87, 104);
                  

                  在使用画布之前,原始图像如下所示:https://mdn.mozillademos.org/files/5397/rhino.jpg

                  Before using the canvas the original image looks like this: https://mdn.mozillademos.org/files/5397/rhino.jpg

                  而使用画布后是这样的:https://jsfiddle.net/8jwq3cs7/

                  Whereas after using canvas it's like this: https://jsfiddle.net/8jwq3cs7/

                  但如果我从 选择文件 中选择图像,那么我在选择后无法看到图像...

                  But if I choose the image from choose file then I am unable to see the image after choosing...

                  工作示例: https://stackblitz.com/edit/angular-file-upload-preview-uwpf8f

                  如果不是采用 Angular 方式,即使仅使用纯 JavaScript 的解决方案也将是可观的......

                  Even the solution with pure JavaScript alone would also be appreciable if not in the Angular way...

                  要求是,如果我选择一个文件,则需要裁剪相同的文件并在预览中自动调整大小...

                  The requirement is, if I choose a file then the same file needs to be cropped and fit the size automatically in preview...

                  请帮助我实现结果无需 jQuery 或任何库...

                  Kindly help me to achieve the result without jQuery or any library...

                  推荐答案

                  这是一个在您上传时使用选择文件按钮获取图像的函数

                  Here is a function to get the image as you are uploading using the choose file button

                  function readURL() {
                      const myimg = document.getElementById("myimg");
                      const input = document.getElementById("myfile");
                      if(input.files && input.files[0]) {
                          const reader = new FileReader();
                          reader.onload = e => {
                              console.log("changed");
                              myimg.src = e.target.result;
                          };
                          reader.readAsDataURL(input.files[0]);
                      }
                  }
                  document.querySelector('#myfile').addEventListener('change', () => {
                      readURL();
                  });
                  

                  HTML 将是

                  <img src="" id="myimg"><br>
                  <input type="file" id="myfile">
                  

                  这是一个工作小提琴

                  如果您添加文件,预览图像将被更新.您实际上在这里获得了一个数据 url.使用数据 url 将图像加载到画布然后裁剪它.调用 drawimg(e.target.result)

                  If you add a file the preview image will be updated. You actually get a data url here. Use the data url to the load the image to the canvas then crop it. calling drawimg(e.target.result)

                  function drawimg(idata) {
                      const img = new Image();
                      img.onload = () => {
                          ctx.drawImage(img, 33, 71, 104, 124, 21, 20, 87, 104);
                      };
                      img.src = idata;
                  }
                  

                  查看工作小提琴:这里

                  这篇关于使用 JavaScript 裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                  How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                  Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                  What is quot;X-Content-Type-Options=nosniffquot;?(什么是“X-Content-Type-Options=nosniff?)
                  SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                  Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)

                    <tbody id='Kbfhc'></tbody>
                  <tfoot id='Kbfhc'></tfoot>
                    <bdo id='Kbfhc'></bdo><ul id='Kbfhc'></ul>

                        • <legend id='Kbfhc'><style id='Kbfhc'><dir id='Kbfhc'><q id='Kbfhc'></q></dir></style></legend>
                        • <i id='Kbfhc'><tr id='Kbfhc'><dt id='Kbfhc'><q id='Kbfhc'><span id='Kbfhc'><b id='Kbfhc'><form id='Kbfhc'><ins id='Kbfhc'></ins><ul id='Kbfhc'></ul><sub id='Kbfhc'></sub></form><legend id='Kbfhc'></legend><bdo id='Kbfhc'><pre id='Kbfhc'><center id='Kbfhc'></center></pre></bdo></b><th id='Kbfhc'></th></span></q></dt></tr></i><div id='Kbfhc'><tfoot id='Kbfhc'></tfoot><dl id='Kbfhc'><fieldset id='Kbfhc'></fieldset></dl></div>
                        • <small id='Kbfhc'></small><noframes id='Kbfhc'>