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 maxPadding = (newCanvasWidth > newCanvasHeight) ? newCanvasWidth * 0.05 : newCanvasHeight * 0.05;
656 console.log("=================================");
657 console.log("old canvas width: " + this.canvasEl.width);
658 console.log("old canvas height: " + this.canvasEl.height);
660 console.log("new canvas width: " + newCanvasWidth);
661 console.log("new canvas height: " + newCanvasHeight);
663 console.log("old preview left: " + this.previewEl.getLeft(true));
664 console.log("old preview top: " + this.previewEl.getTop(true));
666 console.log("new preview left: " + newPreviewLeft);
667 console.log("new preview top: " + newPreviewTop);
669 console.log("padding left: " + paddingLeft);
670 console.log("padding top: " + paddingTop);
672 console.log("padding right: " + paddingRight);
673 console.log("padding bottom: " + paddingBottom);
675 console.log("max padding: " + maxPadding);
679 (this.rotate == 0 || this.rotate == 180) &&
681 width > this.imageEl.OriginWidth ||
682 height > this.imageEl.OriginHeight ||
683 (width < this.minWidth && height < this.minHeight)
691 (this.rotate == 90 || this.rotate == 270) &&
693 width > this.imageEl.OriginWidth ||
694 height > this.imageEl.OriginHeight ||
695 (width < this.minHeight && height < this.minWidth)
703 (this.rotate == 0 || this.rotate == 180) &&
705 (this.imageEl.OriginWidth / this.imageEl.OriginHeight >= this.minWidth / this.minHeight) && paddingLeft > maxPadding ||
706 (this.imageEl.OriginWidth / this.imageEl.OriginHeight >= this.minWidth / this.minHeight) && paddingRight > maxPadding ||
707 (this.imageEl.OriginWidth / this.imageEl.OriginHeight <= this.minWidth / this.minHeight) && paddingTop > maxPadding ||
708 (this.imageEl.OriginWidth / this.imageEl.OriginHeight <= this.minWidth / this.minHeight) && paddingBottom > maxPadding ||
718 (this.rotate == 90 || this.rotate == 270) &&
720 width < this.minHeight ||
721 width > this.imageEl.OriginWidth ||
722 height < this.minWidth ||
723 height > this.imageEl.OriginHeight
733 onRotateLeft : function(e)
735 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
737 var minScale = this.thumbEl.getWidth() / this.minWidth;
739 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
740 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
742 this.startScale = this.scale;
744 while (this.getScaleLevel() < minScale){
746 this.scale = this.scale + 1;
748 if(!this.zoomable()){
753 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
754 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
759 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
766 this.scale = this.startScale;
773 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
776 this.setThumbBoxSize();
777 this.setThumbBoxPosition();
778 this.setCanvasPosition();
783 this.fireEvent('rotate', this, 'left');
787 onRotateRight : function(e)
789 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
791 var minScale = this.thumbEl.getWidth() / this.minWidth;
793 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
794 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
796 this.startScale = this.scale;
798 while (this.getScaleLevel() < minScale){
800 this.scale = this.scale + 1;
802 if(!this.zoomable()){
807 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
808 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
813 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
820 this.scale = this.startScale;
827 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
830 this.setThumbBoxSize();
831 this.setThumbBoxPosition();
832 this.setCanvasPosition();
837 this.fireEvent('rotate', this, 'right');
840 onRotateFail : function()
842 this.errorEl.show(true);
846 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
851 this.previewEl.dom.innerHTML = '';
853 var canvasEl = document.createElement("canvas");
855 var contextEl = canvasEl.getContext("2d");
857 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
858 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
859 var center = this.imageEl.OriginWidth / 2;
861 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
862 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
863 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
864 center = this.imageEl.OriginHeight / 2;
867 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
869 contextEl.translate(center, center);
870 contextEl.rotate(this.rotate * Math.PI / 180);
872 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
874 this.canvasEl = document.createElement("canvas");
876 this.contextEl = this.canvasEl.getContext("2d");
878 switch (this.rotate) {
881 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
882 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
884 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
889 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
890 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
892 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
893 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);
897 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
902 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
903 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
905 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
906 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);
910 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);
915 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
916 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
918 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
919 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
923 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);
930 this.previewEl.appendChild(this.canvasEl);
932 this.setCanvasPosition(false);
937 if(!this.canvasLoaded){
941 var imageCanvas = document.createElement("canvas");
943 var imageContext = imageCanvas.getContext("2d");
945 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
946 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
948 var center = imageCanvas.width / 2;
950 imageContext.translate(center, center);
952 imageContext.rotate(this.rotate * Math.PI / 180);
954 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
956 var canvas = document.createElement("canvas");
958 var context = canvas.getContext("2d");
960 canvas.width = this.thumbEl.getWidth() / this.getScaleLevel();
962 canvas.height = this.thumbEl.getHeight() / this.getScaleLevel();
964 switch (this.rotate) {
967 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
968 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
970 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
971 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
973 var sx = this.thumbEl.getLeft(true) - this.previewEl.getLeft(true);
974 var sy = this.thumbEl.getTop(true) - this.previewEl.getTop(true);
976 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
977 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
979 if(canvas.width > this.outputMaxWidth) {
980 var scale = this.outputMaxWidth / canvas.width;
981 canvas.width = canvas.width * scale;
982 canvas.height = canvas.height * scale;
983 context.scale(scale, scale);
986 context.fillStyle = 'white';
987 context.fillRect(0, 0, this.thumbEl.getWidth() / this.getScaleLevel(), this.thumbEl.getHeight() / this.getScaleLevel());
990 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
995 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
996 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
998 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
999 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1001 var targetWidth = this.minWidth - 2 * x;
1002 var targetHeight = this.minHeight - 2 * y;
1006 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1007 scale = targetWidth / width;
1010 if(x > 0 && y == 0){
1011 scale = targetHeight / height;
1015 scale = targetWidth / width;
1018 scale = targetHeight / height;
1022 context.scale(scale, scale);
1024 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1025 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1027 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1028 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1030 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
1032 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1037 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
1038 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
1040 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1041 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1043 var targetWidth = this.minWidth - 2 * x;
1044 var targetHeight = this.minHeight - 2 * y;
1048 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1049 scale = targetWidth / width;
1052 if(x > 0 && y == 0){
1053 scale = targetHeight / height;
1057 scale = targetWidth / width;
1060 scale = targetHeight / height;
1064 context.scale(scale, scale);
1066 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1067 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1069 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1070 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1072 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1073 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
1075 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1080 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
1081 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
1083 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1084 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1086 var targetWidth = this.minWidth - 2 * x;
1087 var targetHeight = this.minHeight - 2 * y;
1091 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1092 scale = targetWidth / width;
1095 if(x > 0 && y == 0){
1096 scale = targetHeight / height;
1100 scale = targetWidth / width;
1103 scale = targetHeight / height;
1107 context.scale(scale, scale);
1108 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1109 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1111 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1112 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1114 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1116 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1123 this.cropData = canvas.toDataURL(this.cropType);
1125 if(this.fireEvent('crop', this, this.cropData) !== false){
1126 this.process(this.file, this.cropData);
1133 setThumbBoxSize : function()
1137 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1138 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1139 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1141 this.minWidth = width;
1142 this.minHeight = height;
1144 if(this.rotate == 90 || this.rotate == 270){
1145 this.minWidth = height;
1146 this.minHeight = width;
1150 height = this.windowSize;
1151 width = Math.ceil(this.minWidth * height / this.minHeight);
1153 if(this.minWidth > this.minHeight){
1154 width = this.windowSize;
1155 height = Math.ceil(this.minHeight * width / this.minWidth);
1158 this.thumbEl.setStyle({
1159 width : width + 'px',
1160 height : height + 'px'
1167 setThumbBoxPosition : function()
1169 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1170 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1172 this.thumbEl.setLeft(x);
1173 this.thumbEl.setTop(y);
1177 baseRotateLevel : function()
1179 this.baseRotate = 1;
1182 typeof(this.exif) != 'undefined' &&
1183 typeof(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1184 [1, 3, 6, 8].indexOf(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != -1
1186 this.baseRotate = this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']];
1189 this.rotate = Roo.dialog.UploadCropbox['Orientation'][this.baseRotate];
1193 baseScaleLevel : function()
1197 if(this.isDocument){
1199 if(this.baseRotate == 6 || this.baseRotate == 8){
1201 height = this.thumbEl.getHeight();
1202 this.baseScale = height / this.imageEl.OriginWidth;
1204 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1205 width = this.thumbEl.getWidth();
1206 this.baseScale = width / this.imageEl.OriginHeight;
1212 height = this.thumbEl.getHeight();
1213 this.baseScale = height / this.imageEl.OriginHeight;
1215 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1216 width = this.thumbEl.getWidth();
1217 this.baseScale = width / this.imageEl.OriginWidth;
1223 if(this.baseRotate == 6 || this.baseRotate == 8){
1225 width = this.thumbEl.getHeight();
1226 this.baseScale = width / this.imageEl.OriginHeight;
1228 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1229 height = this.thumbEl.getWidth();
1230 this.baseScale = height / this.imageEl.OriginHeight;
1233 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1234 height = this.thumbEl.getWidth();
1235 this.baseScale = height / this.imageEl.OriginHeight;
1237 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1238 width = this.thumbEl.getHeight();
1239 this.baseScale = width / this.imageEl.OriginWidth;
1246 width = this.thumbEl.getWidth();
1247 this.baseScale = width / this.imageEl.OriginWidth;
1249 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1250 height = this.thumbEl.getHeight();
1251 this.baseScale = height / this.imageEl.OriginHeight;
1254 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1256 height = this.thumbEl.getHeight();
1257 this.baseScale = height / this.imageEl.OriginHeight;
1259 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1260 width = this.thumbEl.getWidth();
1261 this.baseScale = width / this.imageEl.OriginWidth;
1266 if(this.imageEl.OriginWidth < this.minWidth || this.imageEl.OriginHeight < this.minHeight) {
1267 this.baseScale = width / this.minWidth;
1273 getScaleLevel : function()
1275 return this.baseScale * Math.pow(1.02, this.scale);
1278 onTouchStart : function(e)
1280 if(!this.canvasLoaded){
1281 this.beforeSelectFile(e);
1285 var touches = e.browserEvent.touches;
1291 if(touches.length == 1){
1292 this.onMouseDown(e);
1296 if(touches.length != 2){
1302 for(var i = 0, finger; finger = touches[i]; i++){
1303 coords.push(finger.pageX, finger.pageY);
1306 var x = Math.pow(coords[0] - coords[2], 2);
1307 var y = Math.pow(coords[1] - coords[3], 2);
1309 this.startDistance = Math.sqrt(x + y);
1311 this.startScale = this.scale;
1313 this.pinching = true;
1314 this.dragable = false;
1318 onTouchMove : function(e)
1320 if(!this.pinching && !this.dragable){
1324 var touches = e.browserEvent.touches;
1331 this.onMouseMove(e);
1337 for(var i = 0, finger; finger = touches[i]; i++){
1338 coords.push(finger.pageX, finger.pageY);
1341 var x = Math.pow(coords[0] - coords[2], 2);
1342 var y = Math.pow(coords[1] - coords[3], 2);
1344 this.endDistance = Math.sqrt(x + y);
1346 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1348 if(!this.zoomable()){
1349 this.scale = this.startScale;
1357 onTouchEnd : function(e)
1359 this.pinching = false;
1360 this.dragable = false;
1364 process : function(file, crop)
1367 this.maskEl.mask(this.loadingText);
1370 this.xhr = new XMLHttpRequest();
1372 file.xhr = this.xhr;
1374 this.xhr.open(this.method, this.url, true);
1377 "Accept": "application/json",
1378 "Cache-Control": "no-cache",
1379 "X-Requested-With": "XMLHttpRequest"
1382 for (var headerName in headers) {
1383 var headerValue = headers[headerName];
1385 this.xhr.setRequestHeader(headerName, headerValue);
1391 this.xhr.onload = function()
1393 _this.xhrOnLoad(_this.xhr);
1396 this.xhr.onerror = function()
1398 _this.xhrOnError(_this.xhr);
1401 var formData = new FormData();
1403 formData.append('returnHTML', 'NO');
1406 formData.append('crop', crop);
1407 var blobBin = atob(crop.split(',')[1]);
1409 for(var i = 0; i < blobBin.length; i++) {
1410 array.push(blobBin.charCodeAt(i));
1412 var croppedFile =new Blob([new Uint8Array(array)], {type: this.cropType});
1413 formData.append(this.paramName, croppedFile, file.name);
1416 if(typeof(file.filename) != 'undefined'){
1417 formData.append('filename', file.filename);
1420 if(typeof(file.mimetype) != 'undefined'){
1421 formData.append('mimetype', file.mimetype);
1424 if(this.fireEvent('arrange', this, formData) != false){
1425 this.xhr.send(formData);
1429 xhrOnLoad : function(xhr)
1432 this.maskEl.unmask();
1435 if (xhr.readyState !== 4) {
1436 this.fireEvent('exception', this, xhr);
1440 var response = Roo.decode(xhr.responseText);
1442 if(!response.success){
1443 this.fireEvent('exception', this, xhr);
1447 var response = Roo.decode(xhr.responseText);
1449 this.fireEvent('upload', this, response);
1453 xhrOnError : function()
1456 this.maskEl.unmask();
1459 Roo.log('xhr on error');
1461 var response = Roo.decode(xhr.responseText);
1467 prepare : function(file)
1470 this.maskEl.mask(this.loadingText);
1476 if(typeof(file) === 'string'){
1477 this.loadCanvas(file);
1481 if(!file || !this.urlAPI){
1486 if(typeof(file.type) != 'undefined' && file.type.length != 0) {
1487 this.cropType = file.type;
1492 if(this.fireEvent('prepare', this, this.file) != false){
1494 var reader = new FileReader();
1496 reader.onload = function (e) {
1497 if (e.target.error) {
1498 Roo.log(e.target.error);
1502 var buffer = e.target.result,
1503 dataView = new DataView(buffer),
1505 maxOffset = dataView.byteLength - 4,
1509 if (dataView.getUint16(0) === 0xffd8) {
1510 while (offset < maxOffset) {
1511 markerBytes = dataView.getUint16(offset);
1513 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1514 markerLength = dataView.getUint16(offset + 2) + 2;
1515 if (offset + markerLength > dataView.byteLength) {
1516 Roo.log('Invalid meta data: Invalid segment size.');
1520 if(markerBytes == 0xffe1){
1521 _this.parseExifData(
1528 offset += markerLength;
1538 var url = _this.urlAPI.createObjectURL(_this.file);
1540 _this.loadCanvas(url);
1545 reader.readAsArrayBuffer(this.file);
1551 parseExifData : function(dataView, offset, length)
1553 var tiffOffset = offset + 10,
1557 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1558 // No Exif data, might be XMP data instead
1562 // Check for the ASCII code for "Exif" (0x45786966):
1563 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1564 // No Exif data, might be XMP data instead
1567 if (tiffOffset + 8 > dataView.byteLength) {
1568 Roo.log('Invalid Exif data: Invalid segment size.');
1571 // Check for the two null bytes:
1572 if (dataView.getUint16(offset + 8) !== 0x0000) {
1573 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1576 // Check the byte alignment:
1577 switch (dataView.getUint16(tiffOffset)) {
1579 littleEndian = true;
1582 littleEndian = false;
1585 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1588 // Check for the TIFF tag marker (0x002A):
1589 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1590 Roo.log('Invalid Exif data: Missing TIFF marker.');
1593 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1594 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1599 tiffOffset + dirOffset,
1604 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1609 if (dirOffset + 6 > dataView.byteLength) {
1610 Roo.log('Invalid Exif data: Invalid directory offset.');
1613 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1614 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1615 if (dirEndOffset + 4 > dataView.byteLength) {
1616 Roo.log('Invalid Exif data: Invalid directory size.');
1619 for (i = 0; i < tagsNumber; i += 1) {
1623 dirOffset + 2 + 12 * i, // tag offset
1627 // Return the offset to the next directory:
1628 return dataView.getUint32(dirEndOffset, littleEndian);
1631 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1633 var tag = dataView.getUint16(offset, littleEndian);
1635 this.exif[tag] = this.getExifValue(
1639 dataView.getUint16(offset + 2, littleEndian), // tag type
1640 dataView.getUint32(offset + 4, littleEndian), // tag length
1645 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1647 var tagType = Roo.dialog.UploadCropbox.exifTagTypes[type],
1656 Roo.log('Invalid Exif data: Invalid tag type.');
1660 tagSize = tagType.size * length;
1661 // Determine if the value is contained in the dataOffset bytes,
1662 // or if the value at the dataOffset is a pointer to the actual data:
1663 dataOffset = tagSize > 4 ?
1664 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1665 if (dataOffset + tagSize > dataView.byteLength) {
1666 Roo.log('Invalid Exif data: Invalid data offset.');
1670 return tagType.getValue(dataView, dataOffset, littleEndian);
1673 for (i = 0; i < length; i += 1) {
1674 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1677 if (tagType.ascii) {
1679 // Concatenate the chars:
1680 for (i = 0; i < values.length; i += 1) {
1682 // Ignore the terminating NULL byte(s):
1683 if (c === '\u0000') {
1695 Roo.apply(Roo.dialog.UploadCropbox, {
1697 'Orientation': 0x0112
1703 3: 180, //'bottom-right',
1704 // 4: 'bottom-left',
1706 6: 90, //'right-top',
1707 // 7: 'right-bottom',
1708 8: 270 //'left-bottom'
1712 // byte, 8-bit unsigned int:
1714 getValue: function (dataView, dataOffset) {
1715 return dataView.getUint8(dataOffset);
1719 // ascii, 8-bit byte:
1721 getValue: function (dataView, dataOffset) {
1722 return String.fromCharCode(dataView.getUint8(dataOffset));
1727 // short, 16 bit int:
1729 getValue: function (dataView, dataOffset, littleEndian) {
1730 return dataView.getUint16(dataOffset, littleEndian);
1734 // long, 32 bit int:
1736 getValue: function (dataView, dataOffset, littleEndian) {
1737 return dataView.getUint32(dataOffset, littleEndian);
1741 // rational = two long values, first is numerator, second is denominator:
1743 getValue: function (dataView, dataOffset, littleEndian) {
1744 return dataView.getUint32(dataOffset, littleEndian) /
1745 dataView.getUint32(dataOffset + 4, littleEndian);
1749 // slong, 32 bit signed int:
1751 getValue: function (dataView, dataOffset, littleEndian) {
1752 return dataView.getInt32(dataOffset, littleEndian);
1756 // srational, two slongs, first is numerator, second is denominator:
1758 getValue: function (dataView, dataOffset, littleEndian) {
1759 return dataView.getInt32(dataOffset, littleEndian) /
1760 dataView.getInt32(dataOffset + 4, littleEndian);
1770 cls : 'btn-group roo-upload-cropbox-rotate-left',
1771 action : 'rotate-left',
1775 cls : 'btn btn-default',
1776 html : '<i class="fa fa-undo"></i>'
1782 cls : 'btn-group roo-upload-cropbox-picture',
1787 cls : 'btn btn-default',
1788 html : '<i class="fa fa-picture-o"></i>'
1794 cls : 'btn-group roo-upload-cropbox-rotate-right',
1795 action : 'rotate-right',
1799 cls : 'btn btn-default',
1800 html : '<i class="fa fa-repeat"></i>'
1808 cls : 'btn-group roo-upload-cropbox-rotate-left',
1809 action : 'rotate-left',
1813 cls : 'btn btn-default',
1814 html : '<i class="fa fa-undo"></i>'
1820 cls : 'btn-group roo-upload-cropbox-download',
1821 action : 'download',
1825 cls : 'btn btn-default',
1826 html : '<i class="fa fa-download"></i>'
1832 cls : 'btn-group roo-upload-cropbox-crop',
1837 cls : 'btn btn-default',
1838 html : '<i class="fa fa-crop"></i>'
1844 cls : 'btn-group roo-upload-cropbox-trash',
1849 cls : 'btn btn-default',
1850 html : '<i class="fa fa-trash"></i>'
1856 cls : 'btn-group roo-upload-cropbox-rotate-right',
1857 action : 'rotate-right',
1861 cls : 'btn btn-default',
1862 html : '<i class="fa fa-repeat"></i>'
1870 cls : 'btn-group roo-upload-cropbox-rotate-left',
1871 action : 'rotate-left',
1875 cls : 'btn btn-default',
1876 html : '<i class="fa fa-undo"></i>'
1882 cls : 'btn-group roo-upload-cropbox-rotate-right',
1883 action : 'rotate-right',
1887 cls : 'btn btn-default',
1888 html : '<i class="fa fa-repeat"></i>'
1896 cls : 'btn-group roo-upload-cropbox-center',
1901 cls : 'btn btn-default',