croppic.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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. // prompt(that.imgUrl);
  562. window.location.href="https://wwww.croppic.com?"+that.imgUrl;
  563. }
  564. });
  565. that.cropControlXiColor = that.cropControlsCrop.find('.cropControlXiColor');
  566. that.cropControlXiColor.on('click',function(){that.colorOnClick(false);});
  567. that.cropControlXiColor2 = that.cropControlsCrop.find('.cropControlXiColor2');
  568. that.cropControlXiColor2.on('click',function(){that.colorOnClick(false);});
  569. that.cropControlSetColor= that.cropControlsCrop.find('.cropControlSetColor');
  570. that.cropControlSetColor.on('click',function(){that.colorOnClick(true);});
  571. that.colpick=that.cropControlSetColor.colpick({
  572. onSubmit:function(hsb,hex,rgb,el) {
  573. var background="rgb("+rgb.r+","+rgb.g+","+rgb.b+")";
  574. that.obj.find('.cropImgWrapper').css('background',background);
  575. $(el).colpickHide();
  576. that.imgBackground=background;
  577. }
  578. });
  579. },
  580. colorOnClick:function(input)
  581. {
  582. var that = this;
  583. if (that.colorAbsorption||input)
  584. {
  585. that.cropControlsCrop.find('.cropControlXiColor').css("display","block");
  586. that.cropControlsCrop.find('.cropControlXiColor2').css("display","none");
  587. that.colorAbsorption=false;
  588. }else {
  589. that.cropControlsCrop.find('.cropControlXiColor').css("display","none");
  590. that.cropControlsCrop.find('.cropControlXiColor2').css("display","block");
  591. that.colorAbsorption=true;
  592. }
  593. },
  594. absorption:function(pageX,pageY){
  595. var that = this;
  596. that.showLoader();
  597. that.cropControlsCrop.hide();
  598. var cropData = {
  599. imgInitW:that.imgInitW,
  600. imgInitH:that.imgInitH,
  601. imgW:that.imgW,
  602. imgH:that.imgH,
  603. imgY1:parseInt( that.img.css('top') ),
  604. imgX1: parseInt( that.img.css('left')),
  605. cropH:that.objH,
  606. cropW:that.objW,
  607. rotation:that.actualRotation,
  608. pageX:pageX,
  609. pageY:pageY,
  610. imgBackground:that.imgBackground
  611. };
  612. var formData = new FormData();
  613. for (var key in cropData) {
  614. if( cropData.hasOwnProperty(key) ) {
  615. formData.append( key , cropData[key] );
  616. }
  617. }
  618. if (that.imgName=='')
  619. {
  620. formData.append("imgUrl", that.convertBase64UrlToBlob(that.imgUrl));
  621. }else {
  622. formData.append( "imgName" , that.imgName );
  623. }
  624. $.ajax({
  625. url: that.options.absorptionUrl,
  626. data: formData,
  627. cache: false,
  628. contentType: false,
  629. processData: false,
  630. type: 'POST'
  631. }).always(function (data) {
  632. var response;
  633. try {
  634. response = jQuery.parseJSON(data);
  635. }
  636. catch(err) {
  637. response = typeof data =='object' ? data : jQuery.parseJSON(data);
  638. }
  639. if (response.status == 'success') {
  640. that.imgName=response.imgName;
  641. var rgb={r:response.r,g:response.g,b:response.b};
  642. var background="rgb("+rgb.r+","+rgb.g+","+rgb.b+")";
  643. that.obj.find('.cropImgWrapper').css("background",background);
  644. that.imgBackground=background;
  645. that.cropControlSetColor.colpickSetColor(rgb);
  646. }else if (response.status == 'error') {
  647. if (that.options.onError) that.options.onError.call(that,response.message);
  648. }else {
  649. if (that.options.onError) that.options.onError.call(that,"处理失败");
  650. }
  651. that.cropControlsCrop.show();
  652. that.hideLoader();
  653. });
  654. },
  655. initDrag:function(){
  656. var that = this;
  657. that.img.on("mousedown touchstart", function(e) {
  658. e.preventDefault(); // disable selection
  659. var pageX;
  660. var pageY;
  661. var userAgent = window.navigator.userAgent;
  662. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i) || (e.pageY && e.pageX) == undefined) {
  663. pageX = e.originalEvent.touches[0].pageX;
  664. pageY = e.originalEvent.touches[0].pageY;
  665. } else {
  666. pageX = e.pageX;
  667. pageY = e.pageY;
  668. }
  669. var z_idx = that.img.css('z-index'),
  670. drg_h = that.img.outerHeight(),
  671. drg_w = that.img.outerWidth(),
  672. pos_y = that.img.offset().top + drg_h - pageY,
  673. pos_x = that.img.offset().left + drg_w - pageX;
  674. that.z_idx=z_idx;
  675. that.drg_h=drg_h;
  676. that.drg_w=drg_w;
  677. that.pos_y=pos_y;
  678. that.pos_x=pos_x;
  679. if (that.colorAbsorption)
  680. {
  681. if (that.actualRotation!==0)
  682. {
  683. that.absorption(pageX-10,pageY-50);
  684. return;
  685. }
  686. var ml=that.imgInitW/that.imgW;
  687. var x = pageX-parseInt( that.img.css('left'))-10;
  688. var y = pageY-parseInt( that.img.css('top'))-50;
  689. x=parseInt(x*ml);
  690. y=parseInt(y*ml);
  691. if (!(x<0||x>that.imgInitW||y<0||y>that.imgInitH))
  692. {
  693. var imgData={};
  694. if( $.isEmptyObject(that.ctx))
  695. {
  696. var canvas = document.createElement("canvas");
  697. if (!canvas.getContext) {
  698. alert("浏览器版本太低无法使用吸色!");
  699. return;
  700. }
  701. that.ctx= canvas.getContext("2d");
  702. var newImg = new Image();
  703. newImg.onload = function(){
  704. canvas.width = that.imgInitW;
  705. canvas.height = that.imgInitH;
  706. that.ctx.drawImage(newImg, 0, 0);
  707. imgData = that.ctx.getImageData(x, y, 1, 1);
  708. var rgb = {r:imgData.data[0],g:imgData.data[1],b:imgData.data[2]};
  709. var background="rgb("+rgb.r+","+rgb.g+","+rgb.b+")";
  710. that.obj.find('.cropImgWrapper').css("background",background);
  711. that.imgBackground=background;
  712. that.cropControlSetColor.colpickSetColor(rgb);
  713. }
  714. newImg.src = that.imgUrl;
  715. }else {
  716. imgData = that.ctx.getImageData(x, y, 1, 1);
  717. var rgb = {r:imgData.data[0],g:imgData.data[1],b:imgData.data[2]};
  718. var background="rgb("+rgb.r+","+rgb.g+","+rgb.b+")";
  719. that.obj.find('.cropImgWrapper').css("background",background);
  720. that.imgBackground=background;
  721. that.cropControlSetColor.colpickSetColor(rgb);
  722. }
  723. }
  724. return;
  725. }
  726. }).on("mouseup", function() {
  727. that.img.off("mousemove");
  728. }).on("mouseout", function() {
  729. that.img.off("mousemove");
  730. });
  731. that.img.css('z-index', 1000).on("mousemove touchmove", function(e) {
  732. e.preventDefault();
  733. if (that.colorAbsorption)
  734. {
  735. return;
  736. }
  737. var z_idx=that.z_idx;
  738. var drg_h=that.drg_h;
  739. var drg_w=that.drg_w;
  740. var pos_y=that.pos_y;
  741. var pos_x=that.pos_x;
  742. var imgTop;
  743. var imgLeft;
  744. var userAgent = window.navigator.userAgent;
  745. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i) || (e.pageY && e.pageX) == undefined) {
  746. imgTop = e.originalEvent.touches[0].pageY + pos_y - drg_h;
  747. imgLeft = e.originalEvent.touches[0].pageX + pos_x - drg_w;
  748. } else {
  749. imgTop = e.pageY + pos_y - drg_h;
  750. imgLeft = e.pageX + pos_x - drg_w;
  751. }
  752. that.img.offset({
  753. top:imgTop,
  754. left:imgLeft
  755. }).on("mouseup", function() {
  756. $(this).removeClass('draggable').css('z-index', z_idx);
  757. });
  758. if(that.options.imgEyecandy){ that.imgEyecandy.offset({ top:imgTop, left:imgLeft }); }
  759. var top=that.actualRotationImgH;
  760. var left=that.actualRotationImgW;
  761. if (that.objH < that.imgH+top*2) {
  762. if (parseInt(that.img.css('top')) > top) { that.img.css('top', top); if (that.options.imgEyecandy) { that.imgEyecandy.css('top', top);}}
  763. 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); }}
  764. }else{
  765. if (parseInt(that.img.css('top')) < -top) { that.img.css('top', -top); if (that.options.imgEyecandy) { that.imgEyecandy.css('top', -top); }}
  766. 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); }}
  767. }
  768. if (that.objW < that.imgW+2*left) {
  769. if( parseInt( that.img.css('left')) > left ){ that.img.css('left',left); if(that.options.imgEyecandy){ that.imgEyecandy.css('left', left); }}
  770. 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); } }
  771. }else{
  772. if( parseInt( that.img.css('left')) < -left ){ that.img.css('left',-left); if(that.options.imgEyecandy){ that.imgEyecandy.css('left', -left); }}
  773. 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); } }
  774. }
  775. if (that.options.onImgDrag) that.options.onImgDrag.call(that);
  776. });
  777. },
  778. rotate: function(x) {
  779. var that = this;
  780. that.colorOnClick(true);
  781. that.actualRotation += x;
  782. that.img.css({
  783. '-webkit-transform': 'rotate(' + that.actualRotation + 'deg)',
  784. '-moz-transform': 'rotate(' + that.actualRotation + 'deg)',
  785. 'transform': 'rotate(' + that.actualRotation + 'deg)',
  786. });
  787. if(that.options.imgEyecandy) {
  788. that.imgEyecandy.css({
  789. '-webkit-transform': 'rotate(' + that.actualRotation + 'deg)',
  790. '-moz-transform': 'rotate(' + that.actualRotation + 'deg)',
  791. 'transform': 'rotate(' + that.actualRotation + 'deg)',
  792. });
  793. }
  794. that.actualRotationImgH=0;
  795. that.actualRotationImgW=0;
  796. if (that.actualRotation!=0)
  797. {
  798. var actualRotation=that.actualRotation%360;
  799. if (actualRotation<0)
  800. {
  801. actualRotation=0-actualRotation;
  802. }
  803. if (actualRotation==90||actualRotation==270)
  804. {
  805. if(that.imgH>that.imgW)
  806. {
  807. that.actualRotationImgW=parseInt((that.imgH-that.imgW)/2);
  808. }else if(that.imgH<that.imgW)
  809. {
  810. that.actualRotationImgH=parseInt((that.imgW-that.imgH)/2);
  811. }
  812. }else if (actualRotation!=180&&actualRotation!=360) {
  813. if (actualRotation>180)
  814. {
  815. actualRotation=actualRotation-180;
  816. }
  817. if (actualRotation>90)
  818. {
  819. actualRotation=180-actualRotation;
  820. }
  821. var radian = Math.sin(actualRotation * Math.PI / 180);
  822. that.actualRotationImgW=parseInt(radian*(that.imgH)/2);
  823. that.actualRotationImgH=parseInt(radian*(that.imgW)/2);
  824. }
  825. }
  826. if (typeof that.options.onImgRotate == 'function')
  827. that.options.onImgRotate.call(that);
  828. },
  829. zoom :function(x){
  830. var that = this;
  831. that.colorOnClick(true);
  832. var ratio = that.imgW / that.imgH;
  833. var newWidth = that.imgW+x;
  834. var newHeight = newWidth/ratio;
  835. var doPositioning = true;
  836. if (x==0)
  837. {
  838. if( newWidth - that.objW < newHeight - that.objH ){
  839. newWidth = that.objW;
  840. newHeight = newWidth/ratio;
  841. }else{
  842. newHeight = that.objH;
  843. newWidth = ratio * newHeight;
  844. }
  845. }
  846. if( newWidth < that.objW || newHeight < that.objH){
  847. doPositioning = false;
  848. }
  849. if(!that.options.scaleToFill && (newWidth > that.imgInitW || newHeight > that.imgInitH)){
  850. doPositioning = false;
  851. }
  852. that.imgW = newWidth;
  853. that.img.width(newWidth);
  854. that.imgH = newHeight;
  855. that.img.height(newHeight);
  856. var newTop = parseInt( that.img.css('top') ) - x/2;
  857. var newLeft = parseInt( that.img.css('left') ) - x/2;
  858. that.actualRotationImgH=0;
  859. that.actualRotationImgW=0;
  860. if (that.actualRotation!=0)
  861. {
  862. var actualRotation=that.actualRotation%360;
  863. if (actualRotation<0)
  864. {
  865. actualRotation=0-actualRotation;
  866. }
  867. if (actualRotation==90||actualRotation==270)
  868. {
  869. if(that.imgH>that.imgW)
  870. {
  871. that.actualRotationImgW=parseInt((that.imgH-that.imgW)/2);
  872. }else if(that.imgH<that.imgW)
  873. {
  874. that.actualRotationImgH=parseInt((that.imgW-that.imgH)/2);
  875. }
  876. }else if (actualRotation!=180&&actualRotation!=360) {
  877. if (actualRotation>180)
  878. {
  879. actualRotation=actualRotation-180;
  880. }
  881. if (actualRotation>90)
  882. {
  883. actualRotation=180-actualRotation;
  884. }
  885. var radian = Math.sin(actualRotation * Math.PI / 180);
  886. that.actualRotationImgW=parseInt(radian*(that.imgH)/2);
  887. that.actualRotationImgH=parseInt(radian*(that.imgW)/2);
  888. }
  889. }
  890. if( newTop>-that.actualRotationImgH ){ newTop=-that.actualRotationImgH;}
  891. if( newLeft>-that.actualRotationImgW ){ newLeft=-that.actualRotationImgW;}
  892. var maxTop = -( newHeight-that.objH+that.actualRotationImgH); if( newTop < maxTop){ newTop = maxTop; }
  893. var maxLeft = -( newWidth-that.objW+that.actualRotationImgW); if( newLeft < maxLeft){ newLeft = maxLeft; }
  894. if( doPositioning ){
  895. that.img.css({'top':newTop, 'left':newLeft});
  896. }
  897. if(that.options.imgEyecandy){
  898. that.imgEyecandy.width(newWidth);
  899. that.imgEyecandy.height(newHeight);
  900. if( doPositioning ){
  901. that.imgEyecandy.css({'top':newTop, 'left':newLeft});
  902. }
  903. }
  904. if (that.options.onImgZoom) that.options.onImgZoom.call(that);
  905. },
  906. crop:function(){
  907. var that = this;
  908. that.colorOnClick(true);
  909. if (that.options.onBeforeImgCrop) that.options.onBeforeImgCrop.call(that);
  910. that.cropControlsCrop.hide();
  911. that.showLoader();
  912. var cropData = {
  913. imgInitW:that.imgInitW,
  914. imgInitH:that.imgInitH,
  915. imgW:that.imgW,
  916. imgH:that.imgH,
  917. imgY1:parseInt( that.img.css('top') ),
  918. imgX1: parseInt( that.img.css('left')),
  919. cropH:that.objH,
  920. cropW:that.objW,
  921. rotation:that.actualRotation,
  922. imgBackground:that.imgBackground
  923. };
  924. var formData = new FormData();
  925. for (var key in cropData) {
  926. if( cropData.hasOwnProperty(key) ) {
  927. formData.append( key , cropData[key] );
  928. }
  929. }
  930. for (var key in that.options.cropData) {
  931. if( that.options.cropData.hasOwnProperty(key) ) {
  932. formData.append( key , that.options.cropData[key] );
  933. }
  934. }
  935. if (that.imgName=='')
  936. {
  937. formData.append("imgUrl", that.convertBase64UrlToBlob(that.imgUrl));
  938. }else {
  939. formData.append( "imgName" , that.imgName );
  940. }
  941. var xhr = new XMLHttpRequest();
  942. xhr.open("POST", that.options.cropUrl);
  943. xhr.responseType = "blob";
  944. xhr.timeout = 60000*5;
  945. xhr.onload= function() {
  946. that.afterCrop(xhr);
  947. };
  948. xhr.send(formData);
  949. },
  950. convertBase64UrlToBlob:function (urlData) {
  951. var arr = urlData.split(',');
  952. var mime = arr[0].match(/:(.*?);/)[1];
  953. var bytes = window.atob(urlData.split(',')[1]); //去掉url的头,并转换为byte
  954. //处理异常,将ascii码小于0的转换为大于0
  955. var ab = new ArrayBuffer(bytes.length);
  956. var ia = new Uint8Array(ab);
  957. for (var i = 0; i < bytes.length; i++) {
  958. ia[i] = bytes.charCodeAt(i);
  959. }
  960. return new Blob([ab], { type: mime });
  961. },
  962. getFileMime:function (urlData) {
  963. var arr = urlData.split(',');
  964. return arr[0].match(/:(.*?);/)[1];
  965. },
  966. getFileExt:function (urlData) {
  967. var arr = urlData.split(',');
  968. var mime = arr[0].match(/:(.*?);/)[1];
  969. var FileExt;
  970. switch (mime)
  971. {
  972. case "image/jpeg":
  973. FileExt="jpg";
  974. break;
  975. case "image/png":
  976. FileExt="png";
  977. break;
  978. case "image/gif":
  979. FileExt="gif";
  980. break;
  981. default:
  982. FileExt="jpg";
  983. }
  984. return FileExt;
  985. },
  986. afterCrop: function (xhr) {
  987. var that = this;
  988. if (xhr.readyState === 4 && xhr.status === 200) {
  989. var Error=xhr.getResponseHeader("Content-My-Error");
  990. if (Error!=null)
  991. {
  992. var ErrorMeg=decodeURIComponent(atob(Error).split('').map(function (c) {
  993. return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  994. }).join(''));
  995. if (that.options.onError) that.options.onError.call(that,ErrorMeg);
  996. that.cropControlsCrop.show();
  997. that.hideLoader();
  998. return;
  999. }
  1000. if (that.options.imgEyecandy)
  1001. that.imgEyecandy.hide();
  1002. var filename=xhr.getResponseHeader("Content-My-filename");
  1003. var blob = xhr.response;
  1004. var reader = new FileReader();
  1005. reader.onload = function (e) {
  1006. that.destroy();
  1007. that.saveImgUrl='product_image_crop_md5,'+filename+','+e.target.result;
  1008. var croppedImg='<img class="croppedImg" src="' + e.target.result + '">';
  1009. that.obj.append(croppedImg);
  1010. if (that.options.outputUrlId !== '') { $('#' + that.options.outputUrlId).val(response.url); }
  1011. that.croppedImg = that.obj.find('.croppedImg');
  1012. that.croppedImg .width(that.objW+"px").height(that.objH+"px");
  1013. that.init();
  1014. that.hideLoader();
  1015. that.imgUploadControl.hide();
  1016. that.cropSaveControl.show();
  1017. }
  1018. reader.readAsDataURL(blob);
  1019. }else {
  1020. if (that.options.onError) that.options.onError.call(that,"处理失败");
  1021. that.hideLoader();
  1022. that.cropControlsCrop.show();
  1023. }
  1024. if (that.options.onAfterImgCrop) that.options.onAfterImgCrop.call(that, response);
  1025. },
  1026. showLoader:function(){
  1027. var that = this;
  1028. that.obj.append(that.options.loaderHtml);
  1029. that.loader = that.obj.find('.loader');
  1030. },
  1031. hideLoader:function(){
  1032. var that = this;
  1033. that.loader.remove();
  1034. },
  1035. reset:function(){
  1036. var that = this;
  1037. that.destroy();
  1038. that.init();
  1039. that.imgUploadControl.show();
  1040. if( !$.isEmptyObject(that.croppedImg)){
  1041. that.obj.append(that.croppedImg);
  1042. if(that.options.outputUrlId !== ''){ $('#'+that.options.outputUrlId).val(that.croppedImg.attr('url')); }
  1043. }
  1044. if (typeof that.options.onReset == 'function')
  1045. that.options.onReset.call(that);
  1046. },
  1047. destroy:function(){
  1048. var that = this;
  1049. that.colorOnClick(true);
  1050. if(that.options.modal && !$.isEmptyObject(that.modal) ){ that.destroyModal(); }
  1051. if(that.options.imgEyecandy && !$.isEmptyObject(that.imgEyecandy) ){ that.destroyEyecandy(); }
  1052. if( !$.isEmptyObject( that.cropControlsUpload ) ){ that.cropControlsUpload.remove(); }
  1053. if( !$.isEmptyObject( that.cropControlsCrop ) ){ that.cropControlsCrop.remove(); }
  1054. if( !$.isEmptyObject( that.loader ) ){ that.loader.remove(); }
  1055. if( !$.isEmptyObject( that.form ) ){ that.form.remove(); }
  1056. that.obj.html('');
  1057. },
  1058. isAjaxUploadSupported: function () {
  1059. var input = document.createElement("input");
  1060. input.type = "file";
  1061. return (
  1062. "multiple" in input &&
  1063. typeof File != "undefined" &&
  1064. typeof FormData != "undefined" &&
  1065. typeof (new XMLHttpRequest()).upload != "undefined");
  1066. },
  1067. CreateFallbackIframe: function () {
  1068. var that = this;
  1069. if (!that.isAjaxUploadSupported()) {
  1070. if (jQuery.isEmptyObject(that.iframeobj)) {
  1071. var iframe = document.createElement("iframe");
  1072. iframe.setAttribute("id", that.id + "_upload_iframe");
  1073. iframe.setAttribute("name", that.id + "_upload_iframe");
  1074. iframe.setAttribute("width", "0");
  1075. iframe.setAttribute("height", "0");
  1076. iframe.setAttribute("border", "0");
  1077. iframe.setAttribute("src", "javascript:false;");
  1078. iframe.style.display = "none";
  1079. document.body.appendChild(iframe);
  1080. } else {
  1081. iframe = that.iframeobj[0];
  1082. }
  1083. var myContent = '<!DOCTYPE html>'
  1084. + '<html><head><title>Uploading File</title></head>'
  1085. + '<body>'
  1086. + '<form '
  1087. + 'class="' + that.id + '_upload_iframe_form" '
  1088. + 'name="' + that.id + '_upload_iframe_form" '
  1089. + 'action="' + that.options.uploadUrl + '" method="post" '
  1090. + 'enctype="multipart/form-data" encoding="multipart/form-data" style="display:none;">'
  1091. + $("#" + that.id + '_imgUploadField')[0].outerHTML
  1092. + '</form></body></html>';
  1093. iframe.contentWindow.document.open('text/htmlreplace');
  1094. iframe.contentWindow.document.write(myContent);
  1095. iframe.contentWindow.document.close();
  1096. that.iframeobj = $("#" + that.id + "_upload_iframe");
  1097. that.iframeform = that.iframeobj.contents().find("html").find("." + that.id + "_upload_iframe_form");
  1098. that.iframeform.on("change", "input", function () {
  1099. that.SubmitFallbackIframe(that);
  1100. });
  1101. that.iframeform.find("input")[0].attachEvent("onchange", function () {
  1102. that.SubmitFallbackIframe(that);
  1103. });
  1104. var eventHandlermyFile = function () {
  1105. if (iframe.detachEvent)
  1106. iframe.detachEvent("onload", eventHandlermyFile);
  1107. else
  1108. iframe.removeEventListener("load", eventHandlermyFile, false);
  1109. var response = that.getIframeContentJSON(iframe);
  1110. if (jQuery.isEmptyObject(that.modal)) {
  1111. that.afterUpload(response);
  1112. }
  1113. }
  1114. if (iframe.addEventListener)
  1115. iframe.addEventListener("load", eventHandlermyFile, true);
  1116. if (iframe.attachEvent)
  1117. iframe.attachEvent("onload", eventHandlermyFile);
  1118. return "#" + that.id + '_imgUploadField';
  1119. } else {
  1120. return "";
  1121. }
  1122. },
  1123. SubmitFallbackIframe: function (that) {
  1124. that.showLoader();
  1125. if(that.options.processInline && !that.options.uploadUrl){
  1126. if (that.options.onError){
  1127. that.options.onError.call(that,"processInline is not supported by your browser ");
  1128. that.hideLoader();
  1129. }
  1130. }else{
  1131. if (that.options.onBeforeImgUpload) that.options.onBeforeImgUpload.call(that);
  1132. that.iframeform[0].submit();
  1133. }
  1134. },
  1135. getIframeContentJSON: function (iframe) {
  1136. try {
  1137. var doc = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document,
  1138. response;
  1139. var innerHTML = doc.body.innerHTML;
  1140. if (innerHTML.slice(0, 5).toLowerCase() == "<pre>" && innerHTML.slice(-6).toLowerCase() == "</pre>") {
  1141. innerHTML = doc.body.firstChild.firstChild.nodeValue;
  1142. }
  1143. response = jQuery.parseJSON(innerHTML);
  1144. } catch (err) {
  1145. response = { success: false };
  1146. }
  1147. return response;
  1148. },
  1149. hexadecimal:function(num) {
  1150. var r = parseInt(num).toString(16);
  1151. if (r.length == 1) {
  1152. return '0' + r;
  1153. }
  1154. return r.toUpperCase();
  1155. }
  1156. };
  1157. })(window, document);