jQuery+PHP實(shí)現(xiàn)上傳裁剪圖片
本文是一個(gè)簡單的jquery圖片預(yù)覽+裁剪的例子,原理是在前端獲取要裁剪的信息,如寬高比、裁剪坐標(biāo),上傳圖片之后在后端php進(jìn)行切割
jquery代碼(必須在最后面引入)
function showCutImg(showImg){ var showImg = $(showImg); var changeInput = showImg.parents('.showImgDiv').siblings('.CutImage'); var size = changeInput.siblings('.imgCoord').attr('ratio').split('*'); var needWidth = size[0]; var needHeight = size[1]; var ratio = parseInt(needWidth)/parseInt(needHeight); ratio = parseFloat(ratio.toFixed(2)); var thisFullDiv = showImg.parent(); var coordArr = changeInput.siblings('.imgCoord').val().split(','); thisCutImgWidth = showImg.width(); thisCutImgHeight = showImg.height() thisFullDiv.css('width',thisCutImgWidth); thisFullDiv.css('height',thisCutImgHeight); if((thisCutImgWidth/thisCutImgHeight)>=ratio){ var thisCutDivHeight = thisCutImgHeight; var thisCutDivWidth = thisCutDivHeight*ratio; }else{ var thisCutDivWidth = thisCutImgWidth; var thisCutDivHeight = thisCutDivWidth/ratio; } var hideWidth = (thisFullDiv.width()-thisCutDivWidth)/2; showImg.siblings('.hideImgLeft').width(hideWidth); showImg.siblings('.hideImgRight').width(hideWidth); var hideHeight = (thisFullDiv.height()-thisCutDivHeight)/2; showImg.siblings('.hideImgTop').width(thisCutDivWidth); showImg.siblings('.hideImgBottom').width(thisCutDivWidth); showImg.siblings('.hideImgTop').height(hideHeight); showImg.siblings('.hideImgBottom').height(hideHeight); if(hideWidth>0){ var cutRatioX = thisCutImgWidth/hideWidth; }else{ var cutRatioX = 0 } if(hideHeight>0){ var cutRatioY = thisCutImgHeight/hideHeight; }else{ var cutRatioY = 0; } var coord = needWidth+'#'+needHeight+'#'+(cutRatioX)+'#'+(cutRatioY); if(coordArr!=''){ coordArr.push(coord); }else{ coordArr = [coord]; } changeInput.siblings('.imgCoord').val(coordArr); $('.fullDiv').on('mousedown',function(e){ var me = $(this); var changeInput = me.parent().siblings('.CutImage'); var index = me.attr('index'); var oldx = e.pageX; var oldy = e.pageY; var imgleft = me.children('.cutImg').position().left; var imgtop = me.children('.cutImg').position().top; var maxw = me.children('.hideImgLeft').width(); var maxh = me.children('.hideImgTop').height(); var goordArr = changeInput.siblings('.imgCoord').val().split(','); var cutDivSize = goordArr[index].split('#'); $(document).mousemove(function(e){ var newx = e.pageX; var newy = e.pageY; var movex = newx - oldx; var movey = newy - oldy; var x = movex + imgleft; var y = movey + imgtop; if(Math.abs(x)>maxw){ if(x>0) x = maxw; if(x<0) x = -maxw; } if(Math.abs(y)>maxh){ if(y>0) y = maxh; if(y<0) y = -maxh; } me.children('.cutImg').css('left',x+'px'); me.children('.cutImg').css('top',y+'px'); if(parseInt(maxw - x)>0){ var cutRatioX = me.children('.cutImg').width()/parseInt(maxw - x); }else{ var cutRatioX = 0; } if(parseInt(maxh - y)>0){ var cutRatioY = me.children('.cutImg').height()/parseInt(maxh - y) }else{ var cutRatioY = 0; } var cutImgPo = (cutRatioX) +'#'+ (cutRatioY); var coordVal = cutDivSize[0]+'#'+cutDivSize[1]+'#'+cutImgPo; goordArr[index] = coordVal; changeInput.siblings('.imgCoord').val(goordArr); }); }); $(document).on('mouseup',function(e){ $(document).unbind('mousemove'); }); } $(".CutImage").change(function(){ $(this).siblings('.imgCoord').val(''); if($(this).prop('multiple')!=true){ //判斷是否多文件上傳 var objUrl = getObjectURL1(this.files[0]) ; var showImgWidth = $(this).siblings('.showImgDiv').attr('showImgWidth'); if(!showImgWidth) { showImgWidth = '150'; } if (objUrl) { html = ''; html += '<div style="border:1px solid #000;position:relative;z-index:2;overflow:hidden;cursor:move;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;" index="0" class="fullDiv">'; html += '<div style="position:absolute;background:#ccc;top:0;z-index:4;opacity:0.95;left:0;right:0;margin:auto;" class="hideImgTop"></div>'; html += '<div style="position:absolute;background:#ccc;bottom:0;z-index:4;opacity:0.95;left:0;right:0;margin:auto;" class="hideImgBottom"></div>'; html += '<div style="position:absolute;height:100%;background:#ccc;left:0;z-index:4;opacity:0.95;" class="hideImgLeft"></div><div style="position:absolute;z-index:3;left:0;right:0;top:0;bottom:0;margin:auto;" class="cutDiv"></div>'; html += '<div style="position:absolute;height:100%;background:#ccc;right:0;z-index:4;opacity:0.95;" class="hideImgRight"></div>'; html += '<img style="position:absolute;z-index:1;width:'+showImgWidth+'px" onload="showCutImg(this)" class="cutImg" class="imgshover" src="'+objUrl+'" alt="圖片加載失敗" />'; html += '</div>'; $(this).siblings('.showImgDiv').html(html); } }else{ var objUrl = getObjectURL2($(this).get(0).files); if (objUrl) { var showImgWidth = $(this).siblings('.showImgDiv').attr('showImgWidth'); if(!showImgWidth) { showImgWidth = '150'; } var html = ''; for(var i=0;i<objUrl.length;i++) { html += '<div style="margin-bottom:5px;border:1px solid #000;position:relative;z-index:2;overflow:hidden;cursor:move;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;" index="'+i+'" class="fullDiv">'; html += '<div style="position:absolute;background:#ccc;top:0;z-index:4;opacity:0.95;left:0;right:0;margin:auto;" class="hideImgTop"></div>'; html += '<div style="position:absolute;background:#ccc;bottom:0;z-index:4;opacity:0.95;left:0;right:0;margin:auto;" class="hideImgBottom"></div>'; html += '<div style="position:absolute;height:100%;background:#ccc;left:0;z-index:4;opacity:0.95;" class="hideImgLeft"></div><div style="position:absolute;z-index:3;left:0;right:0;top:0;bottom:0;margin:auto;" class="cutDiv"></div>'; html += '<div style="position:absolute;height:100%;background:#ccc;right:0;z-index:4;opacity:0.95;" class="hideImgRight"></div>'; html += '<img style="position:absolute;z-index:1;width:'+showImgWidth+'px" onload="showCutImg(this)" class="cutImg" class="imgshover" src="'+objUrl[i]+'" alt="圖片加載失敗" />'; html += '</div>'; //修改img標(biāo)簽的width樣式可改變預(yù)覽圖大小 } $(this).siblings('.showImgDiv').html(html); } //$('.fullDiv').css('float','left'); } }) ; //建立一個(gè)可存取到該file的url function getObjectURL1(file) { var url = null ; if (window.createObjectURL!=undefined) { // basic url = window.createObjectURL(file) ; } else if (window.URL!=undefined) { // mozilla(firefox) url = window.URL.createObjectURL(file) ; } else if (window.webkitURL!=undefined) { // webkit or chrome url = window.webkitURL.createObjectURL(file) ; } return url ; } //建立一個(gè)可存取到該file的url function getObjectURL2(file) { var url = new Array(); if (window.createObjectURL!=undefined) { // basic for(var i=0;i<file.length;i++) { url[i] = window.createObjectURL(file[i]) ; } } else if (window.URL!=undefined) { // mozilla(firefox) for(var i=0;i<file.length;i++) { url[i] = window.URL.createObjectURL(file[i]) ; } } else if (window.webkitURL!=undefined) { // webkit or chrome for(var i=0;i<file.length;i++) { url[i] = window.webkitURL.createObjectURL(file[i]) ; } } return url ; }
html代碼(這些代碼要放在同一級)
<!-- 文件上傳標(biāo)簽,添加class屬性CutImage --> <input class="CutImage" type="file" name="img" /> <!-- 傳送圖片裁剪比例等參數(shù),要添加class屬性imgCoord,ratio為裁剪后要保存的寬高width*height --> <input ratio="100*100" type="hidden" class="imgCoord" name="imgCoord"> <!-- 圖片預(yù)覽,要添加class屬性showImgDiv,showImgWidth表示預(yù)覽時(shí)的寬度 --> <div showImgWidth="100" class="showImgDiv"></div>
php代碼
/*圖片上傳代碼略 下面直接進(jìn)行圖片裁剪*/ /** * [cut_img 圖片裁剪函數(shù)] * Author: 程威明 * @param array $imgs 圖片路徑數(shù)組 * @param array $info 裁剪信息數(shù)組,包括裁剪后要保存的寬高、圖片大小與裁剪開始坐標(biāo)之比 * @param bool $cover 是否覆蓋原圖,默認(rèn)不覆蓋 * @return array 若覆蓋原圖返回裁剪數(shù)量,不覆蓋返回圖片路徑組成的數(shù)組 */ function cut_img($imgs=array(),$infoarr=null,$cover=false) { if($infoarr==null) return $imgs; //判斷是否為數(shù)組(必須是一個(gè)以圖片路徑組成的數(shù)組) $imgs = is_array($imgs)?$imgs:array($imgs); //把多個(gè)裁剪信息切成單個(gè)信息組成的數(shù)組 $infoarr = explode(',', $infoarr); $save_file = array(); $i=0; foreach($imgs as $file){ //如果不覆蓋原圖,可重新定義圖片保存路徑 if(false==$cover){ $file = $file; } //把裁剪信息切割成數(shù)組,第一個(gè)為要保存的寬第二個(gè)為要保存的高,第三和第四個(gè)為圖片寬高與裁剪起點(diǎn)的比例 $info = explode('#', $infoarr[$i]); //裁剪寬高比 $ratio = $info[0]/$info[1]; //判斷圖片是否存在 if(!file_exists($file)) continue; //獲取圖片信息 $imgize = getimagesize($file); //圖片寬度 $width = $imgize[0]; //圖片高度 $height = $imgize[1]; //圖片裁剪起點(diǎn)坐標(biāo) $x = $info[2]==0?0:$imgize[0]/$info[2]; $y = $info[3]==0?0:$imgize[1]/$info[3]; //判斷圖片原寬高比與裁剪寬高比的大小 if($width/$height>=$ratio){ $width = $height*$ratio;//如大于即為裁剪寬度 }else{ $height = $width/$ratio;//如小于即為裁剪高度 } //裁剪的寬高不能超出圖片大小 if(($width+$x)>$imgize[0]){ $width = $width-($width+$x-$imgize[0]); } if(($height+$y)>$imgize[1]){ $height = $height-($height+$y-$imgize[1]); } //創(chuàng)建源圖的實(shí)例 $src = imagecreatefromstring(file_get_contents($file)); //創(chuàng)建一個(gè)圖像 $new_image = imagecreatetruecolor($info[0], $info[1]); //分配顏色 $color = imagecolorallocate($new_image,255,255,255); //定義為透明色 imagecolortransparent($new_image,$color); //填充圖片 imagefill($new_image,0,0,$color); //拷貝圖片并保存成指定大小 imagecopyresized($new_image, $src, 0, 0, $x, $y, $info[0], $info[1], $width, $height); //保存 if(false==$cover){ $file = rtrim(dirname($file),'/').'/c_'.basename($file); $save_file[] = $file; } imagejpeg($new_image,$file); imagedestroy($new_image); imagedestroy($src); $i++; } if(false==$cover){ return $save_file; }else{ return $i; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery+ThinkPHP實(shí)現(xiàn)圖片上傳
- jquery+ajax實(shí)現(xiàn)上傳圖片并顯示上傳進(jìn)度功能【附php后臺接收】
- PHP結(jié)合jquery ajax實(shí)現(xiàn)上傳多張圖片,并限制圖片大小操作示例
- thinkphp jquery實(shí)現(xiàn)圖片上傳和預(yù)覽效果
- JQuery PHP圖片在線裁剪實(shí)例
- php+flash+jQuery多圖片上傳源碼分享
- PHP+jQuery+Ajax實(shí)現(xiàn)多圖片上傳效果
- jquery+php+ajax顯示上傳進(jìn)度的多圖片上傳并生成縮略圖代碼
- PHP結(jié)合JQueryJcrop實(shí)現(xiàn)圖片裁切實(shí)例詳解
- jQuery+PHP實(shí)現(xiàn)圖片上傳并提交功能
相關(guān)文章
基于jQuery的一個(gè)擴(kuò)展form序列化到j(luò)son對象
jQuery沒有直接支持form到j(luò)son的序列化方法,目前網(wǎng)上有一個(gè)實(shí)現(xiàn)是這樣的2010-12-12jquery操作checkbox實(shí)現(xiàn)全選和取消全選
這篇文章主要介紹了jquery操作checkbox實(shí)現(xiàn)全選和取消全選,需要的朋友可以參考下2014-05-05jQuery 實(shí)現(xiàn)扁平式小清新導(dǎo)航
這篇文章主要介紹了jQuery 實(shí)現(xiàn)扁平式小清新導(dǎo)航,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07jQuery實(shí)現(xiàn)彈窗下底部頁面禁止滑動(dòng)效果
在項(xiàng)目開發(fā)過程中,經(jīng)常會遇到帶有彈窗的頁面,尤其是在移動(dòng)端。下面通過本文給大家分享jQuery實(shí)現(xiàn)彈窗下底部頁面禁止滑動(dòng)效果,需要的朋友參考下吧2017-12-12jquery在項(xiàng)目中做復(fù)選框時(shí)遇到的一些問題筆記
在實(shí)踐中還是遇到了很多的問題,注意在input的checkbox中,用普通的attr屬性來判斷是不可以的,因?yàn)閏hecked的值是checked,因此做個(gè)筆記2013-11-11jQuery插件boxScroll實(shí)現(xiàn)圖片輪播特效
本文給大家分享的是使用jQuery插件Boxscroll來實(shí)現(xiàn)簡單的圖片輪播特效的代碼,非常簡單實(shí)用,有需要的小伙伴可以參考下。2015-07-07