首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >克罗维特图像src

克罗维特图像src
EN

Stack Overflow用户
提问于 2018-08-20 08:24:34
回答 1查看 940关注 0票数 0

我正在使用钩子,我想预览剪裁的图像在一个模式。如何获得裁剪图像的src url?

我复制了这个插件的一些基本html代码。

代码语言:javascript
复制
    <div class="image-editor">
      <input type="file" class="cropit-image-input">
      <div class="cropit-preview"></div>
      <div class="image-size-label">
        Resize image
      </div>
      <input type="range" class="cropit-image-zoom-input">
      <button class="rotate-ccw">Rotate counterclockwise</button>
      <button class="rotate-cw">Rotate clockwise</button>

      <button class="export">Export</button>
    </div>

JS

代码语言:javascript
复制
      $(function() {
        $('.image-editor').cropit({
          imageState: {
            src: 'http://lorempixel.com/500/400/',
          },
        });

        $('.rotate-cw').click(function() {
          $('.image-editor').cropit('rotateCW');
        });
        $('.rotate-ccw').click(function() {
          $('.image-editor').cropit('rotateCCW');
        });

        $('.export').click(function() {
          var imageData = $('.image-editor').cropit('export');
          window.open(imageData);
        });
      });

到目前为止,我尝试的是从文档中使用

代码语言:javascript
复制
$('.image-editor').cropit('imageSrc'); //but it returns null. Is there any other way to do this? 

演示和文档似乎没有混合,所以我有一个很难使用插件。

EN

回答 1

Stack Overflow用户

发布于 2018-08-20 09:48:14

把这部分改一下。imagedata本身就是一个可用的base64 URL。它不能在新窗口中打开,但您可以轻松地将其设置为任何图像的src。

代码语言:javascript
复制
$('.export').click(function() {
      var imageData = $('.image-editor').cropit('export');
       $("#image").src = imageData;
    });

工作示例

代码语言:javascript
复制
$(function() {
  $('.image-editor').cropit({
    exportZoom: 1.25,
    imageBackground: true,
    imageBackgroundBorderWidth: 50,
     
  });

  $('.export').click(function() {
    var imageData = $('.image-editor').cropit('export');
 document.querySelector("#image").src = imageData;   
  });
});
代码语言:javascript
复制
.image-editor {
   text-align: center;
}

.cropit-preview {
  background-color: #f8f8f8;
  background-size: cover;
  border: 5px solid #ccc;
  border-radius: 3px;
  margin-top: 7px;
  width: 250px;
  height: 250px;
   display: inline-block;
}

.cropit-preview-image-container {
  cursor: move;
}

.cropit-preview-background {
  opacity: .2;
  cursor: auto;
}

.image-size-label {
  margin-top: 10px;
}

input, .export {
  /* Use relative position to prevent from being covered by image background */
  position: relative;
  z-index: 10;
  display: block;
}

button {
  margin-top: 10px;
}
代码语言:javascript
复制
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropit/0.5.1/jquery.cropit.js"></script>
<div class="image-editor">
   <input type="file" class="cropit-image-input">
   <div class="cropit-preview"></div>
   <input type="range" class="cropit-image-zoom-input">
   <button class="export">Export</button>
 </div>
<img id="image"/>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51926727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档