croppic.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /*
  2. * CROPPIC
  3. * dependancy: jQuery
  4. * author: Ognjen "Zmaj Džedaj" Božičković and Mat Steinlin
  5. */
  6. (function (window, document) {
  7. Croppic = function (id, options) {
  8. var that = this;
  9. that.id = id;
  10. that.obj = $('#' + id);
  11. that.outputDiv = that.obj;
  12. that.imgUrl="";
  13. that.saveImgUrl="";
  14. that.imgBackground="";
  15. // DEFAULT OPTIONS
  16. that.options = {
  17. uploadUrl:'',
  18. uploadData:{},
  19. cropUrl:'',
  20. cropData:{},
  21. outputUrlId:'',
  22. absorptionUrl:'',
  23. //styles
  24. imgEyecandy:true,
  25. imgEyecandyOpacity:0.2,
  26. initialZoom:40,
  27. zoomFactor:10,
  28. rotateFactor:5,
  29. doubleZoomControls:true,
  30. rotateControls: true,
  31. modal:false,
  32. customUploadButtonId:'',
  33. customSaveButtonId:'',
  34. loaderHtml:'',
  35. scaleToFill: true,
  36. processInline: false,
  37. loadPicture:'',
  38. onReset: null,
  39. enableMousescroll: false,
  40. colorAbsorption:false,
  41. //callbacks
  42. onBeforeImgUpload: null,
  43. onAfterImgUpload: null,
  44. onImgDrag: null,
  45. onImgZoom: null,
  46. onImgRotate: null,
  47. onBeforeImgCrop: null,
  48. onAfterImgCrop: null,
  49. onBeforeRemoveCroppedImg: null,
  50. onAfterRemoveCroppedImg: null,
  51. onError: null,
  52. };
  53. // OVERWRITE DEFAULT OPTIONS
  54. for (i in options) that.options[i] = options[i];
  55. // INIT THE WHOLE DAMN THING!!!
  56. that.init();
  57. };
  58. Croppic.prototype = {
  59. id:'',
  60. imgInitW:0,
  61. imgInitH:0,
  62. imgW:0,
  63. imgH:0,
  64. actualRotationImgW:0,
  65. actualRotationImgH:0,
  66. objW:0,
  67. objH:0,
  68. actualRotation: 0,
  69. windowW:0,
  70. windowH:$(window).height(),
  71. obj:{},
  72. outputDiv:{},
  73. outputUrlObj:{},
  74. img:{},
  75. defaultImg:{},
  76. croppedImg:{},
  77. imgEyecandy:{},
  78. form:{},
  79. iframeform: {},
  80. iframeobj: {},
  81. cropControlsUpload:{},
  82. cropControlsCrop:{},
  83. cropControlZoomMuchIn:{},
  84. cropControlZoomMuchOut:{},
  85. cropControlZoomIn:{},
  86. cropControlZoomOut:{},
  87. cropControlCrop:{},
  88. cropControlReset:{},
  89. cropControlRemoveCroppedImage:{},
  90. cropControlOriginal:{},
  91. modal:{},
  92. loader:{},
  93. cropSaveControl:{},
  94. ctx:{},
  95. imgName:'',
  96. init: function () {
  97. var that = this;
  98. that.objW = that.obj.width();
  99. that.objH = that.obj.height();
  100. // reset rotation
  101. that.actualRotation = 0;
  102. that.actualRotationImgH=0;
  103. that.actualRotationImgW=0;
  104. that.colorAbsorption=false;
  105. that.imgBackground="";
  106. that.ctx={};
  107. that.imgName='';
  108. if( $.isEmptyObject(that.defaultImg)){ that.defaultImg = that.obj.find('img'); }
  109. that.createImgUploadControls();
  110. if( $.isEmptyObject(that.options.loadPicture)){
  111. that.bindImgUploadControl();
  112. }else{
  113. that.loadExistingImage();
  114. }
  115. },
  116. createImgUploadControls: function(){
  117. var that = this;
  118. var cropControlUpload = '';
  119. if(that.options.customUploadButtonId ===''){ cropControlUpload = '<i class="cropControlUpload"></i>'; }
  120. var cropControlRemoveCroppedImage = '<i class="cropControlRemoveCroppedImage"></i>';
  121. var cropControlOriginal = '<i class="cropControlOriginal"></i>';
  122. if( $.isEmptyObject(that.croppedImg))
  123. {
  124. cropControlRemoveCroppedImage='';
  125. cropControlOriginal='';
  126. }
  127. if( !$.isEmptyObject(that.options.loadPicture)){ cropControlUpload='';}
  128. var html = '<div class="cropControls cropControlsUpload"> ' + cropControlUpload + cropControlOriginal + cropControlRemoveCroppedImage + ' </div>';
  129. that.outputDiv.append(html);
  130. that.cropControlsUpload = that.outputDiv.find('.cropControlsUpload');
  131. if(that.options.customUploadButtonId ===''){ that.imgUploadControl = that.outputDiv.find('.cropControlUpload'); }
  132. else{ that.imgUploadControl = $('#'+that.options.customUploadButtonId); that.imgUploadControl.show(); }
  133. if(that.options.customSaveButtonId !==''){
  134. that.cropSaveControl= $('#'+that.options.customSaveButtonId); that.cropSaveControl.hide();
  135. }
  136. if( !$.isEmptyObject(that.croppedImg)){
  137. that.cropControlRemoveCroppedImage = that.outputDiv.find('.cropControlRemoveCroppedImage');
  138. that.cropControlOriginal = that.outputDiv.find('.cropControlOriginal');
  139. }
  140. },
  141. bindImgUploadControl: function(){
  142. var that = this;
  143. // CREATE UPLOAD IMG FORM
  144. var formHtml = '<form class="' + that.id + '_imgUploadForm" style="visibility: hidden;"> <input type="file" name="img" id="' + that.id + '_imgUploadField"> </form>';
  145. that.outputDiv.append(formHtml);
  146. that.form = that.outputDiv.find('.'+that.id+'_imgUploadForm');
  147. // CREATE FALLBACK IE9 IFRAME
  148. var fileUploadId = that.CreateFallbackIframe();
  149. that.imgUploadControl.off('click');
  150. that.imgUploadControl.on('click',function(){
  151. if (fileUploadId === "") {
  152. that.form.find('input[type="file"]').trigger('click');
  153. } else {
  154. //Trigger iframe file input click, otherwise access restriction error
  155. that.iframeform.find('input[type="file"]').trigger('click');
  156. }
  157. });
  158. that.cropSaveControl.off('click');
  159. that.cropSaveControl.on('click',function(){
  160. that.destroy();
  161. window.location.href="https://wwww.croppic.com?"+that.saveImgUrl;
  162. });
  163. if( !$.isEmptyObject(that.croppedImg)){
  164. that.cropControlRemoveCroppedImage.on('click',function(){
  165. if (typeof (that.options.onBeforeRemoveCroppedImg) === typeof(Function)) {
  166. that.options.onBeforeRemoveCroppedImg.call(that);
  167. }
  168. var res = confirm('您是要使用当前图片还是重新选择图片?\n\r' +
  169. '[确认] 使用当前图片\n\r' +
  170. '[取消] 重新选择图片');
  171. var width= that.imgInitW;
  172. var height= that.imgInitH;
  173. that.croppedImg.remove();
  174. that.croppedImg = {};
  175. $(this).hide();
  176. that.cropSaveControl.hide();
  177. that.cropControlOriginal.hide();
  178. if (typeof (that.options.onAfterRemoveCroppedImg) === typeof(Function)) {
  179. that.options.onAfterRemoveCroppedImg.call(that);
  180. }
  181. if( !$.isEmptyObject(that.defaultImg)){
  182. that.obj.append(that.defaultImg);
  183. }
  184. if(that.options.outputUrlId !== ''){ $('#'+that.options.outputUrlId).val(''); }
  185. that.imgUploadControl.show();
  186. if (res) {
  187. that.showLoader();
  188. that.imgUploadControl.hide();
  189. if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
  190. that.imgInitW = that.imgW = width;
  191. that.imgInitH = that.imgH = height;
  192. that.saveImgUrl="";
  193. that.obj.append('<img src="'+that.imgUrl+'">');
  194. that.initCropper();
  195. that.hideLoader();
  196. }
  197. });
  198. that.cropControlOriginal.on('click',function(){
  199. if (that.imgUrl=='')
  200. {
  201. alert('请选择图片' );
  202. return;
  203. }
  204. var res = confirm('您确定要使用原图吗?\n\r' +
  205. '[确认] 使用原图\n\r' +
  206. '[取消] 继续编辑');
  207. if (res) {
  208. that.destroy();
  209. window.location.href="https://wwww.croppic.com?"+that.imgUrl;
  210. }
  211. });
  212. }
  213. that.form.find('input[type="file"]').change(function(){
  214. if (that.options.onBeforeImgUpload) that.options.onBeforeImgUpload.call(that);
  215. that.showLoader();
  216. that.imgUploadControl.hide();
  217. if(that.options.processInline){
  218. // Checking Browser Support for FileReader API
  219. if (typeof FileReader == "undefined"){
  220. if (that.options.onError) that.options.onError.call(that,"processInline is not supported by your Browser");
  221. that.reset();
  222. }else{
  223. var file=that.form.find('input[type="file"]')[0].files[0];
  224. var rFilter = /^(image\/jpeg|image\/png)$/i; // 检查图片格式
  225. if (!rFilter.test(file.type)) {
  226. alert("请选择jpeg、png格式的图片");
  227. that.imgUploadControl.show();
  228. that.hideLoader();
  229. return;
  230. }
  231. if( !$.isEmptyObject(that.croppedImg)){
  232. that.destroy();
  233. }
  234. var Orientation = null;
  235. //获取照片方向角属性,用户旋转控制
  236. EXIF.getData(file, function() {
  237. EXIF.getAllTags(this);
  238. Orientation = EXIF.getTag(this, 'Orientation');
  239. });
  240. var reader = new FileReader();
  241. reader.onload = function (e) {
  242. if (Orientation!=null&&Orientation != "" && Orientation != 1
  243. &&(Orientation==8||Orientation==6||Orientation==3))
  244. {
  245. that.takingCorrect(e.target.result);
  246. return;
  247. }
  248. var image = new Image();
  249. image.src = e.target.result;
  250. image.onload = function(){
  251. that.imgInitW = that.imgW = image.width;
  252. that.imgInitH = that.imgH = image.height;
  253. that.imgUrl=image.src;
  254. if(that.options.modal){ that.createModal(); }
  255. if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
  256. that.saveImgUrl="";
  257. that.obj.append('<img src="'+image.src+'">');
  258. that.initCropper();
  259. that.hideLoader();
  260. if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
  261. }
  262. };
  263. reader.readAsDataURL(file);
  264. }
  265. } else {
  266. try {
  267. // other modern browsers
  268. formData = new FormData(that.form[0]);
  269. } catch(e) {
  270. // IE10 MUST have all form items appended as individual form key / value pairs
  271. formData = new FormData();
  272. formData.append('img', that.form.find("input[type=file]")[0].files[0]);
  273. }
  274. for (var key in that.options.uploadData) {
  275. if( that.options.uploadData.hasOwnProperty(key) ) {
  276. formData.append( key , that.options.uploadData[key] );
  277. }
  278. }
  279. $.ajax({
  280. url: that.options.uploadUrl,
  281. data: formData,
  282. context: document.body,
  283. cache: false,
  284. contentType: false,
  285. processData: false,
  286. type: 'POST'
  287. }).always(function (data) {
  288. that.afterUpload(data);
  289. });
  290. }
  291. });
  292. },
  293. takingCorrect:function(imageSrc){
  294. var that = this;
  295. var formData = new FormData();
  296. var mime =that.getFileMime(imageSrc);
  297. formData.append("imgUrl", that.convertBase64UrlToBlob(imageSrc));
  298. var xhr = new XMLHttpRequest();
  299. xhr.open("POST", "taking",true);
  300. xhr.responseType = "blob";
  301. xhr.timeout = 60000*5;
  302. xhr.onload= function() {
  303. if (xhr.readyState === 4 && xhr.status === 200) {
  304. var blob = this.response;
  305. var Error=this.getResponseHeader("Content-My-Error");
  306. if (Error!=null)
  307. {
  308. var ErrorMeg=decodeURIComponent(atob(Error).split('').map(function (c) {
  309. return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  310. }).join(''));
  311. if (that.options.onError) that.options.onError.call(that,ErrorMeg);
  312. that.imgUploadControl.show();
  313. that.hideLoader();
  314. return;
  315. }
  316. var filename=xhr.getResponseHeader("Content-My-filename");
  317. if (filename!=null)
  318. {
  319. that.imgName=filename;
  320. }
  321. var reader = new FileReader();
  322. reader.onload = function (e) {
  323. var image = new Image();
  324. image.src = e.target.result;
  325. image.onload = function(){
  326. that.imgInitW = that.imgW = image.width;
  327. that.imgInitH = that.imgH = image.height;
  328. var arr = image.src.split(',');
  329. that.imgUrl= 'data:'+ mime+';base64,'+arr[1];
  330. if(that.options.modal){ that.createModal(); }
  331. if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
  332. that.saveImgUrl="";
  333. that.obj.append('<img src="'+ that.imgUrl+'">');
  334. that.initCropper();
  335. that.hideLoader();
  336. if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
  337. }
  338. }
  339. reader.readAsDataURL(blob);
  340. }else {
  341. if (that.options.onError) that.options.onError.call(that,"处理失败");
  342. that.imgUploadControl.show();
  343. that.hideLoader();
  344. }
  345. };
  346. xhr.send(formData);
  347. }
  348. ,loadExistingImage: function(){
  349. var that = this;
  350. if( $.isEmptyObject(that.croppedImg)){
  351. if (that.options.onBeforeImgUpload) that.options.onBeforeImgUpload.call(that);
  352. that.showLoader();
  353. if(that.options.modal){ that.createModal(); }
  354. if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
  355. that.imgUrl=that.options.loadPicture ;
  356. var img =$('<img src="'+ that.options.loadPicture +'">');
  357. that.obj.append(img);
  358. img.load(function() {
  359. that.imgInitW = that.imgW = this.width;
  360. that.imgInitH = that.imgH = this.height;
  361. that.initCropper();
  362. that.hideLoader();
  363. if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
  364. });
  365. }else{
  366. that.cropControlRemoveCroppedImage.on('click',function(){
  367. var res = confirm('您是要使用当前图片还是重新选择图片?\n\r' +
  368. '[确认] 使用当前图片\n\r' +
  369. '[取消] 重新选择图片');
  370. var width= that.imgInitW;
  371. var height= that.imgInitH;
  372. that.croppedImg.remove();
  373. $(this).hide();
  374. that.cropSaveControl.hide();
  375. that.cropControlOriginal.hide();
  376. if( !$.isEmptyObject(that.defaultImg)){
  377. that.obj.append(that.defaultImg);
  378. }
  379. if(that.options.outputUrlId !== ''){ $('#'+that.options.outputUrlId).val(''); }
  380. that.croppedImg = '';
  381. that.reset();
  382. if (res) {
  383. that.showLoader();
  384. that.imgUploadControl.hide();
  385. if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
  386. that.imgInitW = that.imgW = width;
  387. that.imgInitH = that.imgH = height;
  388. that.saveImgUrl="";
  389. that.obj.append('<img src="'+that.imgUrl+'">');
  390. that.initCropper();
  391. that.hideLoader();
  392. }
  393. });
  394. }
  395. },
  396. afterUpload: function(data){
  397. var that = this;
  398. response = typeof data =='object' ? data : jQuery.parseJSON(data);
  399. if (response.status == 'success') {
  400. that.imgInitW = that.imgW = response.width;
  401. that.imgInitH = that.imgH = response.height;
  402. if (that.options.modal) { that.createModal(); }
  403. if (!$.isEmptyObject(that.croppedImg)) { that.croppedImg.remove(); }
  404. that.imgUrl = response.url;
  405. var img = $('<img src="'+response.url+'">')
  406. that.obj.append(img);
  407. img.load(function(){
  408. that.initCropper();
  409. that.hideLoader();
  410. if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
  411. });
  412. if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
  413. }
  414. if (response.status == 'error') {
  415. alert(response.message);
  416. if (that.options.onError) that.options.onError.call(that,response.message);
  417. that.hideLoader();
  418. // setTimeout( function(){ that.reset(); },2000)
  419. }
  420. },
  421. createModal: function(){
  422. var that = this;
  423. var marginTop = that.windowH/2-that.objH/2;
  424. 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>';
  425. $('body').append(modalHTML);
  426. that.modal = $('#croppicModal');
  427. that.obj = $('#croppicModalObj');
  428. },
  429. destroyModal: function(){
  430. var that = this;
  431. that.obj = that.outputDiv;
  432. that.modal.remove();
  433. that.modal = {};
  434. },
  435. initCropper: function(){
  436. var that = this;
  437. /*SET UP SOME VARS*/
  438. that.img = that.obj.find('img');
  439. that.img.wrap('<div class="cropImgWrapper" style="overflow:hidden; z-index:1; position:absolute; width:'+that.objW+'px; height:'+that.objH+'px;"></div>');
  440. /*INIT DRAGGING*/
  441. that.createCropControls();
  442. if(that.options.imgEyecandy){ that.createEyecandy(); }
  443. that.initDrag();
  444. that.initialScaleImg();
  445. },
  446. createEyecandy: function(){
  447. var that = this;
  448. that.imgEyecandy = that.img.clone();
  449. that.imgEyecandy.css({'z-index':'0','opacity':that.options.imgEyecandyOpacity}).appendTo(that.obj);
  450. },
  451. destroyEyecandy: function(){
  452. var that = this;
  453. that.imgEyecandy.remove();
  454. },
  455. initialScaleImg:function(){
  456. var that = this;
  457. that.zoom(0);
  458. that.zoom(that.options.initialZoom);
  459. // Adding mousewheel zoom capabilities
  460. if (that.options.enableMousescroll){
  461. that.img.on('mousewheel', function(event) {
  462. event.preventDefault();
  463. if (that.colorAbsorption)
  464. {
  465. return;
  466. }
  467. that.zoom(that.options.zoomFactor*event.deltaY);
  468. });
  469. }
  470. // initial center image
  471. that.img.css({'left': -(that.imgW -that.objW)/2, 'top': -(that.imgH -that.objH)/2, 'position':'relative'});
  472. if(that.options.imgEyecandy){ that.imgEyecandy.css({'left': -(that.imgW -that.objW)/2, 'top': -(that.imgH -that.objH)/2, 'position':'relative'}); }
  473. },
  474. createCropControls: function(){
  475. var that = this;
  476. // CREATE CONTROLS
  477. var cropControlZoomMuchIn = '';
  478. var cropControlZoomIn = '<i class="cropControlZoomIn"></i>';
  479. var cropControlZoomOut = '<i class="cropControlZoomOut"></i>';
  480. var cropControlZoomMuchOut = '';
  481. var cropControlRotateLeft = '';
  482. var cropControlRotateRight = '';
  483. var cropControlRotateMuchLeft = '';
  484. var cropControlRotateMuchRight = '';
  485. var cropControlCrop = '<i class="cropControlCrop"></i>';
  486. var cropControlReset = '<i class="cropControlReset"></i>';
  487. var cropControlOriginal = '<i class="cropControlOriginal"></i>';
  488. var cropControlXiColor = '<i class="cropControlXiColor"></i>';
  489. var cropControlXiColor2 = '<i class="cropControlXiColor2"></i>';
  490. var cropControlSetColor = '<i class="cropControlSetColor"></i>';
  491. var html;
  492. if(that.options.doubleZoomControls){
  493. cropControlZoomMuchIn = '<i class="cropControlZoomMuchIn"></i>';
  494. cropControlZoomMuchOut = '<i class="cropControlZoomMuchOut"></i>';
  495. }
  496. if(that.options.rotateControls){
  497. cropControlRotateLeft = '<i class="cropControlRotateLeft"></i>';
  498. cropControlRotateRight = '<i class="cropControlRotateRight"></i>';
  499. cropControlRotateMuchLeft = '<i class="cropControlRotateMuchLeft"></i>';
  500. cropControlRotateMuchRight = '<i class="cropControlRotateMuchRight"></i>';
  501. }
  502. html = '<div class="cropControls cropControlsCrop">'+ cropControlZoomMuchIn + cropControlZoomIn + cropControlZoomOut + cropControlZoomMuchOut
  503. + cropControlRotateMuchLeft+ cropControlRotateLeft + cropControlRotateRight + cropControlRotateMuchRight + cropControlCrop + '</div>';
  504. that.obj.append(html);
  505. html = '<div class="cropControls cropControlsCrop cropControlsUpload">' + cropControlXiColor2 + cropControlXiColor+ cropControlSetColor + cropControlOriginal + cropControlReset + '</div>';
  506. that.obj.append(html);
  507. that.cropControlsCrop = that.obj.find('.cropControlsCrop');
  508. // CACHE AND BIND CONTROLS
  509. if(that.options.doubleZoomControls){
  510. that.cropControlZoomMuchIn = that.cropControlsCrop.find('.cropControlZoomMuchIn');
  511. that.cropControlZoomMuchIn.on('click',function(){ that.zoom( that.options.zoomFactor*10 ); });
  512. that.cropControlZoomMuchOut = that.cropControlsCrop.find('.cropControlZoomMuchOut');
  513. that.cropControlZoomMuchOut.on('click',function(){ that.zoom(-that.options.zoomFactor*10); });
  514. }
  515. that.cropControlZoomIn = that.cropControlsCrop.find('.cropControlZoomIn');
  516. that.cropControlZoomIn.on('click',function(){ that.zoom(that.options.zoomFactor); });
  517. that.cropControlZoomOut = that.cropControlsCrop.find('.cropControlZoomOut');
  518. that.cropControlZoomOut.on('click',function(){ that.zoom(-that.options.zoomFactor); });
  519. that.cropControlRotateLeft = that.cropControlsCrop.find('.cropControlRotateLeft');
  520. that.cropControlRotateLeft.on('click', function() { that.rotate(-0.5);});
  521. that.cropControlRotateMuchLeft = that.cropControlsCrop.find('.cropControlRotateMuchLeft');
  522. that.cropControlRotateMuchLeft.on('click', function() { that.rotate(-10);});
  523. that.cropControlRotateRight = that.cropControlsCrop.find('.cropControlRotateRight');
  524. that.cropControlRotateRight.on('click', function() {that.rotate(0.5);});
  525. that.cropControlRotateMuchRight = that.cropControlsCrop.find('.cropControlRotateMuchRight');
  526. that.cropControlRotateMuchRight.on('click', function() {that.rotate(10);});
  527. that.cropControlCrop = that.cropControlsCrop.find('.cropControlCrop');
  528. that.cropControlCrop.on('click',function(){ that.crop(); });
  529. that.cropControlReset = that.cropControlsCrop.find('.cropControlReset');
  530. that.cropControlReset.on('click',function(){
  531. var res = confirm('您是要使用当前图片还是重新选择图片?\n\r' +
  532. '[确认] 使用当前图片\n\r' +
  533. '[取消] 重新选择图片');
  534. var width= that.imgInitW;
  535. var height= that.imgInitH;
  536. that.reset();
  537. if (res) {
  538. that.showLoader();
  539. that.imgUploadControl.hide();
  540. if( !$.isEmptyObject(that.croppedImg)){ that.croppedImg.remove(); }
  541. that.imgInitW = that.imgW = width;
  542. that.imgInitH = that.imgH = height;
  543. that.saveImgUrl="";
  544. that.obj.append('<img src="'+that.imgUrl+'">');
  545. that.initCropper();
  546. that.hideLoader();
  547. }
  548. });
  549. that.cropControlOriginal = that.cropControlsCrop.find('.cropControlOriginal');
  550. that.cropControlOriginal.on('click',function(){
  551. if (that.imgUrl=='')
  552. {
  553. alert('请选择图片' );
  554. return;
  555. }
  556. var res = confirm('您确定要使用原图吗?\n\r' +
  557. '[确认] 使用原图\n\r' +
  558. '[取消] 继续编辑');
  559. if (res) {
  560. that.destroy();
  561. window.location.href="https://wwww.croppic.com?"+that.imgUrl;
  562. }
  563. });
  564. that.cropControlXiColor = that.cropControlsCrop.find('.cropControlXiColor');
  565. that.cropControlXiColor.on('click',function(){that.colorOnClick(false);});
  566. that.cropControlXiColor2 = that.cropControlsCrop.find('.cropControlXiColor2');
  567. that.cropControlXiColor2.on('click',function(){that.colorOnClick(false);});
  568. that.cropControlSetColor= that.cropControlsCrop.find('.cropControlSetColor');
  569. that.cropControlSetColor.on('click',function(){that.colorOnClick(true);});
  570. that.colpick=that.cropControlSetColor.colpick({
  571. onSubmit:function(hsb,hex,rgb,el) {
  572. var background="rgb("+rgb.r+","+rgb.g+","+rgb.b+")";
  573. that.obj.find('.cropImgWrapper').css('background',background);
  574. $(el).colpickHide();
  575. that.imgBackground=background;
  576. }
  577. });
  578. },
  579. colorOnClick:function(input)
  580. {
  581. var that = this;
  582. if (that.colorAbsorption||input)
  583. {
  584. that.cropControlsCrop.find('.cropControlXiColor').css("display","block");
  585. that.cropControlsCrop.find('.cropControlXiColor2').css("display","none");
  586. that.colorAbsorption=false;
  587. }else {
  588. that.cropControlsCrop.find('.cropControlXiColor').css("display","none");
  589. that.cropControlsCrop.find('.cropControlXiColor2').css("display","block");
  590. that.colorAbsorption=true;
  591. }
  592. },
  593. absorption:function(pageX,pageY){
  594. var that = this;
  595. that.showLoader();
  596. that.cropControlsCrop.hide();
  597. var cropData = {
  598. imgInitW:that.imgInitW,
  599. imgInitH:that.imgInitH,
  600. imgW:that.imgW,
  601. imgH:that.imgH,
  602. imgY1:parseInt( that.img.css('top') ),
  603. imgX1: parseInt( that.img.css('left')),
  604. cropH:that.objH,
  605. cropW:that.objW,
  606. rotation:that.actualRotation,
  607. pageX:pageX,
  608. pageY:pageY,
  609. imgBackground:that.imgBackground
  610. };
  611. var formData = new FormData();
  612. for (var key in cropData) {
  613. if( cropData.hasOwnProperty(key) ) {
  614. formData.append( key , cropData[key] );
  615. }
  616. }
  617. if (that.imgName=='')
  618. {
  619. formData.append("imgUrl", that.convertBase64UrlToBlob(that.imgUrl));
  620. }else {
  621. formData.append( "imgName" , that.imgName );
  622. }
  623. $.ajax({
  624. url: that.options.absorptionUrl,
  625. data: formData,
  626. cache: false,
  627. contentType: false,
  628. processData: false,
  629. type: 'POST'
  630. }).always(function (data) {
  631. var response;
  632. try {
  633. response = jQuery.parseJSON(data);
  634. }
  635. catch(err) {
  636. response = typeof data =='object' ? data : jQuery.parseJSON(data);
  637. }
  638. if (response.status == 'success') {
  639. that.imgName=response.imgName;
  640. var rgb={r:response.r,g:response.g,b:response.b};
  641. var background="rgb("+rgb.r+","+rgb.g+","+rgb.b+")";
  642. that.obj.find('.cropImgWrapper').css("background",background);
  643. that.imgBackground=background;
  644. that.cropControlSetColor.colpickSetColor(rgb);
  645. }else if (response.status == 'error') {
  646. if (that.options.onError) that.options.onError.call(that,response.message);
  647. }else {
  648. if (that.options.onError) that.options.onError.call(that,"处理失败");
  649. }
  650. that.cropControlsCrop.show();
  651. that.hideLoader();
  652. });
  653. },
  654. initDrag:function(){
  655. var that = this;
  656. that.img.on("mousedown touchstart", function(e) {
  657. e.preventDefault(); // disable selection
  658. var pageX;
  659. var pageY;
  660. var userAgent = window.navigator.userAgent;
  661. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i) || (e.pageY && e.pageX) == undefined) {
  662. pageX = e.originalEvent.touches[0].pageX;
  663. pageY = e.originalEvent.touches[0].pageY;
  664. } else {
  665. pageX = e.pageX;
  666. pageY = e.pageY;
  667. }
  668. var z_idx = that.img.css('z-index'),
  669. drg_h = that.img.outerHeight(),
  670. drg_w = that.img.outerWidth(),
  671. pos_y = that.img.offset().top + drg_h - pageY,
  672. pos_x = that.img.offset().left + drg_w - pageX;
  673. that.z_idx=z_idx;
  674. that.drg_h=drg_h;
  675. that.drg_w=drg_w;
  676. that.pos_y=pos_y;
  677. that.pos_x=pos_x;
  678. if (that.colorAbsorption)
  679. {
  680. if (that.actualRotation!==0)
  681. {
  682. that.absorption(pageX-10,pageY-50);
  683. return;
  684. }
  685. var ml=that.imgInitW/that.imgW;
  686. var x = pageX-parseInt( that.img.css('left'))-10;
  687. var y = pageY-parseInt( that.img.css('top'))-50;
  688. x=parseInt(x*ml);
  689. y=parseInt(y*ml);
  690. if (!(x<0||x>that.imgInitW||y<0||y>that.imgInitH))
  691. {
  692. var imgData={};
  693. if( $.isEmptyObject(that.ctx))
  694. {
  695. var canvas = document.createElement("canvas");
  696. if (!canvas.getContext) {
  697. alert("浏览器版本太低无法使用吸色!");
  698. return;
  699. }
  700. that.ctx= canvas.getContext("2d");
  701. var newImg = new Image();
  702. newImg.onload = function(){
  703. canvas.width = that.imgInitW;
  704. canvas.height = that.imgInitH;
  705. that.ctx.drawImage(newImg, 0, 0);
  706. imgData = that.ctx.getImageData(x, y, 1, 1);
  707. var rgb = {r:imgData.data[0],g:imgData.data[1],b:imgData.data[2]};
  708. var background="rgb("+rgb.r+","+rgb.g+","+rgb.b+")";
  709. that.obj.find('.cropImgWrapper').css("background",background);
  710. that.imgBackground=background;
  711. that.cropControlSetColor.colpickSetColor(rgb);
  712. }
  713. newImg.src = that.imgUrl;
  714. }else {
  715. imgData = that.ctx.getImageData(x, y, 1, 1);
  716. var rgb = {r:imgData.data[0],g:imgData.data[1],b:imgData.data[2]};
  717. var background="rgb("+rgb.r+","+rgb.g+","+rgb.b+")";
  718. that.obj.find('.cropImgWrapper').css("background",background);
  719. that.imgBackground=background;
  720. that.cropControlSetColor.colpickSetColor(rgb);
  721. }
  722. }
  723. return;
  724. }
  725. }).on("mouseup", function() {
  726. that.img.off("mousemove");
  727. }).on("mouseout", function() {
  728. that.img.off("mousemove");
  729. });
  730. that.img.css('z-index', 1000).on("mousemove touchmove", function(e) {
  731. e.preventDefault();
  732. if (that.colorAbsorption)
  733. {
  734. return;
  735. }
  736. var z_idx=that.z_idx;
  737. var drg_h=that.drg_h;
  738. var drg_w=that.drg_w;
  739. var pos_y=that.pos_y;
  740. var pos_x=that.pos_x;
  741. var imgTop;
  742. var imgLeft;
  743. var userAgent = window.navigator.userAgent;
  744. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i) || (e.pageY && e.pageX) == undefined) {
  745. imgTop = e.originalEvent.touches[0].pageY + pos_y - drg_h;
  746. imgLeft = e.originalEvent.touches[0].pageX + pos_x - drg_w;
  747. } else {
  748. imgTop = e.pageY + pos_y - drg_h;
  749. imgLeft = e.pageX + pos_x - drg_w;
  750. }
  751. that.img.offset({
  752. top:imgTop,
  753. left:imgLeft
  754. }).on("mouseup", function() {
  755. $(this).removeClass('draggable').css('z-index', z_idx);
  756. });
  757. if(that.options.imgEyecandy){ that.imgEyecandy.offset({ top:imgTop, left:imgLeft }); }
  758. var top=that.actualRotationImgH;
  759. var left=that.actualRotationImgW;
  760. if (that.objH < that.imgH+top*2) {
  761. if (parseInt(that.img.css('top')) > top) { that.img.css('top', top); if (that.options.imgEyecandy) { that.imgEyecandy.css('top', top);}}
  762. 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); }}
  763. }else{
  764. if (parseInt(that.img.css('top')) < -top) { that.img.css('top', -top); if (that.options.imgEyecandy) { that.imgEyecandy.css('top', -top); }}
  765. 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); }}
  766. }
  767. if (that.objW < that.imgW+2*left) {
  768. if( parseInt( that.img.css('left')) > left ){ that.img.css('left',left); if(that.options.imgEyecandy){ that.imgEyecandy.css('left', left); }}
  769. 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); } }
  770. }else{
  771. if( parseInt( that.img.css('left')) < -left ){ that.img.css('left',-left); if(that.options.imgEyecandy){ that.imgEyecandy.css('left', -left); }}
  772. 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); } }
  773. }
  774. if (that.options.onImgDrag) that.options.onImgDrag.call(that);
  775. });
  776. },
  777. rotate: function(x) {
  778. var that = this;
  779. that.colorOnClick(true);
  780. that.actualRotation += x;
  781. that.img.css({
  782. '-webkit-transform': 'rotate(' + that.actualRotation + 'deg)',
  783. '-moz-transform': 'rotate(' + that.actualRotation + 'deg)',
  784. 'transform': 'rotate(' + that.actualRotation + 'deg)',
  785. });
  786. if(that.options.imgEyecandy) {
  787. that.imgEyecandy.css({
  788. '-webkit-transform': 'rotate(' + that.actualRotation + 'deg)',
  789. '-moz-transform': 'rotate(' + that.actualRotation + 'deg)',
  790. 'transform': 'rotate(' + that.actualRotation + 'deg)',
  791. });
  792. }
  793. that.actualRotationImgH=0;
  794. that.actualRotationImgW=0;
  795. if (that.actualRotation!=0)
  796. {
  797. var actualRotation=that.actualRotation%360;
  798. if (actualRotation<0)
  799. {
  800. actualRotation=0-actualRotation;
  801. }
  802. if (actualRotation==90||actualRotation==270)
  803. {
  804. if(that.imgH>that.imgW)
  805. {
  806. that.actualRotationImgW=parseInt((that.imgH-that.imgW)/2);
  807. }else if(that.imgH<that.imgW)
  808. {
  809. that.actualRotationImgH=parseInt((that.imgW-that.imgH)/2);
  810. }
  811. }else if (actualRotation!=180&&actualRotation!=360) {
  812. if (actualRotation>180)
  813. {
  814. actualRotation=actualRotation-180;
  815. }
  816. if (actualRotation>90)
  817. {
  818. actualRotation=180-actualRotation;
  819. }
  820. var radian = Math.sin(actualRotation * Math.PI / 180);
  821. that.actualRotationImgW=parseInt(radian*(that.imgH)/2);
  822. that.actualRotationImgH=parseInt(radian*(that.imgW)/2);
  823. }
  824. }
  825. if (typeof that.options.onImgRotate == 'function')
  826. that.options.onImgRotate.call(that);
  827. },
  828. zoom :function(x){
  829. var that = this;
  830. that.colorOnClick(true);
  831. var ratio = that.imgW / that.imgH;
  832. var newWidth = that.imgW+x;
  833. var newHeight = newWidth/ratio;
  834. var doPositioning = true;
  835. if (x==0)
  836. {
  837. if( newWidth - that.objW < newHeight - that.objH ){
  838. newWidth = that.objW;
  839. newHeight = newWidth/ratio;
  840. }else{
  841. newHeight = that.objH;
  842. newWidth = ratio * newHeight;
  843. }
  844. }
  845. if( newWidth < that.objW || newHeight < that.objH){
  846. doPositioning = false;
  847. }
  848. if(!that.options.scaleToFill && (newWidth > that.imgInitW || newHeight > that.imgInitH)){
  849. doPositioning = false;
  850. }
  851. that.imgW = newWidth;
  852. that.img.width(newWidth);
  853. that.imgH = newHeight;
  854. that.img.height(newHeight);
  855. var newTop = parseInt( that.img.css('top') ) - x/2;
  856. var newLeft = parseInt( that.img.css('left') ) - x/2;
  857. that.actualRotationImgH=0;
  858. that.actualRotationImgW=0;
  859. if (that.actualRotation!=0)
  860. {
  861. var actualRotation=that.actualRotation%360;
  862. if (actualRotation<0)
  863. {
  864. actualRotation=0-actualRotation;
  865. }
  866. if (actualRotation==90||actualRotation==270)
  867. {
  868. if(that.imgH>that.imgW)
  869. {
  870. that.actualRotationImgW=parseInt((that.imgH-that.imgW)/2);
  871. }else if(that.imgH<that.imgW)
  872. {
  873. that.actualRotationImgH=parseInt((that.imgW-that.imgH)/2);
  874. }
  875. }else if (actualRotation!=180&&actualRotation!=360) {
  876. if (actualRotation>180)
  877. {
  878. actualRotation=actualRotation-180;
  879. }
  880. if (actualRotation>90)
  881. {
  882. actualRotation=180-actualRotation;
  883. }
  884. var radian = Math.sin(actualRotation * Math.PI / 180);
  885. that.actualRotationImgW=parseInt(radian*(that.imgH)/2);
  886. that.actualRotationImgH=parseInt(radian*(that.imgW)/2);
  887. }
  888. }
  889. if( newTop>-that.actualRotationImgH ){ newTop=-that.actualRotationImgH;}
  890. if( newLeft>-that.actualRotationImgW ){ newLeft=-that.actualRotationImgW;}
  891. var maxTop = -( newHeight-that.objH+that.actualRotationImgH); if( newTop < maxTop){ newTop = maxTop; }
  892. var maxLeft = -( newWidth-that.objW+that.actualRotationImgW); if( newLeft < maxLeft){ newLeft = maxLeft; }
  893. if( doPositioning ){
  894. that.img.css({'top':newTop, 'left':newLeft});
  895. }
  896. if(that.options.imgEyecandy){
  897. that.imgEyecandy.width(newWidth);
  898. that.imgEyecandy.height(newHeight);
  899. if( doPositioning ){
  900. that.imgEyecandy.css({'top':newTop, 'left':newLeft});
  901. }
  902. }
  903. if (that.options.onImgZoom) that.options.onImgZoom.call(that);
  904. },
  905. crop:function(){
  906. var that = this;
  907. that.colorOnClick(true);
  908. if (that.options.onBeforeImgCrop) that.options.onBeforeImgCrop.call(that);
  909. that.cropControlsCrop.hide();
  910. that.showLoader();
  911. var cropData = {
  912. imgInitW:that.imgInitW,
  913. imgInitH:that.imgInitH,
  914. imgW:that.imgW,
  915. imgH:that.imgH,
  916. imgY1:parseInt( that.img.css('top') ),
  917. imgX1: parseInt( that.img.css('left')),
  918. cropH:that.objH,
  919. cropW:that.objW,
  920. rotation:that.actualRotation,
  921. imgBackground:that.imgBackground
  922. };
  923. var formData = new FormData();
  924. for (var key in cropData) {
  925. if( cropData.hasOwnProperty(key) ) {
  926. formData.append( key , cropData[key] );
  927. }
  928. }
  929. for (var key in that.options.cropData) {
  930. if( that.options.cropData.hasOwnProperty(key) ) {
  931. formData.append( key , that.options.cropData[key] );
  932. }
  933. }
  934. if (that.imgName=='')
  935. {
  936. formData.append("imgUrl", that.convertBase64UrlToBlob(that.imgUrl));
  937. }else {
  938. formData.append( "imgName" , that.imgName );
  939. }
  940. var xhr = new XMLHttpRequest();
  941. xhr.open("POST", that.options.cropUrl);
  942. xhr.responseType = "blob";
  943. xhr.timeout = 60000*5;
  944. xhr.onload= function() {
  945. that.afterCrop(xhr);
  946. };
  947. xhr.send(formData);
  948. },
  949. convertBase64UrlToBlob:function (urlData) {
  950. var arr = urlData.split(',');
  951. var mime = arr[0].match(/:(.*?);/)[1];
  952. var bytes = window.atob(urlData.split(',')[1]); //去掉url的头,并转换为byte
  953. //处理异常,将ascii码小于0的转换为大于0
  954. var ab = new ArrayBuffer(bytes.length);
  955. var ia = new Uint8Array(ab);
  956. for (var i = 0; i < bytes.length; i++) {
  957. ia[i] = bytes.charCodeAt(i);
  958. }
  959. return new Blob([ab], { type: mime });
  960. },
  961. getFileMime:function (urlData) {
  962. var arr = urlData.split(',');
  963. return arr[0].match(/:(.*?);/)[1];
  964. },
  965. getFileExt:function (urlData) {
  966. var arr = urlData.split(',');
  967. var mime = arr[0].match(/:(.*?);/)[1];
  968. var FileExt;
  969. switch (mime)
  970. {
  971. case "image/jpeg":
  972. FileExt="jpg";
  973. break;
  974. case "image/png":
  975. FileExt="png";
  976. break;
  977. case "image/gif":
  978. FileExt="gif";
  979. break;
  980. default:
  981. FileExt="jpg";
  982. }
  983. return FileExt;
  984. },
  985. afterCrop: function (xhr) {
  986. var that = this;
  987. if (xhr.readyState === 4 && xhr.status === 200) {
  988. var Error=xhr.getResponseHeader("Content-My-Error");
  989. if (Error!=null)
  990. {
  991. var ErrorMeg=decodeURIComponent(atob(Error).split('').map(function (c) {
  992. return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  993. }).join(''));
  994. if (that.options.onError) that.options.onError.call(that,ErrorMeg);
  995. that.cropControlsCrop.show();
  996. that.hideLoader();
  997. return;
  998. }
  999. if (that.options.imgEyecandy)
  1000. that.imgEyecandy.hide();
  1001. var filename=xhr.getResponseHeader("Content-My-filename");
  1002. var blob = xhr.response;
  1003. var reader = new FileReader();
  1004. reader.onload = function (e) {
  1005. that.destroy();
  1006. that.saveImgUrl='product_image_crop_md5,'+filename+','+e.target.result;
  1007. var croppedImg='<img class="croppedImg" src="' + e.target.result + '">';
  1008. that.obj.append(croppedImg);
  1009. if (that.options.outputUrlId !== '') { $('#' + that.options.outputUrlId).val(response.url); }
  1010. that.croppedImg = that.obj.find('.croppedImg');
  1011. that.croppedImg .width(that.objW+"px").height(that.objH+"px");
  1012. that.init();
  1013. that.hideLoader();
  1014. that.imgUploadControl.hide();
  1015. that.cropSaveControl.show();
  1016. }
  1017. reader.readAsDataURL(blob);
  1018. }else {
  1019. if (that.options.onError) that.options.onError.call(that,"处理失败");
  1020. that.hideLoader();
  1021. that.cropControlsCrop.show();
  1022. }
  1023. if (that.options.onAfterImgCrop) that.options.onAfterImgCrop.call(that, response);
  1024. },
  1025. showLoader:function(){
  1026. var that = this;
  1027. that.obj.append(that.options.loaderHtml);
  1028. that.loader = that.obj.find('.loader');
  1029. },
  1030. hideLoader:function(){
  1031. var that = this;
  1032. that.loader.remove();
  1033. },
  1034. reset:function(){
  1035. var that = this;
  1036. that.destroy();
  1037. that.init();
  1038. that.imgUploadControl.show();
  1039. if( !$.isEmptyObject(that.croppedImg)){
  1040. that.obj.append(that.croppedImg);
  1041. if(that.options.outputUrlId !== ''){ $('#'+that.options.outputUrlId).val(that.croppedImg.attr('url')); }
  1042. }
  1043. if (typeof that.options.onReset == 'function')
  1044. that.options.onReset.call(that);
  1045. },
  1046. destroy:function(){
  1047. var that = this;
  1048. that.colorOnClick(true);
  1049. if(that.options.modal && !$.isEmptyObject(that.modal) ){ that.destroyModal(); }
  1050. if(that.options.imgEyecandy && !$.isEmptyObject(that.imgEyecandy) ){ that.destroyEyecandy(); }
  1051. if( !$.isEmptyObject( that.cropControlsUpload ) ){ that.cropControlsUpload.remove(); }
  1052. if( !$.isEmptyObject( that.cropControlsCrop ) ){ that.cropControlsCrop.remove(); }
  1053. if( !$.isEmptyObject( that.loader ) ){ that.loader.remove(); }
  1054. if( !$.isEmptyObject( that.form ) ){ that.form.remove(); }
  1055. that.obj.html('');
  1056. },
  1057. isAjaxUploadSupported: function () {
  1058. var input = document.createElement("input");
  1059. input.type = "file";
  1060. return (
  1061. "multiple" in input &&
  1062. typeof File != "undefined" &&
  1063. typeof FormData != "undefined" &&
  1064. typeof (new XMLHttpRequest()).upload != "undefined");
  1065. },
  1066. CreateFallbackIframe: function () {
  1067. var that = this;
  1068. if (!that.isAjaxUploadSupported()) {
  1069. if (jQuery.isEmptyObject(that.iframeobj)) {
  1070. var iframe = document.createElement("iframe");
  1071. iframe.setAttribute("id", that.id + "_upload_iframe");
  1072. iframe.setAttribute("name", that.id + "_upload_iframe");
  1073. iframe.setAttribute("width", "0");
  1074. iframe.setAttribute("height", "0");
  1075. iframe.setAttribute("border", "0");
  1076. iframe.setAttribute("src", "javascript:false;");
  1077. iframe.style.display = "none";
  1078. document.body.appendChild(iframe);
  1079. } else {
  1080. iframe = that.iframeobj[0];
  1081. }
  1082. var myContent = '<!DOCTYPE html>'
  1083. + '<html><head><title>Uploading File</title></head>'
  1084. + '<body>'
  1085. + '<form '
  1086. + 'class="' + that.id + '_upload_iframe_form" '
  1087. + 'name="' + that.id + '_upload_iframe_form" '
  1088. + 'action="' + that.options.uploadUrl + '" method="post" '
  1089. + 'enctype="multipart/form-data" encoding="multipart/form-data" style="display:none;">'
  1090. + $("#" + that.id + '_imgUploadField')[0].outerHTML
  1091. + '</form></body></html>';
  1092. iframe.contentWindow.document.open('text/htmlreplace');
  1093. iframe.contentWindow.document.write(myContent);
  1094. iframe.contentWindow.document.close();
  1095. that.iframeobj = $("#" + that.id + "_upload_iframe");
  1096. that.iframeform = that.iframeobj.contents().find("html").find("." + that.id + "_upload_iframe_form");
  1097. that.iframeform.on("change", "input", function () {
  1098. that.SubmitFallbackIframe(that);
  1099. });
  1100. that.iframeform.find("input")[0].attachEvent("onchange", function () {
  1101. that.SubmitFallbackIframe(that);
  1102. });
  1103. var eventHandlermyFile = function () {
  1104. if (iframe.detachEvent)
  1105. iframe.detachEvent("onload", eventHandlermyFile);
  1106. else
  1107. iframe.removeEventListener("load", eventHandlermyFile, false);
  1108. var response = that.getIframeContentJSON(iframe);
  1109. if (jQuery.isEmptyObject(that.modal)) {
  1110. that.afterUpload(response);
  1111. }
  1112. }
  1113. if (iframe.addEventListener)
  1114. iframe.addEventListener("load", eventHandlermyFile, true);
  1115. if (iframe.attachEvent)
  1116. iframe.attachEvent("onload", eventHandlermyFile);
  1117. return "#" + that.id + '_imgUploadField';
  1118. } else {
  1119. return "";
  1120. }
  1121. },
  1122. SubmitFallbackIframe: function (that) {
  1123. that.showLoader();
  1124. if(that.options.processInline && !that.options.uploadUrl){
  1125. if (that.options.onError){
  1126. that.options.onError.call(that,"processInline is not supported by your browser ");
  1127. that.hideLoader();
  1128. }
  1129. }else{
  1130. if (that.options.onBeforeImgUpload) that.options.onBeforeImgUpload.call(that);
  1131. that.iframeform[0].submit();
  1132. }
  1133. },
  1134. getIframeContentJSON: function (iframe) {
  1135. try {
  1136. var doc = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document,
  1137. response;
  1138. var innerHTML = doc.body.innerHTML;
  1139. if (innerHTML.slice(0, 5).toLowerCase() == "<pre>" && innerHTML.slice(-6).toLowerCase() == "</pre>") {
  1140. innerHTML = doc.body.firstChild.firstChild.nodeValue;
  1141. }
  1142. response = jQuery.parseJSON(innerHTML);
  1143. } catch (err) {
  1144. response = { success: false };
  1145. }
  1146. return response;
  1147. },
  1148. hexadecimal:function(num) {
  1149. var r = parseInt(num).toString(16);
  1150. if (r.length == 1) {
  1151. return '0' + r;
  1152. }
  1153. return r.toUpperCase();
  1154. }
  1155. };
  1156. })(window, document);