7 * @class Roo.dialog.UploadCropbox
8 * @extends Roo.BoxComponent
9 * Dialog 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 {Number} outputMaxWidth default 1200
16 * @cfg {Number} windowSize default 300
17 * @cfg {Array} buttons default ['rotateLeft', 'pictureBtn', 'rotateRight']
18 * @cfg {Boolean} isDocument (true|false) default false
19 * @cfg {String} url action url
20 * @cfg {String} paramName default 'imageUpload'
21 * @cfg {String} method default POST
22 * @cfg {Boolean} loadMask (true|false) default true
23 * @cfg {Boolean} loadingText default 'Loading...'
26 * Create a new UploadCropbox
27 * @param {Object} config The config object
30 Roo.dialog.UploadCropbox = function(config){
31 Roo.dialog.UploadCropbox.superclass.constructor.call(this, config);
35 * @event beforeselectfile
36 * Fire before select file
37 * @param {Roo.dialog.UploadCropbox} this
39 "beforeselectfile" : true,
42 * Fire after initEvent
43 * @param {Roo.dialog.UploadCropbox} this
48 * Fire after initEvent
49 * @param {Roo.dialog.UploadCropbox} this
50 * @param {String} data
55 * Fire when preparing the file data
56 * @param {Roo.dialog.UploadCropbox} this
57 * @param {Object} file
62 * Fire when get exception
63 * @param {Roo.dialog.UploadCropbox} this
64 * @param {XMLHttpRequest} xhr
68 * @event beforeloadcanvas
69 * Fire before load the canvas
70 * @param {Roo.dialog.UploadCropbox} this
73 "beforeloadcanvas" : true,
76 * Fire when trash image
77 * @param {Roo.dialog.UploadCropbox} this
82 * Fire when download the image
83 * @param {Roo.dialog.UploadCropbox} this
87 * @event footerbuttonclick
88 * Fire when footerbuttonclick
89 * @param {Roo.dialog.UploadCropbox} this
90 * @param {String} type
92 "footerbuttonclick" : true,
96 * @param {Roo.dialog.UploadCropbox} this
101 * Fire when rotate the image
102 * @param {Roo.dialog.UploadCropbox} this
103 * @param {String} pos
108 * Fire when inspect the file
109 * @param {Roo.dialog.UploadCropbox} this
110 * @param {Object} file
115 * Fire when xhr upload the file
116 * @param {Roo.dialog.UploadCropbox} this
117 * @param {Object} data
122 * Fire when arrange the file data
123 * @param {Roo.dialog.UploadCropbox} this
124 * @param {Object} formData
129 * Fire after load the canvas
130 * @param {Roo.dialog.UploadCropbox}
131 * @param {Object} imgEl
136 this.buttons = this.buttons || Roo.dialog.UploadCropbox.footer.STANDARD;
139 Roo.extend(Roo.dialog.UploadCropbox, Roo.Component, {
141 emptyText : 'Click to upload image',
142 rotateNotify : 'Image is too small to rotate',
154 outputMaxWidth : 1200,
159 cropType : 'image/jpeg',
161 canvasLoaded : false,
164 paramName : 'imageUpload',
166 loadingText : 'Loading...',
169 getAutoCreate : function()
173 cls : 'roo-upload-cropbox',
177 cls : 'roo-upload-cropbox-selector',
182 cls : 'roo-upload-cropbox-body',
183 style : 'cursor:pointer',
187 cls : 'roo-upload-cropbox-preview'
191 cls : 'roo-upload-cropbox-thumb'
195 cls : 'roo-upload-cropbox-empty-notify',
196 html : this.emptyText
200 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
201 html : this.rotateNotify
207 cls : 'roo-upload-cropbox-footer',
210 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
220 onRender : function(ct, position)
222 Roo.dialog.UploadCropbox.superclass.onRender.call(this, ct, position);
225 if (this.el.attr('xtype')) {
226 this.el.attr('xtypex', this.el.attr('xtype'));
227 this.el.dom.removeAttribute('xtype');
233 var cfg = Roo.apply({}, this.getAutoCreate());
235 cfg.id = this.id || Roo.id();
238 cfg.cls = (typeof(cfg.cls) == 'undefined' ? this.cls : cfg.cls) + ' ' + this.cls;
241 if (this.style) { // fixme needs to support more complex style data.
242 cfg.style = (typeof(cfg.style) == 'undefined' ? this.style : cfg.style) + '; ' + this.style;
245 this.el = ct.createChild(cfg, position);
250 if (this.buttons.length) {
252 Roo.each(this.buttons, function(bb) {
254 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
256 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
262 this.maskEl = this.el;
266 initEvents : function()
268 this.urlAPI = (window.createObjectURL && window) ||
269 (window.URL && URL.revokeObjectURL && URL) ||
270 (window.webkitURL && webkitURL);
272 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
273 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
275 this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
276 this.selectorEl.hide();
278 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
279 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
281 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
282 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
285 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
286 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
288 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
289 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
292 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
293 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
294 this.footerEl.hide();
296 this.setThumbBoxSize();
302 this.fireEvent('initial', this);
309 window.addEventListener("resize", function() { _this.resize(); } );
311 this.bodyEl.on('click', this.beforeSelectFile, this);
314 this.bodyEl.on('touchstart', this.onTouchStart, this);
315 this.bodyEl.on('touchmove', this.onTouchMove, this);
316 this.bodyEl.on('touchend', this.onTouchEnd, this);
320 this.bodyEl.on('mousedown', this.onMouseDown, this);
321 this.bodyEl.on('mousemove', this.onMouseMove, this);
322 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
323 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
324 Roo.get(document).on('mouseup', this.onMouseUp, this);
327 this.selectorEl.on('change', this.onFileSelected, this);
336 this.dragable = false;
337 this.pinching = false;
340 this.cropData = false;
341 this.notifyEl.dom.innerHTML = this.emptyText;
343 // this.selectorEl.dom.value = '';
349 if(this.fireEvent('resize', this) != false){
350 this.setThumbBoxPosition();
351 this.setCanvasPosition();
355 onFooterButtonClick : function(e, el, o, type)
359 this.onRotateLeft(e);
361 case 'rotate-right' :
362 this.onRotateRight(e);
365 this.beforeSelectFile(e);
383 this.fireEvent('footerbuttonclick', this, type);
386 beforeSelectFile : function(e)
390 if(this.fireEvent('beforeselectfile', this) != false){
391 this.selectorEl.dom.click();
395 onFileSelected : function(e)
399 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
403 var file = this.selectorEl.dom.files[0];
405 if(this.fireEvent('inspect', this, file) != false){
413 this.fireEvent('trash', this);
416 download : function(e)
418 this.fireEvent('download', this);
423 this.setCanvasPosition();
426 loadCanvas : function(src)
428 if(this.fireEvent('beforeloadcanvas', this, src) != false){
432 this.imageEl = document.createElement('img');
436 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
438 this.imageEl.src = src;
442 onLoadCanvas : function()
444 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
445 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
447 if(this.fireEvent('loadcanvas', this, this.imageEl) != false){
449 this.bodyEl.un('click', this.beforeSelectFile, this);
451 this.notifyEl.hide();
453 this.footerEl.show();
455 this.baseRotateLevel();
458 this.setThumbBoxSize();
461 this.setThumbBoxPosition();
463 this.baseScaleLevel();
469 this.canvasLoaded = true;
474 this.maskEl.unmask();
479 setCanvasPosition : function(center = true)
485 var newCenterLeft = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
486 var newCenterTop = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
489 this.previewEl.setLeft(newCenterLeft);
490 this.previewEl.setTop(newCenterTop);
495 var oldScaleLevel = this.baseScale * Math.pow(1.02, this.startScale);
496 var oldCanvasWidth = Math.floor(this.imageEl.OriginWidth * oldScaleLevel);
497 var oldCanvasHeight = Math.floor(this.imageEl.OriginHeight * oldScaleLevel);
499 var oldCenterLeft = Math.ceil((this.bodyEl.getWidth() - oldCanvasWidth) / 2);
500 var oldCenterTop = Math.ceil((this.bodyEl.getHeight() - oldCanvasHeight) / 2);
502 var leftDiff = newCenterLeft - oldCenterLeft;
503 var topDiff = newCenterTop - oldCenterTop;
505 var newPreviewLeft = this.previewEl.getLeft(true) + leftDiff;
506 var newPreviewTop = this.previewEl.getTop(true) + topDiff;
508 console.log("-------------");
510 // console.log("old canvas width: " + oldCanvasWidth);
511 // console.log("old canvas height: " + oldCanvasHeight);
513 // console.log("new canvas width: " + this.canvasEl.width);
514 // console.log("new canvas height: " + this.canvasEl.height);
516 // console.log("old preview left: " + this.previewEl.getLeft(true));
517 // console.log("old preview top: " + this.previewEl.getTop(true));
519 // console.log("new preview left: " + newPreviewLeft);
520 // console.log("new preview top: " + newPreviewTop);
522 this.previewEl.setLeft(newPreviewLeft);
523 this.previewEl.setTop(newPreviewTop);
527 onMouseDown : function(e)
531 this.dragable = true;
532 this.pinching = false;
534 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
535 this.dragable = false;
539 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
540 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
544 onMouseMove : function(e)
548 if(!this.canvasLoaded){
556 var minX = Math.ceil(this.thumbEl.getLeft(true));
557 var minY = Math.ceil(this.thumbEl.getTop(true));
559 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
560 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
574 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
575 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
580 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
581 var bgY = Math.ceil(y + this.previewEl.getTop(true));
583 bgX = (bgX < minX) ? minX : ((bgX > maxX) ? maxX : bgX);
584 bgY = (bgY < minY) ? minY : ((bgY > maxY) ? maxY : bgY);
586 this.previewEl.setLeft(bgX);
587 this.previewEl.setTop(bgY);
589 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
590 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
593 onMouseUp : function(e)
597 this.dragable = false;
600 onMouseWheel : function(e)
604 this.startScale = this.scale;
605 this.scale = (e.getWheelDelta() > 0) ? (this.scale + 1) : (this.scale - 1);
607 if(!this.zoomable()){
608 this.scale = this.startScale;
618 zoomable : function()
620 var minScale = this.thumbEl.getWidth() / this.minWidth;
622 if(this.minWidth < this.minHeight){
623 minScale = this.thumbEl.getHeight() / this.minHeight;
626 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
627 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
629 var maxWidth = this.imageEl.OriginWidth;
630 var maxHeight = this.imageEl.OriginHeight;
633 var newCanvasWidth = Math.floor(this.imageEl.OriginWidth * this.getScaleLevel());
634 var newCanvasHeight = Math.floor(this.imageEl.OriginHeight * this.getScaleLevel());
636 var oldCenterLeft = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
637 var oldCenterTop = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
639 var newCenterLeft = Math.ceil((this.bodyEl.getWidth() - newCanvasWidth) / 2);
640 var newCenterTop = Math.ceil((this.bodyEl.getHeight() - newCanvasHeight) / 2);
642 var leftDiff = newCenterLeft - oldCenterLeft;
643 var topDiff = newCenterTop - oldCenterTop;
645 var newPreviewLeft = this.previewEl.getLeft(true) + leftDiff;
646 var newPreviewTop = this.previewEl.getTop(true) + topDiff;
648 var paddingLeft = newPreviewLeft - this.thumbEl.getLeft(true);
649 var paddingTop = newPreviewTop - this.thumbEl.getTop(true);
651 var paddingRight = this.thumbEl.getLeft(true) + this.thumbEl.getWidth() - newCanvasWidth - newPreviewLeft;
652 var paddingBottom = this.thumbEl.getTop(true) + this.thumbEl.getHeight() - newCanvasHeight - newPreviewTop;
654 var maxPaddingLeft = newCanvasWidth * 0.05;
655 var maxPaddingTop = newCanvasHeight * 0.05;
657 console.log("=================================");
658 console.log("old canvas width: " + this.canvasEl.width);
659 console.log("old canvas height: " + this.canvasEl.height);
661 console.log("new canvas width: " + newCanvasWidth);
662 console.log("new canvas height: " + newCanvasHeight);
664 console.log("old preview left: " + this.previewEl.getLeft(true));
665 console.log("old preview top: " + this.previewEl.getTop(true));
667 console.log("new preview left: " + newPreviewLeft);
668 console.log("new preview top: " + newPreviewTop);
670 console.log("padding left: " + paddingLeft);
671 console.log("padding top: " + paddingTop);
673 console.log("padding right: " + paddingRight);
674 console.log("padding bottom: " + paddingBottom);
676 console.log("max padding left: " + maxPaddingLeft);
677 console.log("max padding top: " + maxPaddingTop);
681 (this.rotate == 0 || this.rotate == 180) &&
683 width > this.imageEl.OriginWidth ||
684 height > this.imageEl.OriginHeight ||
685 (width < this.minWidth && height < this.minHeight)
693 (this.rotate == 90 || this.rotate == 270) &&
695 width > this.imageEl.OriginWidth ||
696 height > this.imageEl.OriginHeight ||
697 (width < this.minHeight && height < this.minWidth)
705 (this.rotate == 0 || this.rotate == 180) &&
707 (this.imageEl.OriginWidth / this.imageEl.OriginHeight >= this.minWidth / this.minHeight) && paddingLeft > maxPaddingLeft ||
708 (this.imageEl.OriginWidth / this.imageEl.OriginHeight >= this.minWidth / this.minHeight) && paddingRight > maxPaddingLeft ||
709 (this.imageEl.OriginWidth / this.imageEl.OriginHeight <= this.minWidth / this.minHeight) && paddingTop > maxPaddingTop ||
710 (this.imageEl.OriginWidth / this.imageEl.OriginHeight <= this.minWidth / this.minHeight) && paddingBottom > maxPaddingTop ||
720 (this.rotate == 90 || this.rotate == 270) &&
722 width < this.minHeight ||
723 width > this.imageEl.OriginWidth ||
724 height < this.minWidth ||
725 height > this.imageEl.OriginHeight
735 onRotateLeft : function(e)
737 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
739 var minScale = this.thumbEl.getWidth() / this.minWidth;
741 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
742 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
744 this.startScale = this.scale;
746 while (this.getScaleLevel() < minScale){
748 this.scale = this.scale + 1;
750 if(!this.zoomable()){
755 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
756 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
761 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
768 this.scale = this.startScale;
775 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
778 this.setThumbBoxSize();
779 this.setThumbBoxPosition();
780 this.setCanvasPosition();
785 this.fireEvent('rotate', this, 'left');
789 onRotateRight : function(e)
791 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
793 var minScale = this.thumbEl.getWidth() / this.minWidth;
795 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
796 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
798 this.startScale = this.scale;
800 while (this.getScaleLevel() < minScale){
802 this.scale = this.scale + 1;
804 if(!this.zoomable()){
809 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
810 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
815 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
822 this.scale = this.startScale;
829 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
832 this.setThumbBoxSize();
833 this.setThumbBoxPosition();
834 this.setCanvasPosition();
839 this.fireEvent('rotate', this, 'right');
842 onRotateFail : function()
844 this.errorEl.show(true);
848 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
853 this.previewEl.dom.innerHTML = '';
855 var canvasEl = document.createElement("canvas");
857 var contextEl = canvasEl.getContext("2d");
859 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
860 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
861 var center = this.imageEl.OriginWidth / 2;
863 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
864 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
865 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
866 center = this.imageEl.OriginHeight / 2;
869 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
871 contextEl.translate(center, center);
872 contextEl.rotate(this.rotate * Math.PI / 180);
874 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
876 this.canvasEl = document.createElement("canvas");
878 this.contextEl = this.canvasEl.getContext("2d");
880 switch (this.rotate) {
883 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
884 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
886 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
891 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
892 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
894 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
895 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);
899 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
904 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
905 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
907 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
908 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);
912 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);
917 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
918 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
920 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
921 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
925 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);
932 this.previewEl.appendChild(this.canvasEl);
934 this.setCanvasPosition(false);
939 if(!this.canvasLoaded){
943 var imageCanvas = document.createElement("canvas");
945 var imageContext = imageCanvas.getContext("2d");
947 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
948 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
950 var center = imageCanvas.width / 2;
952 imageContext.translate(center, center);
954 imageContext.rotate(this.rotate * Math.PI / 180);
956 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
958 var canvas = document.createElement("canvas");
960 var context = canvas.getContext("2d");
962 canvas.width = this.thumbEl.getWidth() / this.getScaleLevel();
964 canvas.height = this.thumbEl.getHeight() / this.getScaleLevel();
966 switch (this.rotate) {
969 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
970 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
972 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
973 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
975 var sx = this.thumbEl.getLeft(true) - this.previewEl.getLeft(true);
976 var sy = this.thumbEl.getTop(true) - this.previewEl.getTop(true);
978 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
979 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
981 if(canvas.width > this.outputMaxWidth) {
982 var scale = this.outputMaxWidth / canvas.width;
983 canvas.width = canvas.width * scale;
984 canvas.height = canvas.height * scale;
985 context.scale(scale, scale);
988 context.fillStyle = 'white';
989 context.fillRect(0, 0, this.thumbEl.getWidth() / this.getScaleLevel(), this.thumbEl.getHeight() / this.getScaleLevel());
992 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
997 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
998 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
1000 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1001 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1003 var targetWidth = this.minWidth - 2 * x;
1004 var targetHeight = this.minHeight - 2 * y;
1008 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1009 scale = targetWidth / width;
1012 if(x > 0 && y == 0){
1013 scale = targetHeight / height;
1017 scale = targetWidth / width;
1020 scale = targetHeight / height;
1024 context.scale(scale, scale);
1026 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1027 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1029 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1030 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1032 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
1034 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1039 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
1040 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
1042 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1043 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1045 var targetWidth = this.minWidth - 2 * x;
1046 var targetHeight = this.minHeight - 2 * y;
1050 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1051 scale = targetWidth / width;
1054 if(x > 0 && y == 0){
1055 scale = targetHeight / height;
1059 scale = targetWidth / width;
1062 scale = targetHeight / height;
1066 context.scale(scale, scale);
1068 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1069 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1071 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1072 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1074 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1075 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
1077 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1082 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
1083 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
1085 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1086 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1088 var targetWidth = this.minWidth - 2 * x;
1089 var targetHeight = this.minHeight - 2 * y;
1093 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1094 scale = targetWidth / width;
1097 if(x > 0 && y == 0){
1098 scale = targetHeight / height;
1102 scale = targetWidth / width;
1105 scale = targetHeight / height;
1109 context.scale(scale, scale);
1110 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1111 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1113 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1114 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1116 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1118 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1125 this.cropData = canvas.toDataURL(this.cropType);
1127 if(this.fireEvent('crop', this, this.cropData) !== false){
1128 this.process(this.file, this.cropData);
1135 setThumbBoxSize : function()
1139 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1140 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1141 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1143 this.minWidth = width;
1144 this.minHeight = height;
1146 if(this.rotate == 90 || this.rotate == 270){
1147 this.minWidth = height;
1148 this.minHeight = width;
1152 height = this.windowSize;
1153 width = Math.ceil(this.minWidth * height / this.minHeight);
1155 if(this.minWidth > this.minHeight){
1156 width = this.windowSize;
1157 height = Math.ceil(this.minHeight * width / this.minWidth);
1160 this.thumbEl.setStyle({
1161 width : width + 'px',
1162 height : height + 'px'
1169 setThumbBoxPosition : function()
1171 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1172 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1174 this.thumbEl.setLeft(x);
1175 this.thumbEl.setTop(y);
1179 baseRotateLevel : function()
1181 this.baseRotate = 1;
1184 typeof(this.exif) != 'undefined' &&
1185 typeof(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1186 [1, 3, 6, 8].indexOf(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != -1
1188 this.baseRotate = this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']];
1191 this.rotate = Roo.dialog.UploadCropbox['Orientation'][this.baseRotate];
1195 baseScaleLevel : function()
1199 if(this.isDocument){
1201 if(this.baseRotate == 6 || this.baseRotate == 8){
1203 height = this.thumbEl.getHeight();
1204 this.baseScale = height / this.imageEl.OriginWidth;
1206 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1207 width = this.thumbEl.getWidth();
1208 this.baseScale = width / this.imageEl.OriginHeight;
1214 height = this.thumbEl.getHeight();
1215 this.baseScale = height / this.imageEl.OriginHeight;
1217 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1218 width = this.thumbEl.getWidth();
1219 this.baseScale = width / this.imageEl.OriginWidth;
1225 if(this.baseRotate == 6 || this.baseRotate == 8){
1227 width = this.thumbEl.getHeight();
1228 this.baseScale = width / this.imageEl.OriginHeight;
1230 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1231 height = this.thumbEl.getWidth();
1232 this.baseScale = height / this.imageEl.OriginHeight;
1235 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1236 height = this.thumbEl.getWidth();
1237 this.baseScale = height / this.imageEl.OriginHeight;
1239 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1240 width = this.thumbEl.getHeight();
1241 this.baseScale = width / this.imageEl.OriginWidth;
1248 width = this.thumbEl.getWidth();
1249 this.baseScale = width / this.imageEl.OriginWidth;
1251 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1252 height = this.thumbEl.getHeight();
1253 this.baseScale = height / this.imageEl.OriginHeight;
1256 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1258 height = this.thumbEl.getHeight();
1259 this.baseScale = height / this.imageEl.OriginHeight;
1261 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1262 width = this.thumbEl.getWidth();
1263 this.baseScale = width / this.imageEl.OriginWidth;
1268 if(this.imageEl.OriginWidth < this.minWidth || this.imageEl.OriginHeight < this.minHeight) {
1269 this.baseScale = width / this.minWidth;
1275 getScaleLevel : function()
1277 return this.baseScale * Math.pow(1.02, this.scale);
1280 onTouchStart : function(e)
1282 if(!this.canvasLoaded){
1283 this.beforeSelectFile(e);
1287 var touches = e.browserEvent.touches;
1293 if(touches.length == 1){
1294 this.onMouseDown(e);
1298 if(touches.length != 2){
1304 for(var i = 0, finger; finger = touches[i]; i++){
1305 coords.push(finger.pageX, finger.pageY);
1308 var x = Math.pow(coords[0] - coords[2], 2);
1309 var y = Math.pow(coords[1] - coords[3], 2);
1311 this.startDistance = Math.sqrt(x + y);
1313 this.startScale = this.scale;
1315 this.pinching = true;
1316 this.dragable = false;
1320 onTouchMove : function(e)
1322 if(!this.pinching && !this.dragable){
1326 var touches = e.browserEvent.touches;
1333 this.onMouseMove(e);
1339 for(var i = 0, finger; finger = touches[i]; i++){
1340 coords.push(finger.pageX, finger.pageY);
1343 var x = Math.pow(coords[0] - coords[2], 2);
1344 var y = Math.pow(coords[1] - coords[3], 2);
1346 this.endDistance = Math.sqrt(x + y);
1348 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1350 if(!this.zoomable()){
1351 this.scale = this.startScale;
1359 onTouchEnd : function(e)
1361 this.pinching = false;
1362 this.dragable = false;
1366 process : function(file, crop)
1369 this.maskEl.mask(this.loadingText);
1372 this.xhr = new XMLHttpRequest();
1374 file.xhr = this.xhr;
1376 this.xhr.open(this.method, this.url, true);
1379 "Accept": "application/json",
1380 "Cache-Control": "no-cache",
1381 "X-Requested-With": "XMLHttpRequest"
1384 for (var headerName in headers) {
1385 var headerValue = headers[headerName];
1387 this.xhr.setRequestHeader(headerName, headerValue);
1393 this.xhr.onload = function()
1395 _this.xhrOnLoad(_this.xhr);
1398 this.xhr.onerror = function()
1400 _this.xhrOnError(_this.xhr);
1403 var formData = new FormData();
1405 formData.append('returnHTML', 'NO');
1408 formData.append('crop', crop);
1409 var blobBin = atob(crop.split(',')[1]);
1411 for(var i = 0; i < blobBin.length; i++) {
1412 array.push(blobBin.charCodeAt(i));
1414 var croppedFile =new Blob([new Uint8Array(array)], {type: this.cropType});
1415 formData.append(this.paramName, croppedFile, file.name);
1418 if(typeof(file.filename) != 'undefined'){
1419 formData.append('filename', file.filename);
1422 if(typeof(file.mimetype) != 'undefined'){
1423 formData.append('mimetype', file.mimetype);
1426 if(this.fireEvent('arrange', this, formData) != false){
1427 this.xhr.send(formData);
1431 xhrOnLoad : function(xhr)
1434 this.maskEl.unmask();
1437 if (xhr.readyState !== 4) {
1438 this.fireEvent('exception', this, xhr);
1442 var response = Roo.decode(xhr.responseText);
1444 if(!response.success){
1445 this.fireEvent('exception', this, xhr);
1449 var response = Roo.decode(xhr.responseText);
1451 this.fireEvent('upload', this, response);
1455 xhrOnError : function()
1458 this.maskEl.unmask();
1461 Roo.log('xhr on error');
1463 var response = Roo.decode(xhr.responseText);
1469 prepare : function(file)
1472 this.maskEl.mask(this.loadingText);
1478 if(typeof(file) === 'string'){
1479 this.loadCanvas(file);
1483 if(!file || !this.urlAPI){
1488 if(typeof(file.type) != 'undefined' && file.type.length != 0) {
1489 this.cropType = file.type;
1494 if(this.fireEvent('prepare', this, this.file) != false){
1496 var reader = new FileReader();
1498 reader.onload = function (e) {
1499 if (e.target.error) {
1500 Roo.log(e.target.error);
1504 var buffer = e.target.result,
1505 dataView = new DataView(buffer),
1507 maxOffset = dataView.byteLength - 4,
1511 if (dataView.getUint16(0) === 0xffd8) {
1512 while (offset < maxOffset) {
1513 markerBytes = dataView.getUint16(offset);
1515 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1516 markerLength = dataView.getUint16(offset + 2) + 2;
1517 if (offset + markerLength > dataView.byteLength) {
1518 Roo.log('Invalid meta data: Invalid segment size.');
1522 if(markerBytes == 0xffe1){
1523 _this.parseExifData(
1530 offset += markerLength;
1540 var url = _this.urlAPI.createObjectURL(_this.file);
1542 _this.loadCanvas(url);
1547 reader.readAsArrayBuffer(this.file);
1553 parseExifData : function(dataView, offset, length)
1555 var tiffOffset = offset + 10,
1559 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1560 // No Exif data, might be XMP data instead
1564 // Check for the ASCII code for "Exif" (0x45786966):
1565 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1566 // No Exif data, might be XMP data instead
1569 if (tiffOffset + 8 > dataView.byteLength) {
1570 Roo.log('Invalid Exif data: Invalid segment size.');
1573 // Check for the two null bytes:
1574 if (dataView.getUint16(offset + 8) !== 0x0000) {
1575 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1578 // Check the byte alignment:
1579 switch (dataView.getUint16(tiffOffset)) {
1581 littleEndian = true;
1584 littleEndian = false;
1587 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1590 // Check for the TIFF tag marker (0x002A):
1591 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1592 Roo.log('Invalid Exif data: Missing TIFF marker.');
1595 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1596 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1601 tiffOffset + dirOffset,
1606 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1611 if (dirOffset + 6 > dataView.byteLength) {
1612 Roo.log('Invalid Exif data: Invalid directory offset.');
1615 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1616 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1617 if (dirEndOffset + 4 > dataView.byteLength) {
1618 Roo.log('Invalid Exif data: Invalid directory size.');
1621 for (i = 0; i < tagsNumber; i += 1) {
1625 dirOffset + 2 + 12 * i, // tag offset
1629 // Return the offset to the next directory:
1630 return dataView.getUint32(dirEndOffset, littleEndian);
1633 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1635 var tag = dataView.getUint16(offset, littleEndian);
1637 this.exif[tag] = this.getExifValue(
1641 dataView.getUint16(offset + 2, littleEndian), // tag type
1642 dataView.getUint32(offset + 4, littleEndian), // tag length
1647 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1649 var tagType = Roo.dialog.UploadCropbox.exifTagTypes[type],
1658 Roo.log('Invalid Exif data: Invalid tag type.');
1662 tagSize = tagType.size * length;
1663 // Determine if the value is contained in the dataOffset bytes,
1664 // or if the value at the dataOffset is a pointer to the actual data:
1665 dataOffset = tagSize > 4 ?
1666 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1667 if (dataOffset + tagSize > dataView.byteLength) {
1668 Roo.log('Invalid Exif data: Invalid data offset.');
1672 return tagType.getValue(dataView, dataOffset, littleEndian);
1675 for (i = 0; i < length; i += 1) {
1676 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1679 if (tagType.ascii) {
1681 // Concatenate the chars:
1682 for (i = 0; i < values.length; i += 1) {
1684 // Ignore the terminating NULL byte(s):
1685 if (c === '\u0000') {
1697 Roo.apply(Roo.dialog.UploadCropbox, {
1699 'Orientation': 0x0112
1705 3: 180, //'bottom-right',
1706 // 4: 'bottom-left',
1708 6: 90, //'right-top',
1709 // 7: 'right-bottom',
1710 8: 270 //'left-bottom'
1714 // byte, 8-bit unsigned int:
1716 getValue: function (dataView, dataOffset) {
1717 return dataView.getUint8(dataOffset);
1721 // ascii, 8-bit byte:
1723 getValue: function (dataView, dataOffset) {
1724 return String.fromCharCode(dataView.getUint8(dataOffset));
1729 // short, 16 bit int:
1731 getValue: function (dataView, dataOffset, littleEndian) {
1732 return dataView.getUint16(dataOffset, littleEndian);
1736 // long, 32 bit int:
1738 getValue: function (dataView, dataOffset, littleEndian) {
1739 return dataView.getUint32(dataOffset, littleEndian);
1743 // rational = two long values, first is numerator, second is denominator:
1745 getValue: function (dataView, dataOffset, littleEndian) {
1746 return dataView.getUint32(dataOffset, littleEndian) /
1747 dataView.getUint32(dataOffset + 4, littleEndian);
1751 // slong, 32 bit signed int:
1753 getValue: function (dataView, dataOffset, littleEndian) {
1754 return dataView.getInt32(dataOffset, littleEndian);
1758 // srational, two slongs, first is numerator, second is denominator:
1760 getValue: function (dataView, dataOffset, littleEndian) {
1761 return dataView.getInt32(dataOffset, littleEndian) /
1762 dataView.getInt32(dataOffset + 4, littleEndian);
1772 cls : 'btn-group roo-upload-cropbox-rotate-left',
1773 action : 'rotate-left',
1777 cls : 'btn btn-default',
1778 html : '<i class="fa fa-undo"></i>'
1784 cls : 'btn-group roo-upload-cropbox-picture',
1789 cls : 'btn btn-default',
1790 html : '<i class="fa fa-picture-o"></i>'
1796 cls : 'btn-group roo-upload-cropbox-rotate-right',
1797 action : 'rotate-right',
1801 cls : 'btn btn-default',
1802 html : '<i class="fa fa-repeat"></i>'
1810 cls : 'btn-group roo-upload-cropbox-rotate-left',
1811 action : 'rotate-left',
1815 cls : 'btn btn-default',
1816 html : '<i class="fa fa-undo"></i>'
1822 cls : 'btn-group roo-upload-cropbox-download',
1823 action : 'download',
1827 cls : 'btn btn-default',
1828 html : '<i class="fa fa-download"></i>'
1834 cls : 'btn-group roo-upload-cropbox-crop',
1839 cls : 'btn btn-default',
1840 html : '<i class="fa fa-crop"></i>'
1846 cls : 'btn-group roo-upload-cropbox-trash',
1851 cls : 'btn btn-default',
1852 html : '<i class="fa fa-trash"></i>'
1858 cls : 'btn-group roo-upload-cropbox-rotate-right',
1859 action : 'rotate-right',
1863 cls : 'btn btn-default',
1864 html : '<i class="fa fa-repeat"></i>'
1872 cls : 'btn-group roo-upload-cropbox-rotate-left',
1873 action : 'rotate-left',
1877 cls : 'btn btn-default',
1878 html : '<i class="fa fa-undo"></i>'
1884 cls : 'btn-group roo-upload-cropbox-rotate-right',
1885 action : 'rotate-right',
1889 cls : 'btn btn-default',
1890 html : '<i class="fa fa-repeat"></i>'
1898 cls : 'btn-group roo-upload-cropbox-center',
1903 cls : 'btn btn-default',