`
天梯梦
  • 浏览: 13732824 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

jQuery 缩放 旋转 裁剪图片 Image Cropper

 
阅读更多

A simple jQuery image cropping plugin.

cropper是一款使用简单且功能强大的图片剪裁jQuery插件。该图片剪裁插件支持图片放大缩小,支持图片旋转,支持触摸屏设备,支持canvas,并且支持跨浏览器使用。

 

Features

  • Supports touch
  • Supports zoom
  • Supports rotation
  • Supports canvas
  • Supports options
  • Supports methods
  • Supports events
  • Supports multiple croppers
  • Supports RTL
  • Cross-browser support

 

Main

dist/
├── cropper.css     ( 5 KB)
├── cropper.min.css ( 4 KB)
├── cropper.js      (63 KB)
└── cropper.min.js  (23 KB)

 

Getting started

 

Quick start

Four quick start options are available:

  • Download the latest release.
  • Clone the repository: git clone https://github.com/fengyuanchen/cropper.git.
  • Install with NPM: npm install cropper.
  • Install with Bower: bower install cropper.

 

Installation

Include files:

<script src="/path/to/jquery.js"></script><!-- jQuery is required -->
<link  href="/path/to/cropper.css" rel="stylesheet">
<script src="/path/to/cropper.js"></script>

 

CDNJS

The CDNJS provides CDN support for Cropper's CSS and JavaScript. You can find the links here.

 

RawGit

<link  href="https://cdn.rawgit.com/fengyuanchen/cropper/v0.11.0/dist/cropper.min.css" rel="stylesheet">
<script src="https://cdn.rawgit.com/fengyuanchen/cropper/v0.11.0/dist/cropper.min.js"></script>

 

Usage

Initialize with $.fn.cropper method.

 

RawGit

<!-- Wrap the image or canvas element with a block element -->
<div class="container">
  <img src="picture.jpg">
</div>

 

$('.container > img').cropper({
  aspectRatio: 16 / 9,
  crop: function(e) {
    // Output the result data for cropping image.
    console.log(e.x);
    console.log(e.y);
    console.log(e.width);
    console.log(e.height);
    console.log(e.rotate);
    console.log(e.scaleX);
    console.log(e.scaleY);
  }
});

 

Notes

  • The size of the cropper inherits from the size of the image's parent element (wrapper), so be sure to wrap the image with a visible block element.
  • The outputted cropped data bases on the original image size, so you can use them to crop the image directly.
  • If you try to start cropper on a cross-origin image, please make sure that your browser supports HTML5 CORS settings attributes, and your image server supports the Access-Control-Allow-Origin option.

 

Known issues

  • About getCroppedCanvas method: The canvas.drawImage API in some Mac OS / iOS browsers will rotate an image with EXIF Orientation automatically, so the output cropped canvas may be incorrect. To fix this, you may upload the cropped data and crop the image in the server-side, see the example: Crop Avatar. Or you may handle the EXIF Orientation in server first before to use cropper.
  • Known iOS resource limits: As iOS devices limit memory, the browser may crash when you are cropping a large image (iPhone camera resolution). To avoid this, you may resize the image first (below 1024px) before start a cropper.

 

Options

You may set cropper options with $().cropper(options). If you want to change the global default options, You may use $.fn.cropper.setDefaults(options).

 

aspectRatio

  • Type: Number
  • Default: NaN

Set the aspect ratio of the crop box. By default, the crop box is free ratio.

 

data

  • Type: Object
  • Default: null

The previous cropped data if you had stored, will be passed to setData method automatically.

 

preview

  • Type: String (jQuery selector)
  • Default: ''

Add extra elements (containers) for previewing.

Notes:

  • The maximum width is the initial width of preview container.
  • The maximum height is the initial height of preview container.
  • If you set an aspectRatio option, be sure to set the preview container with the same aspect ratio.

 

strict

  • Type: Boolean
  • Default: true

In strict mode, the canvas (image wrapper) cannot be smaller than the container, and the crop box cannot be outside of the canvas (image wrapper).

 

responsive

  • Type: Boolean
  • Default: true

Rebuild the cropper when resize the window.

checkImageOrigin

  • Type: Boolean
  • Default: true

By default, the plugin will check the image origin, and if it is a cross-origin image, a crossOrigin attribute will be added to the image element and a timestamp will be added to the image url to reload the image for "getCroppedCanvas".

By adding crossOrigin attribute to image will stop adding timestamp to image url, and stop reload of image.

 

modal

  • Type: Boolean
  • Default: true

Show the black modal above the image and under the crop box.

 

guides

  • Type: Boolean
  • Default: true

Show the dashed lines above the crop box.

 

center

  • Type: Boolean
  • Default: true

Show the center indicator above the crop box.

 

highlight

  • Type: Boolean
  • Default: true

Show the white modal above the crop box (highlight the crop box).

 

background

  • Type: Boolean
  • Default: true

Show the grid background of the container.

 

autoCrop

  • Type: Boolean
  • Default: true

Enable to crop the image automatically when initialize.

 

autoCropArea

  • Type: Number
  • Default: 0.8 (80% of the image)

A number between 0 and 1. Define the automatic cropping area size (percentage).

 

dragCrop

  • Type: Boolean
  • Default: true

Enable to remove the current crop box and create a new one by dragging over the image.

 

movable

  • Type: Boolean
  • Default: true

Enable to move the image.

 

rotatable

  • Type: Boolean
  • Default: true

Enable to rotate the image.

 

zoomable

  • Type: Boolean
  • Default: true

Enable to zoom the image.

 

mouseWheelZoom

  • Type: Boolean
  • Default: true

Enable to zoom the image by wheeling mouse.

 

wheelZoomRatio

  • Type: Number
  • Default: 0.1

Define zoom ratio when zoom the image by wheeling mouse.

 

touchDragZoom

  • Type: Boolean
  • Default: true

Enable to zoom the image by dragging touch.

 

cropBoxMovable

  • Type: Boolean
  • Default: true

Enable to move the crop box.

 

cropBoxResizable

  • Type: Boolean
  • Default: true

Enable to resize the crop box.

 

doubleClickToggle

  • Type: Boolean
  • Default: true

Enable to toggle drag mode between "crop" and "move" when double click on the cropper.

 

minContainerWidth

  • Type: Number
  • Default: 200

The minimum width of the container.

 

minContainerHeight

  • Type: Number
  • Default: 100

The minimum height of the container.

 

minCanvasWidth

  • Type: Number
  • Default: 0

The minimum width of the canvas (image wrapper).

 

minCanvasHeight

  • Type: Number
  • Default: 0

The minimum height of the canvas (image wrapper).

 

minCropBoxWidth

  • Type: Number
  • Default: 0

The minimum width of the crop box.

 

minCropBoxHeight

  • Type: Number
  • Default: 0

The minimum height of the crop box.

 

build

  • Type: Function
  • Default: null

A shortcut of the "build.cropper" event.

 

built

  • Type: Function
  • Default: null

A shortcut of the "built.cropper" event.

 

cropstart

  • Type: Function
  • Default: null

A shortcut of the "cropstart.cropper" event.

 

cropmove

  • Type: Function
  • Default: null

A shortcut of the "cropmove.cropper" event.

 

cropend

  • Type: Function
  • Default: null

A shortcut of the "cropend.cropper" event.

 

crop

  • Type: Function
  • Default: null

A shortcut of the "crop.cropper" event.

zoom

  • Type: Function
  • Default: null

A shortcut of the "zoom.cropper" event.

Methods

As there is an asynchronous process when load the image, you should call most of the methods after built, except “setAspectRatio”, “replace” and “destroy”.

$().cropper({
  built: function () {
    $().cropper('method', argument1, , argument2, ..., argumentN);
  }
});
 

 

crop()

Show the crop box manually.

$().cropper({
  autoCrop: false,
  built: function () {
    // Do something here
    // ...

    // And then
    $(this).cropper('crop');
  }
});
 

 

reset()

Reset the image and crop box to their initial states.

clear()

Clear the crop box.

replace(url)

  • url:
    • Type: String
    • A new image url.

Replace the image’s src and rebuild the cropper.

enable()

Enable (unfreeze) the cropper.

disable()

Disable (freeze) the cropper.

destroy()

Destroy the cropper and remove the instance from the image.

move(offsetX[, offsetY])

  • offsetX:
    • Type: Number
    • Default: 0
    • Moving size (px) in the horizontal direction.
  • offsetY (optional):
    • Type: Number
    • Moving size (px) in the vertical direction.
    • If not present, its defualt value is offsetX.

Move the canvas (image wrapper).

$().cropper('move', 1);
$().cropper('move', 1, 0);
$().cropper('move', 0, -1);
 

 

zoom(ratio)

  • ratio:
    • Type: Number
    • Zoom in: requires a positive number (ratio > 0)
    • Zoom out: requires a negative number (ratio < 0)

Zoom the canvas (image wrapper).

$().cropper('zoom', 0.1);
$().cropper('zoom', -0.1);
 

 

rotate(degree)

  • degree:
    • Type: Number
    • Rotate right: requires a positive number (degree > 0)
    • Rotate left: requires a negative number (degree < 0)

Rotate the canvas (image wrapper).

Requires CSS3 2D Transforms support (IE 9+).

$().cropper('rotate', 90);
$().cropper('rotate', -90);

 

scale(scaleX[, scaleY])

  • scaleX:
    • Type: Number
    • Default: 1
    • The scaling factor to apply on the abscissa of the image.
    • When equal to 1 it does nothing.
  • scaleY (optional):
    • Type: Number
    • The scaling factor to apply on the ordinate of the image.
    • If not present, its defualt value is scaleX.

Scale the image.

Requires CSS3 2D Transforms support (IE 9+).

$().cropper('scale', -1); // Flip both horizontal and vertical
$().cropper('scale', -1, 1); // Flip horizontal
$().cropper('scale', 1, -1); // Flip vertical
 

getData([rounded])

  • rounded (optional):
    • Type: Boolean
    • Default: false
    • Set true to get rounded values.
  • (return value):
    • Type: Object
    • Properties:
      • x: the offset left of the cropped area
      • y: the offset top of the cropped area
      • width: the width of the cropped area
      • height: the height of the cropped area
      • rotate: the rotated degrees of the image
      • scaleX: the scaling factor to apply on the abscissa of the image
      • scaleY: the scaling factor to apply on the ordinate of the image

Output the cropped area position and size data (base on the original image).

Image Cropper

setData(data)

  • data:
    • Type: Object
    • Properties: See the getData method.

Change the cropped area position and size with new data (base on the original image).

Note: Only available in strict mode.

getContainerData()

  • (return value):
    • Type: Object
    • Properties:
      • width: the current width of the container
      • height: the current height of the container

Output the container size data.

Image Cropper

getImageData()

  • (return value):
    • Type: Object
    • Properties:
      • left: the offset left of the image
      • top: the offset top of the image
      • width: the width of the image
      • height: the height of the image
      • naturalWidth: the natural width of the image
      • naturalHeight: the natural height of the image
      • aspectRatio: the aspect ratio of the image
      • rotate: the rotated degrees of the image if rotated
      • scaleX: the scaling factor to apply on the abscissa of the image if scaled
      • scaleY: the scaling factor to apply on the ordinate of the image if scaled

Output the image position, size and other related data.

getCanvasData()

  • (return value):
    • Type: Object
    • Properties:
      • left: the offset left of the canvas
      • top: the offset top of the canvas
      • width: the width of the canvas
      • height: the height of the canvas

Output the canvas (image wrapper) position and size data.

setCanvasData(data)

  • data:
    • Type: Object
    • Properties:
      • left: the new offset left of the canvas
      • top: the new offset top of the canvas
      • width: the new width of the canvas
      • height: the new height of the canvas

Change the canvas (image wrapper) position and size with new data.

getCropBoxData()

  • (return value):
    • Type: Object
    • Properties:
      • left: the offset left of the crop box
      • top: the offset top of the crop box
      • width: the width of the crop box
      • height: the height of the crop box

Output the crop box position and size data.

setCropBoxData(data)

  • data:
    • Type: Object
    • Properties:
      • left: the new offset left of the crop box
      • top: the new offset top of the crop box
      • width: the new width of the crop box
      • height: the new height of the crop box

Change the crop box position and size with new data.

getCroppedCanvas([options])

  • options (optional):
    • Type: Object
    • Properties:
      • width: the destination width of the output canvas
      • height: the destination height of the output canvas
      • fillColor: a color to fill any alpha values in the output canvas
  • (return value):
    • Type: HTMLCanvasElement
    • A canvas drawn the cropped image.
  • Browser support:

Get a canvas drawn the cropped image.

 

After then, you can display the canvas as an image directly, or use canvas.toDataURL to get a Data URL, or use canvas.toBlob to get a blob and upload it to server with FormData if the browser supports these APIs.

 

 

$().cropper('getCroppedCanvas');

$().cropper('getCroppedCanvas', {
  width: 160,
  height: 90
});

// Upload cropped image to server
$().cropper('getCroppedCanvas').toBlob(function (blob) {
  var formData = new FormData();

  formData.append('croppedImage', blob);

  $.ajax('/path/to/upload', {
    method: "POST",
    data: formData,
    processData: false,
    contentType: false,
    success: function () {
      console.log('Upload success');
    },
    error: function () {
      console.log('Upload error');
    }
  });
});
 

 

setAspectRatio(aspectRatio)

  • aspectRatio:
    • Type: Number
    • Requires a positive number.

Change the aspect ratio of the crop box.

setDragMode([mode])

  • mode (optional):
    • Type: String
    • Default: 'none'
    • Options: 'none', 'crop', 'move'

Change the drag mode.

Tips: You can toggle the “crop” and “move” mode by double click on the cropper.

Events

build.cropper

This event fires when a cropper instance starts to load an image.

built.cropper

This event fires when a cropper instance has built completely.

cropstart.cropper

  • event.originalEvent:
    • Type: Event
    • Options: mousedown, touchstart and pointerdown
  • event.action:
    • Type: String
    • Options:
      • 'crop': create a new crop box
      • 'move': move the canvas (image wrapper)
      • 'zoom': zoom in / out the canvas (image wrapper) by touch.
      • 'e': resize the east side of the crop box
      • 'w': resize the west side of the crop box
      • 's': resize the south side of the crop box
      • 'n': resize the north side of the crop box
      • 'se': resize the southeast side of the crop box
      • 'sw': resize the southwest side of the crop box
      • 'ne': resize the northeast side of the crop box
      • 'nw': resize the northwest side of the crop box
      • 'all': move the crop box (all directions)

This event fires when the canvas (image wrapper) or the crop box starts to change.

$().on('cropstart.cropper', function (e) {
  console.log(e.type); // cropstart
  console.log(e.namespace); // cropper
  console.log(e.action); // ...
  console.log(e.originalEvent.pageX);

  // Prevent to start cropping, moving, etc if necessary
  if (e.action === 'crop') {
    e.preventDefault();
  }
});

 

cropmove.cropper

  • event.originalEvent:
    • Type: Event
    • Options: mousemove, touchmove and pointermove.
  • event.action: the same as “cropstart.cropper”.

This event fires when the canvas (image wrapper) or the crop box is changing.

cropend.cropper

  • event.originalEvent:
    • Type: Event
    • Options: mouseup, touchend, touchcancel, pointerup and pointercancel.
  • event.action: the same as “cropstart.cropper”.

This event fires when the canvas (image wrapper) or the crop box stops to change.

crop.cropper

  • event.x
  • event.y
  • event.width
  • event.height
  • event.rotate
  • event.scaleX
  • event.scaleY

About these properies, see the getData method.

This event fires when the canvas (image wrapper) or the crop box changed.

zoom.cropper

  • event.originalEvent:
    • Type: Event
    • Options: wheel, touchmove.
  • event.ratio:
    • Type: Number
    • The current zoom ratio (ratio > 0 means zoom in, ratio < 0 means zoom out)

This event fires when a cropper instance starts to zoom in or zoom out its canvas (image wrapper).

$().on('zoom.cropper', function (e) {
  var maxRatio = 10;
  var imageData = $(this).cropper('getImageData');
  var currentRatio = imageData.width / imageData.naturalWidth;

  // Zoom in
  if (e.ratio > 0 && currentRatio > maxRatio) {

    // Prevent zoom in
    e.preventDefault();

    // Fit the max zoom ratio
    $(this).cropper('setCanvasData', {
      width: imageData.naturalWidth * maxRatio
    });
  }

  // Zoom out
  // ...
});

 

No conflict

If you have to use other plugin with the same namespace, just call the $.fn.cropper.noConflict method to revert to it.

<script src="other-plugin.js"></script>
<script src="cropper.js"></script>
<script>
  $.fn.cropper.noConflict();
  // Code that uses other plugin's "$().cropper" can follow here.
</script>

 

Browser support

  • Chrome (latest 2)
  • Firefox (latest 2)
  • Internet Explorer 8+
  • Opera (latest 2)
  • Safari (latest 2)

As a jQuery plugin, you also need to see the jQuery Browser Support.

 

Related projects

 

主页:http://fengyuanchen.github.io/cropper/

下载:cropper-master

项目地址:https://github.com/fengyuanchen/cropper

转自:jQuery 缩放 旋转 裁剪图片 Image Cropper

 

 

 

分享到:
评论

相关推荐

    JQuery 图片剪切分割上传 - ImageCropper

    - **图片剪切**:ImageCropper 提供了强大的图片剪切功能,用户可以通过拖动和调整矩形框来选择需要的部分,支持矩形的缩放,确保用户可以精确地裁剪出想要的区域。 - **图片分割**:除了剪切,该插件还支持图片的...

    基于Jquery 的图片裁剪插件cropper

    **基于jQuery的图片裁剪插件Cropper** 在网页开发中,经常需要处理用户上传的图片,例如调整尺寸、旋转或裁剪以适应特定的显示需求。这时,一个高效的图片裁剪工具就显得尤为重要。"基于jQuery的图片裁剪插件...

    基于jquery的图片裁剪插件

    许多图片裁剪插件还支持高级特性,如旋转、缩放、镜像、拖拽等。同时,可以通过API自定义UI、设置裁剪限制、处理多张图片等,以满足各种复杂场景的需求。 综上所述,基于jQuery的图片裁剪插件通过结合HTML5的Canvas...

    Jquery的Cropper头像剪切

    `jQuery Cropper` 是一个强大的JavaScript图像裁剪插件,它基于HTML5的Canvas元素,允许用户在网页上自由地拖动选择并裁剪图片,尤其适用于创建头像剪切功能。本篇将详细介绍如何使用 `jQuery Cropper` 实现头像剪切...

    image-cropper.zip 压缩包

    "image-cropper.zip"压缩包中的主要组件是名为"image-cropper"的资源,这通常指的是一个图像裁剪工具或库。在IT行业中,图像裁剪是图像处理的一个重要部分,它允许用户根据需求选择并提取图像的特定部分。这种功能...

    jquery 图片裁剪

    - **JQuery Cropper**:这是一款基于 jQuery 的图片裁剪插件,提供了丰富的配置选项和方法,如设置裁剪框大小、旋转角度、比例等,支持触控设备,可以轻松实现图片裁剪功能。 - **jQuery Image Crop**:另一个常用...

    基于jquery/cropper插件裁剪头像demo

    此外,Cropper.js还提供了一些实用的API方法,如`crop()`用于立即裁剪当前视图,`rotate()`用于旋转图片,`scale()`用于缩放图片等。 在头像裁剪过程中,用户通常需要提交裁剪后的结果。这时,可以利用Cropper.js的...

    cropper.js图片裁剪

    压缩包中的`cropper-master`文件夹包含了`example`目录,这里提供了各种功能的实例代码,例如基本的裁剪、旋转、缩放、比例锁定等。这些示例可以帮助开发者快速理解和使用`cropper.js`。同时,`cropper.js`通常会有...

    php+jquery图片裁剪代码.

    jQuery Cropper是一款强大的图片裁剪库,它基于HTML5的Canvas元素,提供了丰富的API和事件,可以方便地实现图片的预览、旋转、缩放和裁剪。在"cropper"文件中,可能包含了这个插件的源码和示例。使用jQuery Cropper...

    jQuery的图片裁剪插件集合

    3. jQuery Image Crop:简单易用,提供基本的裁剪、缩放和旋转功能,适合快速实现裁剪需求。 4. imgAreaSelect:一个简单但灵活的插件,允许用户通过鼠标绘制裁剪区域,可与其他jQuery插件配合使用。 5. Bootstrap...

    基于jqueryajax+cropper+springboot的图片裁剪和存储程序.zip

    它提供了图片的缩放、旋转、裁剪等操作,用户可以通过可视化的方式调整图片大小和形状,然后将裁剪结果传递给服务器。 3. **Spring Boot**: Spring Boot是Java开发的一个微服务框架,简化了创建独立的、生产级别...

    基于jquerycropperjs裁剪插件

    - **方法**:如`.crop()`进行裁剪操作,`.rotate(degree)`旋转图片,`.zoom(factor)`缩放图片,`.getData()`获取裁剪数据等。 ### 四、与后端交互 在实际项目中,你可能需要将裁剪的结果发送到服务器。可以通过`...

    jquery上传裁剪图片

    在IT领域,jQuery是一个...综上所述,使用jQuery和Cropper.js,我们可以实现图片上传、预览和裁剪功能,并确保兼容各大浏览器。在整个过程中,注意前端与后端的交互,以及浏览器兼容性的处理,将使功能更加完善和健壮。

    jQuery实现的对上传图片进行裁剪插件

    3. 裁剪操作:提供拖动、缩放等交互方式,让用户自由裁剪图片,满足特定尺寸或比例的要求。 4. 参数设置:开发者可以配置裁剪区域的大小、比例、位置等参数,以适应不同的应用场景。 5. 数据提交:裁剪完成后,插件...

    angular-image-cropper, 用于裁剪图像的AngularJS指令.zip

    angular-image-cropper, 用于裁剪图像的AngularJS指令 Angular 图像 cropper实时示例要查看一个实时示例,请转到演示文档的页面...描述Angular 图像 cropper inspired受欢迎的裁剪jQuery插件,允许收费 缩放或者旋转图

    jquery进行图片裁剪

    JQuery Cropper是一款强大的基于jQuery的图片裁剪插件,它提供了丰富的API和事件,易于集成到项目中。首先,你需要在项目中引入jQuery和Cropper.js的相关文件。然后,创建一个图片元素,通过`cropper()`方法初始化...

    jquery图片预览与裁剪

    这些插件提供了丰富的选项和事件,使得开发者可以定制裁剪界面,设置比例,甚至实现旋转、缩放等功能。 关于兼容性,由于IE6至9等旧版本浏览器对HTML5新特性支持不足,实现图片预览和裁剪功能时需要额外的工作。...

    cropper基本用法.pdf

    5. 裁剪操作:用户可以通过拖动、缩放或旋转来调整裁剪区域,这些动作可以通过设置`dragMode`和配置相应的操作键来实现。 6. 预览和更新:预览区域的图片可以通过`preview`选项自动更新,或者手动调用`cropper对象....

    jquery实现的图片裁剪插件

    在本文中,我们将深入探讨基于jQuery的图片裁剪插件,这是一种广泛应用于Web开发中的工具,用于实现用户交互式的图片裁剪功能。jQuery是JavaScript的一个轻量级库,它简化了DOM操作、事件处理和动画制作,使得创建...

Global site tag (gtag.js) - Google Analytics