引用CSS
/js/jquery.imgareaselect-0.9.10/css/imgareaselect-default.css
引用js
/js/jquery.imgareaselect-0.9.10/scripts/jquery.imgareaselect.min.js /js/AjaxFileUploaderV2.1/ajaxfileupload.js
html
<div> <img src="/static/imghwm/default1.png" data-src="blank.jpg" class="lazy" id="mainimg" alt="php结合imgareaselect实现图片裁剪_jquery" > </div> <div id="myPreview" > <img src="/static/imghwm/default1.png" data-src="blank.jpg" class="lazy" id="mainimgthumb" / alt="php结合imgareaselect实现图片裁剪_jquery" > </div> <form name="fmCrop"> <input type="hidden" name="src_path" value="" /> <input type="hidden" name="x1" value="" /> <input type="hidden" name="x2" value="" /> <input type="hidden" name="y1" value="" /> <input type="hidden" name="y2" value="" /> <input type="submit" name="btnSubmit" class="baseinf_but1" style="display:none" value="确定" /> </form>
jQuery代码
$("#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); } }); }
确定裁剪
//确定剪切 $("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代码
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; }
以上所述就是本文的全部内容了,希望大家能够喜欢。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。