12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070 |
- /*
- * CROPPIC
- * dependancy: jQuery
- * author: Ognjen "Zmaj Džedaj" Božičković and Mat Steinlin
- */
- (function (window, document) {
- Croppic = function (id, options) {
- var that = this;
- that.id = id;
- that.obj = $('#' + id);
- that.outputDiv = that.obj;
- that.imgUrl="";
- that.saveImgUrl="";
- // DEFAULT OPTIONS
- that.options = {
- uploadUrl:'',
- uploadData:{},
- cropUrl:'',
- cropData:{},
- outputUrlId:'',
- //styles
- imgEyecandy:true,
- imgEyecandyOpacity:0.2,
- initialZoom:40,
- zoomFactor:10,
- rotateFactor:5,
- doubleZoomControls:true,
- rotateControls: true,
- modal:false,
- customUploadButtonId:'',
- customSaveButtonId:'',
- loaderHtml:'',
- scaleToFill: true,
- processInline: false,
- loadPicture:'',
- onReset: null,
- enableMousescroll: false,
-
- //callbacks
- onBeforeImgUpload: null,
- onAfterImgUpload: null,
- onImgDrag: null,
- onImgZoom: null,
- onImgRotate: null,
- onBeforeImgCrop: null,
- onAfterImgCrop: null,
- onBeforeRemoveCroppedImg: null,
- onAfterRemoveCroppedImg: null,
- onError: null,
-
- };
- // OVERWRITE DEFAULT OPTIONS
- for (i in options) that.options[i] = options[i];
- // INIT THE WHOLE DAMN THING!!!
- that.init();
-
- };
- Croppic.prototype = {
- id:'',
- imgInitW:0,
- imgInitH:0,
- imgW:0,
- imgH:0,
- actualRotationImgW:0,
- actualRotationImgH:0,
- objW:0,
- objH:0,
- actualRotation: 0,
- windowW:0,
- windowH:$(window).height(),
- obj:{},
- outputDiv:{},
- outputUrlObj:{},
- img:{},
- defaultImg:{},
- croppedImg:{},
- imgEyecandy:{},
- form:{},
- iframeform: {},
- iframeobj: {},
- cropControlsUpload:{},
- cropControlsCrop:{},
- cropControlZoomMuchIn:{},
- cropControlZoomMuchOut:{},
- cropControlZoomIn:{},
- cropControlZoomOut:{},
- cropControlCrop:{},
- cropControlReset:{},
- cropControlRemoveCroppedImage:{},
- cropControlOriginal:{},
- modal:{},
- loader:{},
- cropSaveControl:{},
-
- init: function () {
- var that = this;
-
- that.objW = that.obj.width();
- that.objH = that.obj.height();
-
- // reset rotation
- that.actualRotation = 0;
- that.actualRotationImgH=0;
- that.actualRotationImgW=0;
-
- if( $.isEmptyObject(that.defaultImg)){ that.defaultImg = that.obj.find('img'); }
-
- that.createImgUploadControls();
-
- if( $.isEmptyObject(that.options.loadPicture)){
- that.bindImgUploadControl();
- }else{
- that.loadExistingImage();
- }
-
- },
- createImgUploadControls: function(){
- var that = this;
-
- var cropControlUpload = '';
- if(that.options.customUploadButtonId ===''){ cropControlUpload = '<i class="cropControlUpload"></i>'; }
- var cropControlRemoveCroppedImage = '<i class="cropControlRemoveCroppedImage"></i>';
- var cropControlOriginal = '<i class="cropControlOriginal"></i>';
-
- if( $.isEmptyObject(that.croppedImg))
- {
- cropControlRemoveCroppedImage='';
- cropControlOriginal='';
- }
- if( !$.isEmptyObject(that.options.loadPicture)){ cropControlUpload='';}
- var html = '<div class="cropControls cropControlsUpload"> ' + cropControlUpload + cropControlOriginal + cropControlRemoveCroppedImage + ' </div>';
- that.outputDiv.append(html);
-
- that.cropControlsUpload = that.outputDiv.find('.cropControlsUpload');
-
- if(that.options.customUploadButtonId ===''){ that.imgUploadControl = that.outputDiv.find('.cropControlUpload'); }
- else{ that.imgUploadControl = $('#'+that.options.customUploadButtonId); that.imgUploadControl.show(); }
- if(that.options.customSaveButtonId !==''){
- that.cropSaveControl= $('#'+that.options.customSaveButtonId); that.cropSaveControl.hide();
- }
- if( !$.isEmptyObject(that.croppedImg)){
- that.cropControlRemoveCroppedImage = that.outputDiv.find('.cropControlRemoveCroppedImage');
- that.cropControlOriginal = that.outputDiv.find('.cropControlOriginal');
- }
-
- },
- bindImgUploadControl: function(){
-
- var that = this;
-
- // CREATE UPLOAD IMG FORM
- var formHtml = '<form class="' + that.id + '_imgUploadForm" style="visibility: hidden;"> <input type="file" name="img" id="' + that.id + '_imgUploadField"> </form>';
- that.outputDiv.append(formHtml);
- that.form = that.outputDiv.find('.'+that.id+'_imgUploadForm');
-
-
- // CREATE FALLBACK IE9 IFRAME
- var fileUploadId = that.CreateFallbackIframe();
-
- that.imgUploadControl.off('click');
- that.imgUploadControl.on('click',function(){
- if (fileUploadId === "") {
- that.form.find('input[type="file"]').trigger('click');
- } else {
- //Trigger iframe file input click, otherwise access restriction error
- that.iframeform.find('input[type="file"]').trigger('click');
- }
- });
- that.cropSaveControl.off('click');
- that.cropSaveControl.on('click',function(){
- var res = confirm('您确定要使用编辑好的图片吗?\n\r' +
- '[确认]使用图片\n\r' +
- '[取消]继续编辑');
- if (res) {
- that.destroy();
- window.location.href="https://wwww.croppic.com?"+that.saveImgUrl;
- }
- });
- if( !$.isEmptyObject(that.croppedImg)){
-
- that.cropControlRemoveCroppedImage.on('click',function(){
- if (typeof (that.options.onBeforeRemoveCroppedImg) === typeof(Function)) {
- that.options.onBeforeRemoveCroppedImg.call(that);
- }
- var res = confirm('您是要使用当前图片还是重新选择图片?\n\r' +
- '[确认]使用当前图片\n\r' +
- '[取消]重新选择图片');
- var width= that.imgInitW;
- var height= that.imgInitH;
- that.croppedImg.remove();
- that.croppedImg = {};
- $(this).hide();
- that.cropSaveControl.hide();
- that.cropControlOriginal.hide();
- if (typeof (that.options.onAfterRemoveCroppedImg) === typeof(Function)) {
- that.options.onAfterRemoveCroppedImg.call(that);
- }
- if( !$.isEmptyObject(that.defaultImg)){
- that.obj.append(that.defaultImg);
- }
- if(that.options.outputUrlId !== ''){ $('#'+that.options.outputUrlId).val(''); }
- that.imgUploadControl.show();
- if (res) {
- that.showLoader();
- that.imgUploadControl.hide();
- if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
- that.imgInitW = that.imgW = width;
- that.imgInitH = that.imgH = height;
- that.saveImgUrl="";
- that.obj.append('<img src="'+that.imgUrl+'">');
- that.initCropper();
- that.hideLoader();
- }
- });
- that.cropControlOriginal.on('click',function(){
- if (that.imgUrl=='')
- {
- alert('请选择图片' );
- return;
- }
- var res = confirm('您确定要使用原图吗?\n\r' +
- '[确认]使用原图\n\r' +
- '[取消]继续编辑');
- if (res) {
- that.destroy();
- window.location.href="https://wwww.croppic.com?"+that.imgUrl;
- }
- });
- }
-
- that.form.find('input[type="file"]').change(function(){
-
- if (that.options.onBeforeImgUpload) that.options.onBeforeImgUpload.call(that);
- that.showLoader();
- that.imgUploadControl.hide();
- if(that.options.processInline){
- // Checking Browser Support for FileReader API
- if (typeof FileReader == "undefined"){
- if (that.options.onError) that.options.onError.call(that,"processInline is not supported by your Browser");
- that.reset();
- }else{
- var filePath=that.form.find('input[type="file"]')[0].files[0];
- if( !$.isEmptyObject(that.croppedImg)){
- that.destroy();
- }
- var reader = new FileReader();
- reader.onload = function (e) {
- var image = new Image();
- image.src = e.target.result;
- image.onload = function(){
- that.imgInitW = that.imgW = image.width;
- that.imgInitH = that.imgH = image.height;
- if(that.options.modal){ that.createModal(); }
- if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
-
- that.imgUrl=image.src;
- that.saveImgUrl="";
-
- that.obj.append('<img src="'+image.src+'">');
-
- that.initCropper();
- that.hideLoader();
-
- if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
-
- }
- };
- reader.readAsDataURL(filePath);
- }
- } else {
-
- try {
- // other modern browsers
- formData = new FormData(that.form[0]);
- } catch(e) {
- // IE10 MUST have all form items appended as individual form key / value pairs
- formData = new FormData();
- formData.append('img', that.form.find("input[type=file]")[0].files[0]);
-
- }
-
- for (var key in that.options.uploadData) {
- if( that.options.uploadData.hasOwnProperty(key) ) {
- formData.append( key , that.options.uploadData[key] );
- }
- }
-
- $.ajax({
- url: that.options.uploadUrl,
- data: formData,
- context: document.body,
- cache: false,
- contentType: false,
- processData: false,
- type: 'POST'
- }).always(function (data) {
- that.afterUpload(data);
- });
- }
- });
- },
- loadExistingImage: function(){
- var that = this;
-
- if( $.isEmptyObject(that.croppedImg)){
- if (that.options.onBeforeImgUpload) that.options.onBeforeImgUpload.call(that);
-
- that.showLoader();
- if(that.options.modal){ that.createModal(); }
- if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
-
- that.imgUrl=that.options.loadPicture ;
-
- var img =$('<img src="'+ that.options.loadPicture +'">');
- that.obj.append(img);
- img.load(function() {
- that.imgInitW = that.imgW = this.width;
- that.imgInitH = that.imgH = this.height;
- that.initCropper();
- that.hideLoader();
- if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
- });
-
- }else{
- that.cropControlRemoveCroppedImage.on('click',function(){
- var res = confirm('您是要使用当前图片还是重新选择图片?\n\r' +
- '[确认]使用当前图片\n\r' +
- '[取消]重新选择图片');
- var width= that.imgInitW;
- var height= that.imgInitH;
- that.croppedImg.remove();
- $(this).hide();
- that.cropSaveControl.hide();
- that.cropControlOriginal.hide();
- if( !$.isEmptyObject(that.defaultImg)){
- that.obj.append(that.defaultImg);
- }
- if(that.options.outputUrlId !== ''){ $('#'+that.options.outputUrlId).val(''); }
- that.croppedImg = '';
- that.reset();
- if (res) {
- that.showLoader();
- that.imgUploadControl.hide();
- if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
- that.imgInitW = that.imgW = width;
- that.imgInitH = that.imgH = height;
- that.saveImgUrl="";
- that.obj.append('<img src="'+that.imgUrl+'">');
- that.initCropper();
- that.hideLoader();
- }
- });
- }
-
- },
- afterUpload: function(data){
- var that = this;
- response = typeof data =='object' ? data : jQuery.parseJSON(data);
-
- if (response.status == 'success') {
- that.imgInitW = that.imgW = response.width;
- that.imgInitH = that.imgH = response.height;
- if (that.options.modal) { that.createModal(); }
- if (!$.isEmptyObject(that.croppedImg)) { that.croppedImg.remove(); }
- that.imgUrl = response.url;
- var img = $('<img src="'+response.url+'">')
- that.obj.append(img);
- img.load(function(){
- that.initCropper();
- that.hideLoader();
- if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
- });
-
- if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
- }
- if (response.status == 'error') {
- alert(response.message);
- if (that.options.onError) that.options.onError.call(that,response.message);
- that.hideLoader();
- // setTimeout( function(){ that.reset(); },2000)
- }
- },
-
- createModal: function(){
- var that = this;
-
- var marginTop = that.windowH/2-that.objH/2;
- var modalHTML = '<div id="croppicModal">'+'<div id="croppicModalObj" style="width:'+ that.objW +'px; height:'+ that.objH +'px; margin:0 auto; margin-top:'+ marginTop +'px; position: relative;"> </div>'+'</div>';
- $('body').append(modalHTML);
-
- that.modal = $('#croppicModal');
-
- that.obj = $('#croppicModalObj');
-
- },
- destroyModal: function(){
- var that = this;
-
- that.obj = that.outputDiv;
- that.modal.remove();
- that.modal = {};
- },
- initCropper: function(){
- var that = this;
-
- /*SET UP SOME VARS*/
- that.img = that.obj.find('img');
- that.img.wrap('<div class="cropImgWrapper" style="overflow:hidden; z-index:1; position:absolute; width:'+that.objW+'px; height:'+that.objH+'px;"></div>');
-
- /*INIT DRAGGING*/
- that.createCropControls();
-
- if(that.options.imgEyecandy){ that.createEyecandy(); }
- that.initDrag();
- that.initialScaleImg();
- },
- createEyecandy: function(){
- var that = this;
- that.imgEyecandy = that.img.clone();
- that.imgEyecandy.css({'z-index':'0','opacity':that.options.imgEyecandyOpacity}).appendTo(that.obj);
- },
- destroyEyecandy: function(){
- var that = this;
- that.imgEyecandy.remove();
- },
- initialScaleImg:function(){
- var that = this;
- that.zoom(-that.imgInitW);
- that.zoom(that.options.initialZoom);
-
- // Adding mousewheel zoom capabilities
- if (that.options.enableMousescroll){
- that.img.on('mousewheel', function(event) {
- event.preventDefault();
- that.zoom(that.options.zoomFactor*event.deltaY);
- });
- }
- // initial center image
-
- that.img.css({'left': -(that.imgW -that.objW)/2, 'top': -(that.imgH -that.objH)/2, 'position':'relative'});
- if(that.options.imgEyecandy){ that.imgEyecandy.css({'left': -(that.imgW -that.objW)/2, 'top': -(that.imgH -that.objH)/2, 'position':'relative'}); }
-
- },
-
- createCropControls: function(){
- var that = this;
-
- // CREATE CONTROLS
- var cropControlZoomMuchIn = '';
- var cropControlZoomIn = '<i class="cropControlZoomIn"></i>';
- var cropControlZoomOut = '<i class="cropControlZoomOut"></i>';
- var cropControlZoomMuchOut = '';
- var cropControlRotateLeft = '';
- var cropControlRotateRight = '';
- var cropControlCrop = '<i class="cropControlCrop"></i>';
- var cropControlReset = '<i class="cropControlReset"></i>';
- var cropControlOriginal = '<i class="cropControlOriginal"></i>';
- var html;
-
- if(that.options.doubleZoomControls){
- cropControlZoomMuchIn = '<i class="cropControlZoomMuchIn"></i>';
- cropControlZoomMuchOut = '<i class="cropControlZoomMuchOut"></i>';
- }
- if(that.options.rotateControls){
- cropControlRotateLeft = '<i class="cropControlRotateLeft"></i>';
- cropControlRotateRight = '<i class="cropControlRotateRight"></i>';
- }
-
- html = '<div class="cropControls cropControlsCrop">'+ cropControlZoomMuchIn + cropControlZoomIn + cropControlZoomOut + cropControlZoomMuchOut + cropControlRotateLeft + cropControlRotateRight + cropControlCrop + '</div>';
-
- that.obj.append(html);
- html = '<div class="cropControls cropControlsCrop cropControlsUpload">' + cropControlOriginal + cropControlReset + '</div>';
- that.obj.append(html);
-
- that.cropControlsCrop = that.obj.find('.cropControlsCrop');
- // CACHE AND BIND CONTROLS
- if(that.options.doubleZoomControls){
- that.cropControlZoomMuchIn = that.cropControlsCrop.find('.cropControlZoomMuchIn');
- that.cropControlZoomMuchIn.on('click',function(){ that.zoom( that.options.zoomFactor*10 ); });
-
- that.cropControlZoomMuchOut = that.cropControlsCrop.find('.cropControlZoomMuchOut');
- that.cropControlZoomMuchOut.on('click',function(){ that.zoom(-that.options.zoomFactor*10); });
- }
-
- that.cropControlZoomIn = that.cropControlsCrop.find('.cropControlZoomIn');
- that.cropControlZoomIn.on('click',function(){ that.zoom(that.options.zoomFactor); });
- that.cropControlZoomOut = that.cropControlsCrop.find('.cropControlZoomOut');
- that.cropControlZoomOut.on('click',function(){ that.zoom(-that.options.zoomFactor); });
- that.cropControlRotateLeft = that.cropControlsCrop.find('.cropControlRotateLeft');
- that.touchtimeLeft = new Date().getTime();
- that.cropControlRotateLeft.on('click', function() {
- if(new Date().getTime()-that.touchtimeLeft > 300 ){
- that.rotate(-that.options.rotateFactor);
- that.touchtimeLeft = new Date().getTime();
- }else{
- that.rotate(-45);
- }
- });
- that.cropControlRotateRight = that.cropControlsCrop.find('.cropControlRotateRight');
- that.touchtimeRight = new Date().getTime();
- that.cropControlRotateRight.on('click', function() {
- if(new Date().getTime()-that.touchtimeRight > 300 ){
- that.rotate(that.options.rotateFactor);
- that.touchtimeRight = new Date().getTime();
- }else{
- that.rotate(45);
- }
- });
-
- that.cropControlCrop = that.cropControlsCrop.find('.cropControlCrop');
- that.cropControlCrop.on('click',function(){ that.crop(); });
- that.cropControlReset = that.cropControlsCrop.find('.cropControlReset');
- that.cropControlReset.on('click',function(){
- var res = confirm('您是要使用当前图片还是重新选择图片?\n\r' +
- '[确认]使用当前图片\n\r' +
- '[取消]重新选择图片');
- var width= that.imgInitW;
- var height= that.imgInitH;
- that.reset();
- if (res) {
- that.showLoader();
- that.imgUploadControl.hide();
- if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
- that.imgInitW = that.imgW = width;
- that.imgInitH = that.imgH = height;
- that.saveImgUrl="";
- that.obj.append('<img src="'+that.imgUrl+'">');
- that.initCropper();
- that.hideLoader();
- }
- });
- that.cropControlOriginal = that.cropControlsCrop.find('.cropControlOriginal');
- that.cropControlOriginal.on('click',function(){
- if (that.imgUrl=='')
- {
- alert('请选择图片' );
- return;
- }
- var res = confirm('您确定要使用原图吗?\n\r' +
- '[确认]使用原图\n\r' +
- '[取消]继续编辑');
- if (res) {
- that.destroy();
- window.location.href="https://wwww.croppic.com?"+that.imgUrl;
- }
- });
-
- },
- initDrag:function(){
- var that = this;
-
- that.img.on("mousedown touchstart", function(e) {
-
- e.preventDefault(); // disable selection
-
- var pageX;
- var pageY;
- var userAgent = window.navigator.userAgent;
- if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i) || (e.pageY && e.pageX) == undefined) {
- pageX = e.originalEvent.touches[0].pageX;
- pageY = e.originalEvent.touches[0].pageY;
- } else {
- pageX = e.pageX;
- pageY = e.pageY;
- }
-
- var z_idx = that.img.css('z-index'),
- drg_h = that.img.outerHeight(),
- drg_w = that.img.outerWidth(),
- pos_y = that.img.offset().top + drg_h - pageY,
- pos_x = that.img.offset().left + drg_w - pageX;
-
- that.img.css('z-index', 1000).on("mousemove touchmove", function(e) {
-
- var imgTop;
- var imgLeft;
-
- if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i) || (e.pageY && e.pageX) == undefined) {
- imgTop = e.originalEvent.touches[0].pageY + pos_y - drg_h;
- imgLeft = e.originalEvent.touches[0].pageX + pos_x - drg_w;
- } else {
- imgTop = e.pageY + pos_y - drg_h;
- imgLeft = e.pageX + pos_x - drg_w;
- }
-
- that.img.offset({
- top:imgTop,
- left:imgLeft
- }).on("mouseup", function() {
- $(this).removeClass('draggable').css('z-index', z_idx);
- });
-
- if(that.options.imgEyecandy){ that.imgEyecandy.offset({ top:imgTop, left:imgLeft }); }
- var top=that.actualRotationImgH;
- var left=that.actualRotationImgW;
- // var top=0;
- // var left=0;
- if (that.objH < that.imgH+top*2) {
- if (parseInt(that.img.css('top')) > top) { that.img.css('top', top); if (that.options.imgEyecandy) { that.imgEyecandy.css('top', top);}}
- var maxTop = -( that.imgH - that.objH+top); if (parseInt(that.img.css('top')) < maxTop) { that.img.css('top', maxTop); if (that.options.imgEyecandy) { that.imgEyecandy.css('top', maxTop); }}
- }else{
- if (parseInt(that.img.css('top')) < -top) { that.img.css('top', -top); if (that.options.imgEyecandy) { that.imgEyecandy.css('top', top); }}
- var maxTop = that.objH - that.imgH+top; if (parseInt(that.img.css('top')) > maxTop) { that.img.css('top', maxTop);if (that.options.imgEyecandy) {that.imgEyecandy.css('top', maxTop); }}
- }
- if (that.objW < that.imgW+2*left) {
- if( parseInt( that.img.css('left')) > left ){ that.img.css('left',left); if(that.options.imgEyecandy){ that.imgEyecandy.css('left', left); }}
- var maxLeft = -( that.imgW-that.objW+left); if( parseInt( that.img.css('left')) < maxLeft){ that.img.css('left', maxLeft); if(that.options.imgEyecandy){ that.imgEyecandy.css('left', maxLeft); } }
- }else{
- if( parseInt( that.img.css('left')) < -left ){ that.img.css('left',-left); if(that.options.imgEyecandy){ that.imgEyecandy.css('left', 0); }}
- var maxLeft = ( that.objW - that.imgW+left); if( parseInt( that.img.css('left')) > maxLeft){ that.img.css('left', maxLeft); if(that.options.imgEyecandy){ that.imgEyecandy.css('left', maxLeft); } }
- }
- if (that.options.onImgDrag) that.options.onImgDrag.call(that);
-
- });
-
- }).on("mouseup", function() {
- that.img.off("mousemove");
- }).on("mouseout", function() {
- that.img.off("mousemove");
- });
-
- },
- rotate: function(x) {
- var that = this;
- that.actualRotation += x;
- that.img.css({
- '-webkit-transform': 'rotate(' + that.actualRotation + 'deg)',
- '-moz-transform': 'rotate(' + that.actualRotation + 'deg)',
- 'transform': 'rotate(' + that.actualRotation + 'deg)',
- });
- if(that.options.imgEyecandy) {
- that.imgEyecandy.css({
- '-webkit-transform': 'rotate(' + that.actualRotation + 'deg)',
- '-moz-transform': 'rotate(' + that.actualRotation + 'deg)',
- 'transform': 'rotate(' + that.actualRotation + 'deg)',
- });
- }
- // that.actualRotationImgH=0;
- // that.actualRotationImgW=0;
- // if (that.actualRotation!=0)
- // {
- // if (that.actualRotation==90||that.actualRotation==270)
- // {
- // if(that.imgH>that.imgW)
- // {
- // that.actualRotationImgW=parseInt((that.imgH-that.imgW)/2);
- //
- // }else if(that.imgH<that.imgW)
- // {
- // that.actualRotationImgH=parseInt((that.imgW-that.imgH)/2);
- // }
- // }else if (that.actualRotation!=180&&that.actualRotation!=360) {
- // var actualRotation=that.actualRotation;
- // if (actualRotation>180)
- // {
- // actualRotation=actualRotation-180;
- // }
- // if (actualRotation>90)
- // {
- // actualRotation=180-actualRotation;
- // }
- // var radian = Math.tan(actualRotation * Math.PI / 180);
- // that.actualRotationImgW=parseInt(radian*(that.imgH/2));
- // that.actualRotationImgH=parseInt(radian*(that.imgW/2));
- // }
- // }
- if (typeof that.options.onImgRotate == 'function')
- that.options.onImgRotate.call(that);
- },
- zoom :function(x){
- var that = this;
- var ratio = that.imgW / that.imgH;
- var newWidth = that.imgW+x;
- var newHeight = newWidth/ratio;
- var doPositioning = true;
- if( newWidth < that.objW || newHeight < that.objH){
- if( newWidth - that.objW < newHeight - that.objH ){
- newWidth = that.objW;
- newHeight = newWidth/ratio;
- }else{
- newHeight = that.objH;
- newWidth = ratio * newHeight;
- }
- doPositioning = false;
- }
- if(!that.options.scaleToFill && (newWidth > that.imgInitW || newHeight > that.imgInitH)){
- if( newWidth - that.imgInitW < newHeight - that.imgInitH ){
- newWidth = that.imgInitW;
- newHeight = newWidth/ratio;
- }else{
- newHeight = that.imgInitH;
- newWidth = ratio * newHeight;
- }
- doPositioning = false;
- }
- that.imgW = newWidth;
- that.img.width(newWidth);
- that.imgH = newHeight;
- that.img.height(newHeight);
- var newTop = parseInt( that.img.css('top') ) - x/2;
- var newLeft = parseInt( that.img.css('left') ) - x/2;
- if( newTop>0 ){ newTop=0;}
- if( newLeft>0 ){ newLeft=0;}
- var maxTop = -( newHeight-that.objH); if( newTop < maxTop){ newTop = maxTop; }
- var maxLeft = -( newWidth-that.objW); if( newLeft < maxLeft){ newLeft = maxLeft; }
- if( doPositioning ){
- that.img.css({'top':newTop, 'left':newLeft});
- }
- if(that.options.imgEyecandy){
- that.imgEyecandy.width(newWidth);
- that.imgEyecandy.height(newHeight);
- if( doPositioning ){
- that.imgEyecandy.css({'top':newTop, 'left':newLeft});
- }
- }
- if (that.options.onImgZoom) that.options.onImgZoom.call(that);
- },
- crop:function(){
- var that = this;
-
- if (that.options.onBeforeImgCrop) that.options.onBeforeImgCrop.call(that);
-
- that.cropControlsCrop.hide();
- that.showLoader();
-
- var cropData = {
- imgUrl:that.imgUrl,
- imgInitW:that.imgInitW,
- imgInitH:that.imgInitH,
- imgW:that.imgW,
- imgH:that.imgH,
- imgY1:Math.abs( parseInt( that.img.css('top') ) ),
- imgX1:Math.abs( parseInt( that.img.css('left') ) ),
- // imgY1:parseInt( that.img.css('top') ),
- // imgX1: parseInt( that.img.css('left')),
- cropH:that.objH,
- cropW:that.objW,
- rotation:that.actualRotation
- };
-
- var formData;
-
- if(typeof FormData == 'undefined'){
- var XHR = new XMLHttpRequest();
- var urlEncodedData = "";
- var urlEncodedDataPairs = [];
-
- for(var key in cropData) {
- urlEncodedDataPairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(cropData[key]));
- }
- for(var key in that.options.cropData) {
- urlEncodedDataPairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(that.options.cropData[key]));
- }
- urlEncodedData = urlEncodedDataPairs.join('&').replace(/%20/g, '+');
-
- XHR.addEventListener('error', function(event) {
- if (that.options.onError) that.options.onError.call(that,"XHR Request failed");
- });
- XHR.onreadystatechange=function(){
- if (XHR.readyState==4 && XHR.status==200)
- {
- that.afterCrop(XHR.responseText);
- }
- }
- XHR.open('POST', that.options.cropUrl);
- XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- XHR.setRequestHeader('Content-Length', urlEncodedData.length);
- XHR.send(urlEncodedData);
-
- }else{
- formData = new FormData();
- for (var key in cropData) {
- if( cropData.hasOwnProperty(key) ) {
- formData.append( key , cropData[key] );
- }
- }
-
- for (var key in that.options.cropData) {
- if( that.options.cropData.hasOwnProperty(key) ) {
- formData.append( key , that.options.cropData[key] );
- }
- }
-
- $.ajax({
- url: that.options.cropUrl,
- data: formData,
- context: document.body,
- cache: false,
- contentType: false,
- processData: false,
- type: 'POST'
- }).always(function (data) {
- that.afterCrop(data);
- });
- }
-
- //
- },
- afterCrop: function (data) {
- var that = this;
- try {
- response = jQuery.parseJSON(data);
- }
- catch(err) {
- response = typeof data =='object' ? data : jQuery.parseJSON(data);
- }
-
- if (response.status == 'success') {
- if (that.options.imgEyecandy)
- that.imgEyecandy.hide();
- that.destroy();
- that.saveImgUrl=response.url;
- var croppedImg='<img class="croppedImg" src="' + response.url + '">';
- that.obj.append(croppedImg);
- if (that.options.outputUrlId !== '') { $('#' + that.options.outputUrlId).val(response.url); }
- that.croppedImg = that.obj.find('.croppedImg');
- that.croppedImg .width(that.objW+"px").height(that.objH+"px");
- that.init();
- that.hideLoader();
- that.imgUploadControl.hide();
- that.cropSaveControl.show();
- }
- if (response.status == 'error') {
- if (that.options.onError) that.options.onError.call(that,response.message);
- that.hideLoader();
- // setTimeout( function(){ that.reset(); },2000)
- }
- if (that.options.onAfterImgCrop) that.options.onAfterImgCrop.call(that, response);
- },
- showLoader:function(){
- var that = this;
-
- that.obj.append(that.options.loaderHtml);
- that.loader = that.obj.find('.loader');
-
- },
- hideLoader:function(){
- var that = this;
- that.loader.remove();
- },
- reset:function(){
- var that = this;
- that.destroy();
-
- that.init();
- that.imgUploadControl.show();
- if( !$.isEmptyObject(that.croppedImg)){
- that.obj.append(that.croppedImg);
- if(that.options.outputUrlId !== ''){ $('#'+that.options.outputUrlId).val(that.croppedImg.attr('url')); }
- }
- if (typeof that.options.onReset == 'function')
- that.options.onReset.call(that);
- },
- destroy:function(){
- var that = this;
- if(that.options.modal && !$.isEmptyObject(that.modal) ){ that.destroyModal(); }
- if(that.options.imgEyecandy && !$.isEmptyObject(that.imgEyecandy) ){ that.destroyEyecandy(); }
- if( !$.isEmptyObject( that.cropControlsUpload ) ){ that.cropControlsUpload.remove(); }
- if( !$.isEmptyObject( that.cropControlsCrop ) ){ that.cropControlsCrop.remove(); }
- if( !$.isEmptyObject( that.loader ) ){ that.loader.remove(); }
- if( !$.isEmptyObject( that.form ) ){ that.form.remove(); }
- that.obj.html('');
- },
- isAjaxUploadSupported: function () {
- var input = document.createElement("input");
- input.type = "file";
- return (
- "multiple" in input &&
- typeof File != "undefined" &&
- typeof FormData != "undefined" &&
- typeof (new XMLHttpRequest()).upload != "undefined");
- },
- CreateFallbackIframe: function () {
- var that = this;
-
- if (!that.isAjaxUploadSupported()) {
- if (jQuery.isEmptyObject(that.iframeobj)) {
- var iframe = document.createElement("iframe");
- iframe.setAttribute("id", that.id + "_upload_iframe");
- iframe.setAttribute("name", that.id + "_upload_iframe");
- iframe.setAttribute("width", "0");
- iframe.setAttribute("height", "0");
- iframe.setAttribute("border", "0");
- iframe.setAttribute("src", "javascript:false;");
- iframe.style.display = "none";
- document.body.appendChild(iframe);
- } else {
- iframe = that.iframeobj[0];
- }
- var myContent = '<!DOCTYPE html>'
- + '<html><head><title>Uploading File</title></head>'
- + '<body>'
- + '<form '
- + 'class="' + that.id + '_upload_iframe_form" '
- + 'name="' + that.id + '_upload_iframe_form" '
- + 'action="' + that.options.uploadUrl + '" method="post" '
- + 'enctype="multipart/form-data" encoding="multipart/form-data" style="display:none;">'
- + $("#" + that.id + '_imgUploadField')[0].outerHTML
- + '</form></body></html>';
- iframe.contentWindow.document.open('text/htmlreplace');
- iframe.contentWindow.document.write(myContent);
- iframe.contentWindow.document.close();
- that.iframeobj = $("#" + that.id + "_upload_iframe");
- that.iframeform = that.iframeobj.contents().find("html").find("." + that.id + "_upload_iframe_form");
-
- that.iframeform.on("change", "input", function () {
- that.SubmitFallbackIframe(that);
- });
- that.iframeform.find("input")[0].attachEvent("onchange", function () {
- that.SubmitFallbackIframe(that);
- });
-
- var eventHandlermyFile = function () {
- if (iframe.detachEvent)
- iframe.detachEvent("onload", eventHandlermyFile);
- else
- iframe.removeEventListener("load", eventHandlermyFile, false);
- var response = that.getIframeContentJSON(iframe);
- if (jQuery.isEmptyObject(that.modal)) {
- that.afterUpload(response);
- }
- }
- if (iframe.addEventListener)
- iframe.addEventListener("load", eventHandlermyFile, true);
- if (iframe.attachEvent)
- iframe.attachEvent("onload", eventHandlermyFile);
- return "#" + that.id + '_imgUploadField';
-
- } else {
- return "";
- }
- },
- SubmitFallbackIframe: function (that) {
- that.showLoader();
- if(that.options.processInline && !that.options.uploadUrl){
- if (that.options.onError){
- that.options.onError.call(that,"processInline is not supported by your browser ");
- that.hideLoader();
- }
- }else{
- if (that.options.onBeforeImgUpload) that.options.onBeforeImgUpload.call(that);
- that.iframeform[0].submit();
- }
- },
- getIframeContentJSON: function (iframe) {
- try {
- var doc = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document,
- response;
- var innerHTML = doc.body.innerHTML;
- if (innerHTML.slice(0, 5).toLowerCase() == "<pre>" && innerHTML.slice(-6).toLowerCase() == "</pre>") {
- innerHTML = doc.body.firstChild.firstChild.nodeValue;
- }
- response = jQuery.parseJSON(innerHTML);
- } catch (err) {
- response = { success: false };
- }
- return response;
- }
-
- };
- })(window, document);
|