7 * @class Roo.bootstrap.UploadCropbox
8 * @extends Roo.bootstrap.Component
9 * Bootstrap UploadCropbox class
10 * @cfg {String} emptyText show when image has been loaded
11 * @cfg {String} rotateNotify show when image too small to rotate
12 * @cfg {Number} errorTimeout default 3000
13 * @cfg {Number} minWidth default 300
14 * @cfg {Number} minHeight default 300
15 * @cfg {Array} buttons default ['rotateLeft', 'pictureBtn', 'rotateRight']
16 * @cfg {Boolean} isDocument (true|false) default false
17 * @cfg {String} url action url
18 * @cfg {String} paramName default 'imageUpload'
19 * @cfg {String} method default POST
22 * Create a new UploadCropbox
23 * @param {Object} config The config object
26 Roo.bootstrap.UploadCropbox = function(config){
27 Roo.bootstrap.UploadCropbox.superclass.constructor.call(this, config);
31 * @event beforeselectfile
32 * Fire before select file
33 * @param {Roo.bootstrap.UploadCropbox} this
35 "beforeselectfile" : true,
38 * Fire after initEvent
39 * @param {Roo.bootstrap.UploadCropbox} this
44 * Fire after initEvent
45 * @param {Roo.bootstrap.UploadCropbox} this
46 * @param {String} data
51 * Fire when preparing the file data
52 * @param {Roo.bootstrap.UploadCropbox} this
53 * @param {Object} file
58 * Fire when get exception
59 * @param {Roo.bootstrap.UploadCropbox} this
60 * @param {XMLHttpRequest} xhr
64 * @event beforeloadcanvas
65 * Fire before load the canvas
66 * @param {Roo.bootstrap.UploadCropbox} this
69 "beforeloadcanvas" : true,
72 * Fire when trash image
73 * @param {Roo.bootstrap.UploadCropbox} this
78 * Fire when download the image
79 * @param {Roo.bootstrap.UploadCropbox} this
83 * @event footerbuttonclick
84 * Fire when footerbuttonclick
85 * @param {Roo.bootstrap.UploadCropbox} this
86 * @param {String} type
88 "footerbuttonclick" : true,
92 * @param {Roo.bootstrap.UploadCropbox} this
97 * Fire when rotate the image
98 * @param {Roo.bootstrap.UploadCropbox} this
104 * Fire when inspect the file
105 * @param {Roo.bootstrap.UploadCropbox} this
106 * @param {Object} file
111 * Fire when xhr upload the file
112 * @param {Roo.bootstrap.UploadCropbox} this
113 * @param {Object} data
118 * Fire when arrange the file data
119 * @param {Roo.bootstrap.UploadCropbox} this
120 * @param {Object} formData
125 this.buttons = this.buttons || Roo.bootstrap.UploadCropbox.footer.STANDARD;
128 Roo.extend(Roo.bootstrap.UploadCropbox, Roo.bootstrap.Component, {
130 emptyText : 'Click to upload image',
131 rotateNotify : 'Image is too small to rotate',
146 cropType : 'image/jpeg',
148 canvasLoaded : false,
151 paramName : 'imageUpload',
153 getAutoCreate : function()
157 cls : 'roo-upload-cropbox',
161 cls : 'roo-upload-cropbox-body',
162 style : 'cursor:pointer',
166 cls : 'roo-upload-cropbox-selector',
171 cls : 'roo-upload-cropbox-preview'
175 cls : 'roo-upload-cropbox-thumb'
179 cls : 'roo-upload-cropbox-empty-notify',
180 html : this.emptyText
184 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
185 html : this.rotateNotify
191 cls : 'roo-upload-cropbox-footer',
194 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
204 onRender : function(ct, position)
206 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
208 if (this.buttons.length) {
210 Roo.each(this.buttons, function(bb) {
212 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
214 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
220 initEvents : function()
222 this.urlAPI = (window.createObjectURL && window) ||
223 (window.URL && URL.revokeObjectURL && URL) ||
224 (window.webkitURL && webkitURL);
226 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
227 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
229 this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
230 this.selectorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
231 this.selectorEl.hide();
233 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
234 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
236 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
237 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
240 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
241 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
243 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
244 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
247 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
248 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
249 this.footerEl.hide();
251 this.setThumbBoxSize();
257 this.fireEvent('initial', this);
264 window.addEventListener("resize", function() { _this.resize(); } );
266 this.bodyEl.on('click', this.beforeSelectFile, this);
269 this.bodyEl.on('touchstart', this.onTouchStart, this);
270 this.bodyEl.on('touchmove', this.onTouchMove, this);
271 this.bodyEl.on('touchend', this.onTouchEnd, this);
275 this.bodyEl.on('mousedown', this.onMouseDown, this);
276 this.bodyEl.on('mousemove', this.onMouseMove, this);
277 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
278 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
279 Roo.get(document).on('mouseup', this.onMouseUp, this);
282 this.selectorEl.on('change', this.onFileSelected, this);
291 this.dragable = false;
292 this.pinching = false;
295 this.cropData = false;
296 this.notifyEl.dom.innerHTML = this.emptyText;
298 this.selectorEl.dom.value = '';
304 if(this.fireEvent('resize', this) != false){
305 this.setThumbBoxPosition();
306 this.setCanvasPosition();
310 onFooterButtonClick : function(e, el, o, type)
314 this.onRotateLeft(e);
316 case 'rotate-right' :
317 this.onRotateRight(e);
320 this.beforeSelectFile(e);
335 this.fireEvent('footerbuttonclick', this, type);
338 beforeSelectFile : function(e)
342 if(this.fireEvent('beforeselectfile', this) != false){
343 this.selectorEl.dom.click();
347 onFileSelected : function(e)
351 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
355 var file = this.selectorEl.dom.files[0];
357 if(this.fireEvent('inspect', this, file) != false){
365 this.fireEvent('trash', this);
368 download : function(e)
370 this.fireEvent('download', this);
373 loadCanvas : function(src)
375 if(this.fireEvent('beforeloadcanvas', this, src) != false){
379 this.imageEl = document.createElement('img');
383 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
385 this.imageEl.src = src;
389 onLoadCanvas : function()
391 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
392 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
394 this.bodyEl.un('click', this.beforeSelectFile, this);
396 this.notifyEl.hide();
398 this.footerEl.show();
400 this.baseRotateLevel();
403 this.setThumbBoxSize();
406 this.setThumbBoxPosition();
408 this.baseScaleLevel();
414 this.canvasLoaded = true;
418 setCanvasPosition : function()
424 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
425 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
427 this.previewEl.setLeft(pw);
428 this.previewEl.setTop(ph);
432 onMouseDown : function(e)
436 this.dragable = true;
437 this.pinching = false;
439 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
440 this.dragable = false;
444 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
445 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
449 onMouseMove : function(e)
453 if(!this.canvasLoaded){
461 var minX = Math.ceil(this.thumbEl.getLeft(true));
462 var minY = Math.ceil(this.thumbEl.getTop(true));
464 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
465 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
467 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
468 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
473 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
474 var bgY = Math.ceil(y + this.previewEl.getTop(true));
476 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
477 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
479 this.previewEl.setLeft(bgX);
480 this.previewEl.setTop(bgY);
482 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
483 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
486 onMouseUp : function(e)
490 this.dragable = false;
493 onMouseWheel : function(e)
497 this.startScale = this.scale;
499 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
501 if(!this.zoomable()){
502 this.scale = this.startScale;
511 zoomable : function()
513 var minScale = this.thumbEl.getWidth() / this.minWidth;
515 if(this.minWidth < this.minHeight){
516 minScale = this.thumbEl.getHeight() / this.minHeight;
519 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
520 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
524 (this.rotate == 0 || this.rotate == 180) &&
526 width > this.imageEl.OriginWidth ||
527 height > this.imageEl.OriginHeight ||
528 (width < this.minWidth && height < this.minHeight)
536 (this.rotate == 90 || this.rotate == 270) &&
538 width > this.imageEl.OriginWidth ||
539 height > this.imageEl.OriginHeight ||
540 (width < this.minHeight && height < this.minWidth)
548 (this.rotate == 0 || this.rotate == 180) &&
550 width < this.minWidth ||
551 width > this.imageEl.OriginWidth ||
552 height < this.minHeight ||
553 height > this.imageEl.OriginHeight
561 (this.rotate == 90 || this.rotate == 270) &&
563 width < this.minHeight ||
564 width > this.imageEl.OriginWidth ||
565 height < this.minWidth ||
566 height > this.imageEl.OriginHeight
576 onRotateLeft : function(e)
578 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
580 var minScale = this.thumbEl.getWidth() / this.minWidth;
582 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
583 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
585 this.startScale = this.scale;
587 while (this.getScaleLevel() < minScale){
589 this.scale = this.scale + 1;
591 if(!this.zoomable()){
596 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
597 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
602 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
609 this.scale = this.startScale;
616 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
619 this.setThumbBoxSize();
620 this.setThumbBoxPosition();
621 this.setCanvasPosition();
626 this.fireEvent('rotate', this, 'left');
630 onRotateRight : function(e)
632 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
634 var minScale = this.thumbEl.getWidth() / this.minWidth;
636 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
637 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
639 this.startScale = this.scale;
641 while (this.getScaleLevel() < minScale){
643 this.scale = this.scale + 1;
645 if(!this.zoomable()){
650 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
651 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
656 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
663 this.scale = this.startScale;
670 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
673 this.setThumbBoxSize();
674 this.setThumbBoxPosition();
675 this.setCanvasPosition();
680 this.fireEvent('rotate', this, 'right');
683 onRotateFail : function()
685 this.errorEl.show(true);
689 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
694 this.previewEl.dom.innerHTML = '';
696 var canvasEl = document.createElement("canvas");
698 var contextEl = canvasEl.getContext("2d");
700 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
701 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
702 var center = this.imageEl.OriginWidth / 2;
704 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
705 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
706 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
707 center = this.imageEl.OriginHeight / 2;
710 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
712 contextEl.translate(center, center);
713 contextEl.rotate(this.rotate * Math.PI / 180);
715 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
717 this.canvasEl = document.createElement("canvas");
719 this.contextEl = this.canvasEl.getContext("2d");
721 switch (this.rotate) {
724 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
725 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
727 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
732 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
733 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
735 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
736 this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
740 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
745 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
746 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
748 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
749 this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
753 this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
758 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
759 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
761 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
762 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
766 this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
773 this.previewEl.appendChild(this.canvasEl);
775 this.setCanvasPosition();
780 if(!this.canvasLoaded){
784 var imageCanvas = document.createElement("canvas");
786 var imageContext = imageCanvas.getContext("2d");
788 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
789 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
791 var center = imageCanvas.width / 2;
793 imageContext.translate(center, center);
795 imageContext.rotate(this.rotate * Math.PI / 180);
797 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
799 var canvas = document.createElement("canvas");
801 var context = canvas.getContext("2d");
803 canvas.width = this.minWidth;
804 canvas.height = this.minHeight;
806 switch (this.rotate) {
809 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
810 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
812 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
813 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
815 var targetWidth = this.minWidth - 2 * x;
816 var targetHeight = this.minHeight - 2 * y;
820 if((x == 0 && y == 0) || (x == 0 && y > 0)){
821 scale = targetWidth / width;
825 scale = targetHeight / height;
829 scale = targetWidth / width;
832 scale = targetHeight / height;
836 context.scale(scale, scale);
838 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
839 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
841 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
842 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
844 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
849 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
850 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
852 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
853 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
855 var targetWidth = this.minWidth - 2 * x;
856 var targetHeight = this.minHeight - 2 * y;
860 if((x == 0 && y == 0) || (x == 0 && y > 0)){
861 scale = targetWidth / width;
865 scale = targetHeight / height;
869 scale = targetWidth / width;
872 scale = targetHeight / height;
876 context.scale(scale, scale);
878 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
879 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
881 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
882 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
884 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
886 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
891 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
892 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
894 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
895 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
897 var targetWidth = this.minWidth - 2 * x;
898 var targetHeight = this.minHeight - 2 * y;
902 if((x == 0 && y == 0) || (x == 0 && y > 0)){
903 scale = targetWidth / width;
907 scale = targetHeight / height;
911 scale = targetWidth / width;
914 scale = targetHeight / height;
918 context.scale(scale, scale);
920 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
921 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
923 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
924 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
926 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
927 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
929 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
934 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
935 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
937 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
938 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
940 var targetWidth = this.minWidth - 2 * x;
941 var targetHeight = this.minHeight - 2 * y;
945 if((x == 0 && y == 0) || (x == 0 && y > 0)){
946 scale = targetWidth / width;
950 scale = targetHeight / height;
954 scale = targetWidth / width;
957 scale = targetHeight / height;
961 context.scale(scale, scale);
963 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
964 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
966 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
967 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
969 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
971 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
978 this.cropData = canvas.toDataURL(this.cropType);
980 if(this.fireEvent('crop', this, this.cropData) !== false){
981 this.process(this.file, this.cropData);
988 setThumbBoxSize : function()
992 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
993 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
994 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
996 this.minWidth = width;
997 this.minHeight = height;
999 if(this.rotate == 90 || this.rotate == 270){
1000 this.minWidth = height;
1001 this.minHeight = width;
1006 width = Math.ceil(this.minWidth * height / this.minHeight);
1008 if(this.minWidth > this.minHeight){
1010 height = Math.ceil(this.minHeight * width / this.minWidth);
1013 this.thumbEl.setStyle({
1014 width : width + 'px',
1015 height : height + 'px'
1022 setThumbBoxPosition : function()
1024 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1025 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1027 this.thumbEl.setLeft(x);
1028 this.thumbEl.setTop(y);
1032 baseRotateLevel : function()
1034 this.baseRotate = 1;
1037 typeof(this.exif) != 'undefined' &&
1038 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1039 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1041 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1044 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1048 baseScaleLevel : function()
1052 if(this.isDocument){
1054 if(this.baseRotate == 6 || this.baseRotate == 8){
1056 height = this.thumbEl.getHeight();
1057 this.baseScale = height / this.imageEl.OriginWidth;
1059 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1060 width = this.thumbEl.getWidth();
1061 this.baseScale = width / this.imageEl.OriginHeight;
1067 height = this.thumbEl.getHeight();
1068 this.baseScale = height / this.imageEl.OriginHeight;
1070 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1071 width = this.thumbEl.getWidth();
1072 this.baseScale = width / this.imageEl.OriginWidth;
1078 if(this.baseRotate == 6 || this.baseRotate == 8){
1080 width = this.thumbEl.getHeight();
1081 this.baseScale = width / this.imageEl.OriginHeight;
1083 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1084 height = this.thumbEl.getWidth();
1085 this.baseScale = height / this.imageEl.OriginHeight;
1088 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1089 height = this.thumbEl.getWidth();
1090 this.baseScale = height / this.imageEl.OriginHeight;
1092 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1093 width = this.thumbEl.getHeight();
1094 this.baseScale = width / this.imageEl.OriginWidth;
1101 width = this.thumbEl.getWidth();
1102 this.baseScale = width / this.imageEl.OriginWidth;
1104 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1105 height = this.thumbEl.getHeight();
1106 this.baseScale = height / this.imageEl.OriginHeight;
1109 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1111 height = this.thumbEl.getHeight();
1112 this.baseScale = height / this.imageEl.OriginHeight;
1114 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1115 width = this.thumbEl.getWidth();
1116 this.baseScale = width / this.imageEl.OriginWidth;
1124 getScaleLevel : function()
1126 return this.baseScale * Math.pow(1.1, this.scale);
1129 onTouchStart : function(e)
1131 if(!this.canvasLoaded){
1132 this.beforeSelectFile(e);
1136 var touches = e.browserEvent.touches;
1142 if(touches.length == 1){
1143 this.onMouseDown(e);
1147 if(touches.length != 2){
1153 for(var i = 0, finger; finger = touches[i]; i++){
1154 coords.push(finger.pageX, finger.pageY);
1157 var x = Math.pow(coords[0] - coords[2], 2);
1158 var y = Math.pow(coords[1] - coords[3], 2);
1160 this.startDistance = Math.sqrt(x + y);
1162 this.startScale = this.scale;
1164 this.pinching = true;
1165 this.dragable = false;
1169 onTouchMove : function(e)
1171 if(!this.pinching && !this.dragable){
1175 var touches = e.browserEvent.touches;
1182 this.onMouseMove(e);
1188 for(var i = 0, finger; finger = touches[i]; i++){
1189 coords.push(finger.pageX, finger.pageY);
1192 var x = Math.pow(coords[0] - coords[2], 2);
1193 var y = Math.pow(coords[1] - coords[3], 2);
1195 this.endDistance = Math.sqrt(x + y);
1197 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1199 if(!this.zoomable()){
1200 this.scale = this.startScale;
1208 onTouchEnd : function(e)
1210 this.pinching = false;
1211 this.dragable = false;
1215 process : function(file, crop)
1217 this.xhr = new XMLHttpRequest();
1219 file.xhr = this.xhr;
1221 this.xhr.open(this.method, this.url, true);
1224 "Accept": "application/json",
1225 "Cache-Control": "no-cache",
1226 "X-Requested-With": "XMLHttpRequest"
1229 for (var headerName in headers) {
1230 var headerValue = headers[headerName];
1232 this.xhr.setRequestHeader(headerName, headerValue);
1238 this.xhr.onload = function()
1240 _this.xhrOnLoad(_this.xhr);
1243 this.xhr.onerror = function()
1245 _this.xhrOnError(_this.xhr);
1248 var formData = new FormData();
1250 formData.append('returnHTML', 'NO');
1253 formData.append('crop', crop);
1256 if(typeof(file) != 'undefined' && file.id * 1 < 1){
1257 formData.append(this.paramName, file, file.name);
1260 if(typeof(file.filename) != 'undefined'){
1261 formData.append('filename', file.filename);
1264 if(typeof(file.mimetype) != 'undefined'){
1265 formData.append('mimetype', file.mimetype);
1268 if(this.fireEvent('arrange', this, formData) != false){
1269 this.xhr.send(formData);
1273 xhrOnLoad : function(xhr)
1275 if (xhr.readyState !== 4) {
1276 this.fireEvent('exception', this, xhr);
1280 var response = Roo.decode(xhr.responseText);
1282 if(!response.success){
1283 this.fireEvent('exception', this, xhr);
1287 var response = Roo.decode(xhr.responseText);
1289 this.fireEvent('upload', this, response);
1293 xhrOnError : function()
1295 Roo.log('xhr on error');
1297 var response = Roo.decode(xhr.responseText);
1303 prepare : function(file)
1308 if(typeof(file) === 'string'){
1309 this.loadCanvas(file);
1313 if(!file || !this.urlAPI){
1318 this.cropType = file.type;
1322 if(this.fireEvent('prepare', this, this.file) != false){
1324 var reader = new FileReader();
1326 reader.onload = function (e) {
1327 if (e.target.error) {
1328 Roo.log(e.target.error);
1332 var buffer = e.target.result,
1333 dataView = new DataView(buffer),
1335 maxOffset = dataView.byteLength - 4,
1339 if (dataView.getUint16(0) === 0xffd8) {
1340 while (offset < maxOffset) {
1341 markerBytes = dataView.getUint16(offset);
1343 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1344 markerLength = dataView.getUint16(offset + 2) + 2;
1345 if (offset + markerLength > dataView.byteLength) {
1346 Roo.log('Invalid meta data: Invalid segment size.');
1350 if(markerBytes == 0xffe1){
1351 _this.parseExifData(
1358 offset += markerLength;
1368 var url = _this.urlAPI.createObjectURL(_this.file);
1370 _this.loadCanvas(url);
1375 reader.readAsArrayBuffer(this.file);
1381 parseExifData : function(dataView, offset, length)
1383 var tiffOffset = offset + 10,
1387 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1388 // No Exif data, might be XMP data instead
1392 // Check for the ASCII code for "Exif" (0x45786966):
1393 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1394 // No Exif data, might be XMP data instead
1397 if (tiffOffset + 8 > dataView.byteLength) {
1398 Roo.log('Invalid Exif data: Invalid segment size.');
1401 // Check for the two null bytes:
1402 if (dataView.getUint16(offset + 8) !== 0x0000) {
1403 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1406 // Check the byte alignment:
1407 switch (dataView.getUint16(tiffOffset)) {
1409 littleEndian = true;
1412 littleEndian = false;
1415 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1418 // Check for the TIFF tag marker (0x002A):
1419 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1420 Roo.log('Invalid Exif data: Missing TIFF marker.');
1423 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1424 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1429 tiffOffset + dirOffset,
1434 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1439 if (dirOffset + 6 > dataView.byteLength) {
1440 Roo.log('Invalid Exif data: Invalid directory offset.');
1443 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1444 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1445 if (dirEndOffset + 4 > dataView.byteLength) {
1446 Roo.log('Invalid Exif data: Invalid directory size.');
1449 for (i = 0; i < tagsNumber; i += 1) {
1453 dirOffset + 2 + 12 * i, // tag offset
1457 // Return the offset to the next directory:
1458 return dataView.getUint32(dirEndOffset, littleEndian);
1461 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1463 var tag = dataView.getUint16(offset, littleEndian);
1465 this.exif[tag] = this.getExifValue(
1469 dataView.getUint16(offset + 2, littleEndian), // tag type
1470 dataView.getUint32(offset + 4, littleEndian), // tag length
1475 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1477 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1486 Roo.log('Invalid Exif data: Invalid tag type.');
1490 tagSize = tagType.size * length;
1491 // Determine if the value is contained in the dataOffset bytes,
1492 // or if the value at the dataOffset is a pointer to the actual data:
1493 dataOffset = tagSize > 4 ?
1494 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1495 if (dataOffset + tagSize > dataView.byteLength) {
1496 Roo.log('Invalid Exif data: Invalid data offset.');
1500 return tagType.getValue(dataView, dataOffset, littleEndian);
1503 for (i = 0; i < length; i += 1) {
1504 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1507 if (tagType.ascii) {
1509 // Concatenate the chars:
1510 for (i = 0; i < values.length; i += 1) {
1512 // Ignore the terminating NULL byte(s):
1513 if (c === '\u0000') {
1525 Roo.apply(Roo.bootstrap.UploadCropbox, {
1527 'Orientation': 0x0112
1533 3: 180, //'bottom-right',
1534 // 4: 'bottom-left',
1536 6: 90, //'right-top',
1537 // 7: 'right-bottom',
1538 8: 270 //'left-bottom'
1542 // byte, 8-bit unsigned int:
1544 getValue: function (dataView, dataOffset) {
1545 return dataView.getUint8(dataOffset);
1549 // ascii, 8-bit byte:
1551 getValue: function (dataView, dataOffset) {
1552 return String.fromCharCode(dataView.getUint8(dataOffset));
1557 // short, 16 bit int:
1559 getValue: function (dataView, dataOffset, littleEndian) {
1560 return dataView.getUint16(dataOffset, littleEndian);
1564 // long, 32 bit int:
1566 getValue: function (dataView, dataOffset, littleEndian) {
1567 return dataView.getUint32(dataOffset, littleEndian);
1571 // rational = two long values, first is numerator, second is denominator:
1573 getValue: function (dataView, dataOffset, littleEndian) {
1574 return dataView.getUint32(dataOffset, littleEndian) /
1575 dataView.getUint32(dataOffset + 4, littleEndian);
1579 // slong, 32 bit signed int:
1581 getValue: function (dataView, dataOffset, littleEndian) {
1582 return dataView.getInt32(dataOffset, littleEndian);
1586 // srational, two slongs, first is numerator, second is denominator:
1588 getValue: function (dataView, dataOffset, littleEndian) {
1589 return dataView.getInt32(dataOffset, littleEndian) /
1590 dataView.getInt32(dataOffset + 4, littleEndian);
1600 cls : 'btn-group roo-upload-cropbox-rotate-left',
1601 action : 'rotate-left',
1605 cls : 'btn btn-default',
1606 html : '<i class="fa fa-undo"></i>'
1612 cls : 'btn-group roo-upload-cropbox-picture',
1617 cls : 'btn btn-default',
1618 html : '<i class="fa fa-picture-o"></i>'
1624 cls : 'btn-group roo-upload-cropbox-rotate-right',
1625 action : 'rotate-right',
1629 cls : 'btn btn-default',
1630 html : '<i class="fa fa-repeat"></i>'
1638 cls : 'btn-group roo-upload-cropbox-rotate-left',
1639 action : 'rotate-left',
1643 cls : 'btn btn-default',
1644 html : '<i class="fa fa-undo"></i>'
1650 cls : 'btn-group roo-upload-cropbox-download',
1651 action : 'download',
1655 cls : 'btn btn-default',
1656 html : '<i class="fa fa-download"></i>'
1662 cls : 'btn-group roo-upload-cropbox-crop',
1667 cls : 'btn btn-default',
1668 html : '<i class="fa fa-crop"></i>'
1674 cls : 'btn-group roo-upload-cropbox-trash',
1679 cls : 'btn btn-default',
1680 html : '<i class="fa fa-trash"></i>'
1686 cls : 'btn-group roo-upload-cropbox-rotate-right',
1687 action : 'rotate-right',
1691 cls : 'btn btn-default',
1692 html : '<i class="fa fa-repeat"></i>'
1700 cls : 'btn-group roo-upload-cropbox-rotate-left',
1701 action : 'rotate-left',
1705 cls : 'btn btn-default',
1706 html : '<i class="fa fa-undo"></i>'
1712 cls : 'btn-group roo-upload-cropbox-rotate-right',
1713 action : 'rotate-right',
1717 cls : 'btn btn-default',
1718 html : '<i class="fa fa-repeat"></i>'