javascript - 使用 Cropper js 裁剪后图像质量降低

标签 javascript jquery ruby-on-rails cropperjs

我克服了很多解决方案和建议,但没有一个对我有用。我正在使用 dropzone 进行图像上传,并使用 cropper js 来裁剪图像。但是每次我裁剪图像后,图像质量都会降低(模糊)

实际图片

/image/UsVqz.jpg

裁剪图片
/image/snAuB.png

   //This is my cropper js code, As per the documentation I have set max height, max width, imageSmoothingQuality etc. But still image qualty get reduced after crop.
   var $cropperModal = $("<div>Company Logo</div>");
   $cropperModal.modal('show').on("shown.bs.modal", function() {
     var $image = $('#img-' + c);
     var cropper = $image.cropper({
       aspectRatio: 1//,
     }).on('hidden.bs.modal', function() {
       $image.cropper('destroy');
     });

     //After I uploaded the image, Below code allow me to crop the image

     $cropperModal.on('click', '.crop-upload', function() {
       $image.cropper('getCroppedCanvas', {
         width: 160,
         height: 90,
         minWidth: 256,
         minHeight: 256,
         maxWidth: 4096,
         maxHeight: 4096,
         fillColor: '#fff',
         imageSmoothingEnabled: false,
         imageSmoothingQuality: 'high'
       })
       .toBlob(function(blob) {
         // Create a new Dropzone file thumbnail
         myDropZone.createThumbnail(
           blob,
           myDropZone.options.thumbnailWidth,
           myDropZone.options.thumbnailHeight,
           myDropZone.options.thumbnailMethod,
           false, 
           function(dataURL) {              
             // Update the Dropzone file thumbnail
             myDropZone.emit('thumbnail', file, dataURL);
             // Return the file to Dropzone
             done(blob);
         });
         $cropperModal.modal('hide');
         });
       })        
       .on('click', '.rotate-right', function() {
         $image.cropper('rotate', 90);
       })
       .on('click', '.rotate-left', function() {
         $image.cropper('rotate', -90);
       })
       .on('click', '.reset', function() {
         $image.cropper('reset');
       })
   });      
 }```

最佳答案

我在使用 cropper js 时遇到了同样的问题,以下是适合我的解决方案。 (我在这里分享,以便它可以帮助别人)

  • 将参数 qualityArgument 设置为 toBlob 方法的值 1

  • 设置 imageSmoothingEnabled 为 getCroppedCanvas 的 true 并且没有设置 高度、宽度

     cropper.getCroppedCanvas({
         minWidth: 256,
         minHeight: 256,
         maxWidth: 4096,
         maxHeight: 4096,
         fillColor: '#fff',
         imageSmoothingEnabled: true,
         imageSmoothingQuality: 'high',
     });
    
     cropper.getCroppedCanvas().toBlob(function (blob) {
         var formData = new FormData();
         formData.append('upload', blob, 'imagetest.jpeg');
    
         $.ajax('/ListingManager/Images/index', {
             method: 'POST',
             data: formData,
             processData: false,
             contentType: false,
             success: function () {
                 console.log('success');
             },
             error: function () {
                 console.log('error');
             }
         });
    
    
    
     }, 'image/jpeg', 1);
    

这是结果

Example image

关于javascript - 使用 Cropper js 裁剪后图像质量降低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573339/

相关文章:

javascript - 如何使用 jquery 停止移动屏幕旋转?

javascript - 滑出并将鼠标移出故障jquery

ruby-on-rails - 从Rails 2.3.8升级到4.0

ruby-on-rails - 助手 “fields_for”无法正常工作

jquery文档高度内存使用情况

ruby-on-rails - rails : default scoping being cached by query cache?

javascript - 我应该如何最好地搜索根据特定元素的 id 动态生成的自定义列表?

JavaScript 正则表达式匹配

jquery - 在 fontawesome 中堆叠图标

javascript - 在声明期间访问对象中的先前值