jquery.Jcrop API详解

Jcrop是一个jQuery图片裁剪插件,它能为你的WEB应用程序快速简单地提供图片裁剪的功能。特点如下:

  • 对所有图片均unobtrusively(无侵入的,保持DOM简洁)
  • 支持宽高比例锁定
  • 支持 minSize/maxSize设置
  • 支持改变选区或移 动选区时的回调(Callback)
  • 支持用键盘微调选区
  • 通过API创建互动,比如动画效果
  • 支持CSS样式
    官网:http://jcrop.org/
    可下载例子进行学习
    基本使用方法如下:
    一、在head部分(和之间)插入相关css和js文件。
<link rel="stylesheet" href="css/jquery.Jcrop.css">
<script src="js/jquery.js"></script> 
<script src="js/jquery.Jcrop.js"></script>

二、在head部分插入回调函数等相关处理参数。

<script language="Javascript">
  <!--
  jQuery(function($){
 // Create variables (in this scope) to hold the API and image size
  var jcrop_api, boundx, boundy;

  $('#cropbox').Jcrop({
  minSize: [0,0],
  maxSize:[0,0],
  setSelect: [0,0,0,0],
  boxWidth:800,
  borderOpacity:0.3,
  keySupport:false,
  dragEdges:false,
  allowSelect:false,
  allowResize:false,
  bgOpacity:0.2,
  boundary:0,
  //allowMove:false,
  addClass: 'jcrop-handle',
  onSelect: updateCoords,
  },
  function(){
  // Use the API to get the real image size
  var bounds = this.getBounds();
  boundx = bounds[0];
  boundy = bounds[1];
  // Store the API in the jcrop_api variable
  jcrop_api = this;
  });
  function updateCoords(c)
  {
  $('#x').val(c.x);
  $('#y').val(c.y);
  $('#w').val(c.w);
  $('#h').val(c.h);
  };
  function checkCoords()
  { 
  if (parseInt($('#w').val())) return true;
  alert('请选择裁剪区域');
  return false;
  }; 
  });
-->
</script>

三、给相关图片加上id以便识别。

<img id="cropbox" src="x.jpg">

这样就能实现最简单的裁剪效果,至于如何结合php等动态语句处理图片,在置顶的文章里已经给出了示例。
下表给出基本options参数设置:

名称默认值说明
allowSelecttrue允许新选框
allowMovetrue允许选框移动
allowResizetrue允许选框缩放
trackDocumenttrue
baseClass“jcrop”基础样式名前缀。说明:class=”jcrop-holder”,更改的只是其中的 jcrop。
addClassnull添加样式会。例:假设值为 “test”,那么会添加样式到 class=”test jcrop-holder”
bgColor“black”背景颜色。颜色关键字、HEX、RGB 均可。
bgOpacity0.6背景透明度
bgFadefalse使用背景过渡效果
borderOpacity0.4选框边框透明度
handleOpacity0.5缩放按钮透明度
handleSize9缩放按钮大小
handleOffset5缩放按钮与边框的距离
aspectRatio0选框宽高比。说明:width/height
keySupporttrue支持键盘控制。按键列表:上下左右(移动)、Esc(取消)、Tab(跳出裁剪框,到下一个)
cornerHandlestrue允许边角缩放
sideHandlestrue允许四边缩放
drawBorderstrue绘制边框
dragEdgestrue允许拖动边框
fixedSupporttrue
touchSupportnull
boxWidth0画布宽度
boxHeight0画布高度
boundary2边界。说明:可以从边界开始拖动鼠标选择裁剪区域
fadeTime400过度效果的时间
animationDelay20动画延迟
swingSpeed3过渡速度
minSelect[0,0]选框最小选择尺寸。说明:若选框小于该尺寸,则自动取消选择
maxSize[0,0]选框最大尺寸
minSize[0,0]选框最小尺寸
onChangefunction(){}选框改变时的事件
onSelectfunction(){}选框选定时的事件
onReleasefunction(){}取消选框时的事件

下表是api方法

名称说明
setImage(string)设定(或改变)图像。例:jcrop_api.setImage(“newpic.jpg”)
setOptions(object)设定(或改变)参数,格式与初始化设置参数一样
setSelect(array)创建选框,参数格式为:[x,y,x2,y2]
animateTo(array)用动画效果创建选框,参数格式为:[x,y,x2,y2]
release()取消选框
disable()禁用 Jcrop。说明:已有选框不会被清除。
enable()启用 Jcrop
destroy()移除 Jcrop
tellSelect()获取选框的值(实际尺寸)。例子:console.log(jcrop_api.tellSelect())
tellScaled()获取选框的值(界面尺寸)。例子:console.log(jcrop_api.tellScaled())
getBounds()获取图片实际尺寸,格式为:[w,h]
getWidgetSize()获取图片显示尺寸,格式为:[w,h]
getScaleFactor()获取图片缩放的比例,格式为:[w,h]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,这里提供一个基于jcrop库的图片裁剪演示,您可以在下面的代码框中运行它,体验一下: ```html <!DOCTYPE html> <html> <head> <title>Jcrop 图片裁剪演示</title> <meta charset="utf-8"> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.15/css/jquery.Jcrop.min.css" /> <script src="//code.jquery.com/jquery-3.6.0.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.15/js/jquery.Jcrop.min.js"></script> <style> #preview { width: 200px; height: 200px; overflow: hidden; } </style> </head> <body> <h1>Jcrop 图片裁剪演示</h1> <form method="post" action="crop.php"> <input type="hidden" name="image" value="path/to/image.jpg"> <img src="path/to/image.jpg" id="cropbox"> <div id="preview"></div> <p> <button type="submit">保存裁剪结果</button> </p> </form> <script> $(function() { $('#cropbox').Jcrop({ aspectRatio: 1, boxWidth: 500, onChange: updatePreview, onSelect: updatePreview }); }); function updatePreview(coords) { var rx = 200 / coords.w; var ry = 200 / coords.h; $('#preview').css({ width: Math.round(rx * $('#cropbox').width()) + 'px', height: Math.round(ry * $('#cropbox').height()) + 'px', marginLeft: '-' + Math.round(rx * coords.x) + 'px', marginTop: '-' + Math.round(ry * coords.y) + 'px' }); $('input[name="x"]').val(coords.x); $('input[name="y"]').val(coords.y); $('input[name="width"]').val(coords.w); $('input[name="height"]').val(coords.h); } </script> </body> </html> ``` 这段代码会加载jcrop库,并在页面中显示一张图片,您可以使用鼠标拖动裁剪框来选择需要裁剪的区域。当您选择好区域后,可以点击“保存裁剪结果”按钮,将裁剪结果提交给服务器。 为了使代码正常工作,您需要将上面代码中的 `path/to/image.jpg` 修改为您自己的图片路径,并创建一个名为 `crop.php` 的PHP脚本来处理裁剪结果。下面是一个裁剪处理脚本的示例: ```php // 获取裁剪参数 $x = $_POST['x']; $y = $_POST['y']; $width = $_POST['width']; $height = $_POST['height']; // 打开原始图像 $src = imagecreatefromjpeg($_POST['image']); // 创建新图像并复制裁剪区域 $dest = imagecreatetruecolor($width, $height); imagecopy($dest, $src, 0, 0, $x, $y, $width, $height); // 保存新图像 imagejpeg($dest, 'path/to/cropped/image.jpg'); // 释放内存 imagedestroy($src); imagedestroy($dest); ``` 这个脚本会从POST参数中获取裁剪参数和原始图像路径,然后使用GD库创建新图像并保存。请注意,上述代码仅供参考,并且需要根据具体情况进行适当的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值