简体   繁体   English

预览图像并在上传之前裁剪图像

[英]Preview an image and crop it before it's uploaded

I have a image upload button on my site. 我的网站上有一个图片上传按钮。 This is how I want it to behave: 这就是我希望它表现的方式:

  1. A user clicks a "Choose file" button and selects an image. 用户单击“选择文件”按钮并选择图像。
  2. The image shows before any submission (using javascript FileReader API) 图像显示在任何提交之前(使用javascript FileReader API)
  3. A cropping plugin is applied to that image, for instance: http://www.jqueryrain.com/?BEAlLLl9 裁剪插件应用于该图像,例如: http//www.jqueryrain.com/?BEAlLLl9
  4. The user chooses the cropping area and hits "Submit". 用户选择裁剪区域并点击“提交”。

I need help with the step between 2 and 3. The problem using FileReader API to show images right when they are selected is that hey get a src attribute which is base64 encoded. 我需要帮助处理2到3之间的步骤。使用FileReader API在选中它们时正确显示图像的问题是,它获得了一个base64编码的src属性。 And the image cropping plugin I've used can't properly find/initialize/draw on the image since it won't recognize the src="" attribute as valid. 我使用的图像裁剪插件无法在图像上正确查找/初始化/绘制,因为它不会将src=""属性识别为有效。

How can I achieve step 1-4? 我怎样才能实现步骤1-4? Surely this has been done before on major sites like Facebook etc? 当然这已经在Facebook等主要网站上完成了吗?

http://html5.sapnagroup.com/demos/dragDropUploadsCrop/ This link will guide what you want http://html5.sapnagroup.com/2012/08/preview-and-crop-before-upload/ http://html5.sapnagroup.com/demos/dragDropUploadsCrop/此链接将指导您想要的内容http://html5.sapnagroup.com/2012/08/preview-and-crop-before-upload/

Files with following extensions are only allowed
        allowedExtensions: ['gif','jpg','jpeg','png','txt'],
        showCropTool: 1,
        sizeLimit: 10737418240, // Maximum filesize limit which works without any problems is 30MB. Current limit is set to 10MB = 10 * 1024 * 1024
        params: {
            'uploadedBy': 'Sapnagroup',
            'x1': '0',  // x coordinates of the image
            'y1': '0',      // x coordinates of the image
            'x2': '300',    // x coordinates of the image
            'y2': '150',    // y coordinates of the image
            'cWidth': '300',        // required crop width
            'cHeight': '150',       // required crop heignt
            'oWidth': '800',        // width of the crop preview image
            'oHeight': '600',       // height of the crop preview image
            'rWidth': '300',        // resize width
            'rHeight': '150'        // resize height
        },
  1. To show preview of selected file you can use createObjectURL method: 要显示所选文件的预览,您可以使用createObjectURL方法:

     var windowURL = $window.URL || $window.webkitURL; var imageUrl = windowURL.createObjectURL(imageFile); 
  2. Then when you have image url you can display interface for crop selection. 然后,当您有图像网址时,您可以显示裁剪选择的界面。

  3. When area to crop where selected you can crop image using canvas. 选择要裁剪的区域时,可以使用画布裁剪图像。

     function crop(image, x1, y1, x2, y2) { var canvas = document.createElement('canvas'); canvas.setAttribute('width', x2 - x1); canvas.setAttribute('height', y2 - y1); var context = canvas.getContext('2d'); context.drawImage(image, -x1, -y1); return canvas; } 
  4. After that you can get Blob with image from canvas ( https://github.com/blueimp/JavaScript-Canvas-to-Blob ), which can be uploaded to server using XHR2. 之后,您可以使用canvas( https://github.com/blueimp/JavaScript-Canvas-to-Blob )获取带有图像的Blob,可以使用XHR2将其上传到服务器。

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

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