引入CSS与JS依赖文件
首先需要在页面中引入必要的CSS样式文件,以确保裁剪框的视觉效果正常显示。具体路径为 /js/jquery.imgareaselect-0.9.10/css/imgareaselect-default.css 。
/js/jquery.imgareaselect-0.9.10/css/imgareaselect-default.css
接着加载所需的JavaScript脚本文件。主要包含两个部分:一是 imgAreaSelect 插件的核心脚本 /js/jquery.imgareaselect-0.9.10/scripts/jquery.imgareaselect.min.js ,二是用于异步文件上传的工具脚本 /js/AjaxFileUploaderV2.1/ajaxfileupload.js 。
/js/jquery.imgareaselect-0.9.10/scripts/jquery.imgareaselect.min.js
/js/AjaxFileUploaderV2.1/ajaxfileupload.js
构建HTML结构与初始化裁剪
在HTML结构中,需要放置用于显示原图的元素以及用于展示裁剪预览的区域。通常包含一个隐藏的源图片标签和一个用于呈现裁剪结果的容器,同时可能包含一个表单用于提交裁剪数据。
使用jQuery代码初始化裁剪功能。当主图片加载完成后,调用 crop 函数。该函数计算缩放比例,即显示宽度与原图宽度的比值,以及高度方向的对应比值。随后调用 $img.imgAreaSelect 方法配置裁剪区域,设置初始坐标、句柄显示及宽高比等参数,并在选择变化时实时更新隐藏输入框中的坐标值。
$("#mainimg_src", content).load(function () {
crop($("#mainimg", content));
});
function crop($img) { //$img是
//缩小比例
var scalex =$img.width() / $("#mainimg_src").width();
var scaley =$img.height() / $("#mainimg_src").height();
$img.imgAreaSelect({
x1:0,y1:0,x2:150,y2:150,
handles: true, aspectRatio: '1:1',
onSelectEnd: function (img, selection) {
var scaleX = 100 / (selection.width || 1);
var scaleY = 100 / (selection.height || 1);
$('#mainimgthumb').css({
width: Math.round(scaleX * 400) + 'px',
height: Math.round(scaleY * 300) + 'px',
marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
});
$('input[name="x1"]', content).val(selection.x1/scalex);
$('input[name="y1"]', content).val(selection.y1/scaley);
$('input[name="x2"]', content).val(selection.x2/scalex);
$('input[name="y2"]', content).val(selection.y2/scaley);
}
});
}
前端AJAX提交与结果处理
当用户点击确定按钮提交表单时,拦截默认提交行为。通过 serializeArray 获取表单中的数据,包括选定的坐标信息。然后使用 $.get 方法将这些数据异步发送到服务器的裁剪接口。根据返回结果判断操作是否成功,若成功则提示用户,并更新页面上的图片显示,同时隐藏上传控件。
//确定剪切
$("form[name=fmCrop]", content).submit(function () {
var data = $("form[name=fmCrop]", content).serializeArray();
$.get('/members/web-members-rest/crop.html', data, function (r) {
switch (r.result) {
case "Success":
jAlert("操作成功");
$("#left_mainimg").attr("src", "/" + r.path + '?a=' + (new Date()).format('yyyyMMddhhmmss'));
$("#myPreview", content).css("display", "block");
$("#myPreview", content).prev().css("display", "none");
$("#mainimg", content).imgAreaSelect({ hide: true });
$("form[name=fmUpload]", content).css("display", "none");
$("input[name=btnSubmit]", content).css("display", "none");
break;
}
});
return false;
});
后端PHP裁剪逻辑实现
服务器端接收前端传来的坐标参数 src_path, x1, x2, y1, y2。首先获取图片路径,计算裁剪区域的宽度和高度。利用 exif_imagetype 函数检测图片类型,确保其为支持的 JPEG、PNG 或 GIF 格式。若不支持则报错退出。针对不同类型的图片,分别创建对应的图像资源并进行处理。
public function actionCrop($src_path,$x1,$x2,$y1,$y2){
$pic =$src_path;
$width = $x2-$x1;
$height = $y2-$y1;
$type=exif_imagetype($pic); //判断文件类型
$support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);
if(!in_array($type, $support_type,true)) {
echo "this type of image does not support! only support jpg , gif or png";
exit();
}
switch($type) {
case IMAGETYPE_JPEG :
$image = imagecreatefromjpeg($pic);
break;
case IMAGETYPE_PNG :
$image = imagecreatefrompng($pic);
break;
case IMAGETYPE_GIF :
$image = imagecreatefromgif($pic);
break;
default:
echo "Load image error!";
exit();
}
$copy = $this->PIPHP_ImageCrop($image, $x1, $y1, $width, $height);//裁剪
imagejpeg($copy, $src_path); //替换新图
return ['result'=>'Success','path'=>$src_path]; //返回新图地址
}
function PIPHP_ImageCrop($image, $x, $y, $w, $h){
$tw = imagesx($image);
$th = imagesy($image);
if ($x > $tw || $y > $th || $w > $tw || $h > $th) return FALSE;
$temp = imagecreatetruecolor($w, $h);
imagecopyresampled($temp, $image, 0, 0, $x, $y, $w, $h, $w, $h);
return $temp;
}
以上所述就是本文的全部内容了,希望大家能够喜欢。