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
20 * Create a new UploadCropbox
21 * @param {Object} config The config object
24 Roo.bootstrap.UploadCropbox = function(config){
25 Roo.bootstrap.UploadCropbox.superclass.constructor.call(this, config);
29 * @event beforeselectfile
30 * Fire before select file
31 * @param {Roo.bootstrap.UploadCropbox} this
33 "beforeselectfile" : true,
36 * Fire after initEvent
37 * @param {Roo.bootstrap.UploadCropbox} this
42 * Fire after initEvent
43 * @param {Roo.bootstrap.UploadCropbox} this
44 * @param {String} data
49 * Fire when preparing the file data
50 * @param {Roo.bootstrap.UploadCropbox} this
51 * @param {Object} file
56 * Fire when get exception
57 * @param {Roo.bootstrap.UploadCropbox} this
58 * @param {XMLHttpRequest} xhr
62 * @event beforeloadcanvas
63 * Fire before load the canvas
64 * @param {Roo.bootstrap.UploadCropbox} this
67 "beforeloadcanvas" : true,
70 * Fire when trash image
71 * @param {Roo.bootstrap.UploadCropbox} this
76 * Fire when download the image
77 * @param {Roo.bootstrap.UploadCropbox} this
81 * @event footerbuttonclick
82 * Fire when footerbuttonclick
83 * @param {Roo.bootstrap.UploadCropbox} this
84 * @param {String} type
86 "footerbuttonclick" : true,
90 * @param {Roo.bootstrap.UploadCropbox} this
95 * Fire when rotate the image
96 * @param {Roo.bootstrap.UploadCropbox} this
102 * Fire when inspect the file
103 * @param {Roo.bootstrap.UploadCropbox} this
104 * @param {Object} file
109 * Fire when xhr upload the file
110 * @param {Roo.bootstrap.UploadCropbox} this
111 * @param {Object} data
116 * Fire when arrange the file data
117 * @param {Roo.bootstrap.UploadCropbox} this
118 * @param {Object} formData
123 this.buttons = this.buttons || Roo.bootstrap.UploadCropbox.footer.STANDARD;
126 Roo.extend(Roo.bootstrap.UploadCropbox, Roo.bootstrap.Component, {
128 emptyText : 'Click to upload image',
129 rotateNotify : 'Image is too small to rotate',
144 cropType : 'image/jpeg',
146 canvasLoaded : false,
149 getAutoCreate : function()
153 cls : 'roo-upload-cropbox',
157 cls : 'roo-upload-cropbox-body',
158 style : 'cursor:pointer',
162 cls : 'roo-upload-cropbox-selector',
167 cls : 'roo-upload-cropbox-preview'
171 cls : 'roo-upload-cropbox-thumb'
175 cls : 'roo-upload-cropbox-empty-notify',
176 html : this.emptyText
180 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
181 html : this.rotateNotify
187 cls : 'roo-upload-cropbox-footer',
190 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
200 onRender : function(ct, position)
202 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
204 if (this.buttons.length) {
206 Roo.each(this.buttons, function(bb) {
208 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
210 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
216 initEvents : function()
218 this.urlAPI = (window.createObjectURL && window) ||
219 (window.URL && URL.revokeObjectURL && URL) ||
220 (window.webkitURL && webkitURL);
222 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
223 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
225 this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
226 this.selectorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
227 this.selectorEl.hide();
229 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
230 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
232 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
233 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
236 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
237 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
239 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
240 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
243 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
244 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
245 this.footerEl.hide();
247 this.setThumbBoxSize();
253 this.fireEvent('initial', this);
260 window.addEventListener("resize", function() { _this.resize(); } );
262 this.bodyEl.on('click', this.beforeSelectFile, this);
265 this.bodyEl.on('touchstart', this.onTouchStart, this);
266 this.bodyEl.on('touchmove', this.onTouchMove, this);
267 this.bodyEl.on('touchend', this.onTouchEnd, this);
271 this.bodyEl.on('mousedown', this.onMouseDown, this);
272 this.bodyEl.on('mousemove', this.onMouseMove, this);
273 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
274 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
275 Roo.get(document).on('mouseup', this.onMouseUp, this);
278 this.selectorEl.on('change', this.onFileSelected, this);
287 this.dragable = false;
288 this.pinching = false;
291 this.cropData = false;
292 this.notifyEl.dom.innerHTML = this.emptyText;
298 if(this.fireEvent('resize', this) != false){
299 this.setThumbBoxPosition();
300 this.setCanvasPosition();
304 onFooterButtonClick : function(e, el, o, type)
308 this.onRotateLeft(e);
310 case 'rotate-right' :
311 this.onRotateRight(e);
314 this.beforeSelectFile(e);
329 this.fireEvent('footerbuttonclick', this, type);
332 beforeSelectFile : function(e)
336 if(this.fireEvent('beforeselectfile', this) != false){
337 this.selectorEl.dom.click();
341 onFileSelected : function(e)
345 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
349 var file = this.selectorEl.dom.files[0];
351 if(this.fireEvent('inspect', this, file) != false){
355 this.selectorEl.dom.value = '';
361 this.fireEvent('trash', this);
364 download : function(e)
366 this.fireEvent('download', this);
369 loadCanvas : function(src)
371 if(this.fireEvent('beforeloadcanvas', this, src) != false){
375 this.imageEl = document.createElement('img');
379 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
381 this.imageEl.src = src;
385 onLoadCanvas : function()
387 this.bodyEl.un('click', this.beforeSelectFile, this);
389 this.notifyEl.hide();
391 this.footerEl.show();
393 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
394 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
396 this.baseRotateLevel();
399 this.setThumbBoxSize();
402 this.setThumbBoxPosition();
404 this.baseScaleLevel();
410 this.canvasLoaded = true;
414 setCanvasPosition : function()
420 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
421 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
423 this.previewEl.setLeft(pw);
424 this.previewEl.setTop(ph);
428 onMouseDown : function(e)
432 this.dragable = true;
433 this.pinching = false;
435 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
436 this.dragable = false;
440 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
441 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
445 onMouseMove : function(e)
449 if(!this.canvasLoaded){
457 var minX = Math.ceil(this.thumbEl.getLeft(true));
458 var minY = Math.ceil(this.thumbEl.getTop(true));
460 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
461 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
463 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
464 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
469 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
470 var bgY = Math.ceil(y + this.previewEl.getTop(true));
472 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
473 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
475 this.previewEl.setLeft(bgX);
476 this.previewEl.setTop(bgY);
478 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
479 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
482 onMouseUp : function(e)
486 this.dragable = false;
489 onMouseWheel : function(e)
493 this.startScale = this.scale;
495 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
497 if(!this.zoomable()){
498 this.scale = this.startScale;
507 zoomable : function()
509 var minScale = this.thumbEl.getWidth() / this.minWidth;
511 if(this.minWidth < this.minHeight){
512 minScale = this.thumbEl.getHeight() / this.minHeight;
515 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
516 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
520 (this.rotate == 0 || this.rotate == 180) &&
522 width > this.imageEl.OriginWidth ||
523 height > this.imageEl.OriginHeight ||
524 (width < this.minWidth && height < this.minHeight)
532 (this.rotate == 90 || this.rotate == 270) &&
534 width > this.imageEl.OriginWidth ||
535 height > this.imageEl.OriginHeight ||
536 (width < this.minHeight && height < this.minWidth)
544 (this.rotate == 0 || this.rotate == 180) &&
546 width < this.minWidth ||
547 width > this.imageEl.OriginWidth ||
548 height < this.minHeight ||
549 height > this.imageEl.OriginHeight
557 (this.rotate == 90 || this.rotate == 270) &&
559 width < this.minHeight ||
560 width > this.imageEl.OriginWidth ||
561 height < this.minWidth ||
562 height > this.imageEl.OriginHeight
572 onRotateLeft : function(e)
574 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
576 var minScale = this.thumbEl.getWidth() / this.minWidth;
578 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
579 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
581 this.startScale = this.scale;
583 while (this.getScaleLevel() < minScale){
585 this.scale = this.scale + 1;
587 if(!this.zoomable()){
592 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
593 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
598 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
605 this.scale = this.startScale;
612 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
615 this.setThumbBoxSize();
616 this.setThumbBoxPosition();
617 this.setCanvasPosition();
622 this.fireEvent('rotate', this, 'left');
626 onRotateRight : function(e)
628 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
630 var minScale = this.thumbEl.getWidth() / this.minWidth;
632 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
633 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
635 this.startScale = this.scale;
637 while (this.getScaleLevel() < minScale){
639 this.scale = this.scale + 1;
641 if(!this.zoomable()){
646 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
647 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
652 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
659 this.scale = this.startScale;
666 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
669 this.setThumbBoxSize();
670 this.setThumbBoxPosition();
671 this.setCanvasPosition();
676 this.fireEvent('rotate', this, 'right');
679 onRotateFail : function()
681 this.errorEl.show(true);
685 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
690 this.previewEl.dom.innerHTML = '';
692 var canvasEl = document.createElement("canvas");
694 var contextEl = canvasEl.getContext("2d");
696 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
697 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
698 var center = this.imageEl.OriginWidth / 2;
700 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
701 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
702 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
703 center = this.imageEl.OriginHeight / 2;
706 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
708 contextEl.translate(center, center);
709 contextEl.rotate(this.rotate * Math.PI / 180);
711 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
713 this.canvasEl = document.createElement("canvas");
715 this.contextEl = this.canvasEl.getContext("2d");
717 switch (this.rotate) {
720 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
721 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
723 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
728 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
729 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
731 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
732 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);
736 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
741 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
742 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
744 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
745 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);
749 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);
754 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
755 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
757 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
758 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
762 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);
769 this.previewEl.appendChild(this.canvasEl);
771 this.setCanvasPosition();
776 if(!this.canvasLoaded){
780 var imageCanvas = document.createElement("canvas");
782 var imageContext = imageCanvas.getContext("2d");
784 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
785 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
787 var center = imageCanvas.width / 2;
789 imageContext.translate(center, center);
791 imageContext.rotate(this.rotate * Math.PI / 180);
793 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
795 var canvas = document.createElement("canvas");
797 var context = canvas.getContext("2d");
799 canvas.width = this.minWidth;
800 canvas.height = this.minHeight;
802 switch (this.rotate) {
805 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
806 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
808 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
809 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
811 var targetWidth = this.minWidth - 2 * x;
812 var targetHeight = this.minHeight - 2 * y;
816 if((x == 0 && y == 0) || (x == 0 && y > 0)){
817 scale = targetWidth / width;
821 scale = targetHeight / height;
825 scale = targetWidth / width;
828 scale = targetHeight / height;
832 context.scale(scale, scale);
834 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
835 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
837 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
838 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
840 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
845 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
846 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
848 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
849 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
851 var targetWidth = this.minWidth - 2 * x;
852 var targetHeight = this.minHeight - 2 * y;
856 if((x == 0 && y == 0) || (x == 0 && y > 0)){
857 scale = targetWidth / width;
861 scale = targetHeight / height;
865 scale = targetWidth / width;
868 scale = targetHeight / height;
872 context.scale(scale, scale);
874 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
875 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
877 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
878 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
880 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
882 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
887 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
888 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
890 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
891 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
893 var targetWidth = this.minWidth - 2 * x;
894 var targetHeight = this.minHeight - 2 * y;
898 if((x == 0 && y == 0) || (x == 0 && y > 0)){
899 scale = targetWidth / width;
903 scale = targetHeight / height;
907 scale = targetWidth / width;
910 scale = targetHeight / height;
914 context.scale(scale, scale);
916 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
917 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
919 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
920 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
922 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
923 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
925 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
930 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
931 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
933 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
934 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
936 var targetWidth = this.minWidth - 2 * x;
937 var targetHeight = this.minHeight - 2 * y;
941 if((x == 0 && y == 0) || (x == 0 && y > 0)){
942 scale = targetWidth / width;
946 scale = targetHeight / height;
950 scale = targetWidth / width;
953 scale = targetHeight / height;
957 context.scale(scale, scale);
959 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
960 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
962 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
963 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
965 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
967 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
974 this.cropData = canvas.toDataURL(this.cropType);
976 this.fireEvent('crop', this, this.cropData);
982 setThumbBoxSize : function()
986 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
987 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
988 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
990 this.minWidth = width;
991 this.minHeight = height;
993 if(this.rotate == 90 || this.rotate == 270){
994 this.minWidth = height;
995 this.minHeight = width;
1000 width = Math.ceil(this.minWidth * height / this.minHeight);
1002 if(this.minWidth > this.minHeight){
1004 height = Math.ceil(this.minHeight * width / this.minWidth);
1007 this.thumbEl.setStyle({
1008 width : width + 'px',
1009 height : height + 'px'
1016 setThumbBoxPosition : function()
1018 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1019 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1021 this.thumbEl.setLeft(x);
1022 this.thumbEl.setTop(y);
1026 baseRotateLevel : function()
1028 this.baseRotate = 1;
1031 typeof(this.exif) != 'undefined' &&
1032 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1033 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1035 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1038 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1042 baseScaleLevel : function()
1046 if(this.isDocument){
1048 if(this.baseRotate == 6 || this.baseRotate == 8){
1050 height = this.thumbEl.getHeight();
1051 this.baseScale = height / this.imageEl.OriginWidth;
1053 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1054 width = this.thumbEl.getWidth();
1055 this.baseScale = width / this.imageEl.OriginHeight;
1061 height = this.thumbEl.getHeight();
1062 this.baseScale = height / this.imageEl.OriginHeight;
1064 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1065 width = this.thumbEl.getWidth();
1066 this.baseScale = width / this.imageEl.OriginWidth;
1072 if(this.baseRotate == 6 || this.baseRotate == 8){
1074 width = this.thumbEl.getHeight();
1075 this.baseScale = width / this.imageEl.OriginHeight;
1077 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1078 height = this.thumbEl.getWidth();
1079 this.baseScale = height / this.imageEl.OriginHeight;
1082 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1083 height = this.thumbEl.getWidth();
1084 this.baseScale = height / this.imageEl.OriginHeight;
1086 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1087 width = this.thumbEl.getHeight();
1088 this.baseScale = width / this.imageEl.OriginWidth;
1095 width = this.thumbEl.getWidth();
1096 this.baseScale = width / this.imageEl.OriginWidth;
1098 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1099 height = this.thumbEl.getHeight();
1100 this.baseScale = height / this.imageEl.OriginHeight;
1103 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1105 height = this.thumbEl.getHeight();
1106 this.baseScale = height / this.imageEl.OriginHeight;
1108 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1109 width = this.thumbEl.getWidth();
1110 this.baseScale = width / this.imageEl.OriginWidth;
1118 getScaleLevel : function()
1120 return this.baseScale * Math.pow(1.1, this.scale);
1123 onTouchStart : function(e)
1125 if(!this.canvasLoaded){
1126 this.beforeSelectFile(e);
1130 var touches = e.browserEvent.touches;
1136 if(touches.length == 1){
1137 this.onMouseDown(e);
1141 if(touches.length != 2){
1147 for(var i = 0, finger; finger = touches[i]; i++){
1148 coords.push(finger.pageX, finger.pageY);
1151 var x = Math.pow(coords[0] - coords[2], 2);
1152 var y = Math.pow(coords[1] - coords[3], 2);
1154 this.startDistance = Math.sqrt(x + y);
1156 this.startScale = this.scale;
1158 this.pinching = true;
1159 this.dragable = false;
1163 onTouchMove : function(e)
1165 if(!this.pinching && !this.dragable){
1169 var touches = e.browserEvent.touches;
1176 this.onMouseMove(e);
1182 for(var i = 0, finger; finger = touches[i]; i++){
1183 coords.push(finger.pageX, finger.pageY);
1186 var x = Math.pow(coords[0] - coords[2], 2);
1187 var y = Math.pow(coords[1] - coords[3], 2);
1189 this.endDistance = Math.sqrt(x + y);
1191 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1193 if(!this.zoomable()){
1194 this.scale = this.startScale;
1202 onTouchEnd : function(e)
1204 this.pinching = false;
1205 this.dragable = false;
1209 process : function(file, crop)
1211 this.xhr = new XMLHttpRequest();
1213 if(typeof(this.file.id) != 'undefined' && this.file.id * 1 > 0){
1217 file.xhr = this.xhr;
1219 this.xhr.open(this.method, this.url, true);
1222 "Accept": "application/json",
1223 "Cache-Control": "no-cache",
1224 "X-Requested-With": "XMLHttpRequest"
1227 for (var headerName in headers) {
1228 var headerValue = headers[headerName];
1230 this.xhr.setRequestHeader(headerName, headerValue);
1236 this.xhr.onload = function()
1238 _this.xhrOnLoad(_this.xhr);
1241 this.xhr.onerror = function()
1243 _this.xhrOnError(_this.xhr);
1246 var formData = new FormData();
1248 formData.append('returnHTML', 'NO');
1251 formData.append('crop', crop);
1254 formData.append(this.paramName, file, file.name);
1256 if(this.fireEvent('arrange', this, formData) != false){
1257 this.xhr.send(formData);
1261 xhrOnLoad : function(xhr)
1263 if (xhr.readyState !== 4) {
1264 this.fireEvent('exception', this, xhr);
1268 var response = Roo.decode(xhr.responseText);
1270 if(!response.success){
1271 this.fireEvent('exception', this, xhr);
1275 var response = Roo.decode(xhr.responseText);
1277 this.fireEvent('upload', this, response);
1281 xhrOnError : function()
1283 Roo.log('xhr on error');
1285 var response = Roo.decode(xhr.responseText);
1291 uploadFromSource : function(file, crop)
1293 this.xhr = new XMLHttpRequest();
1295 this.xhr.open(this.method, this.url, true);
1298 "Accept": "application/json",
1299 "Cache-Control": "no-cache",
1300 "X-Requested-With": "XMLHttpRequest"
1303 for (var headerName in headers) {
1304 var headerValue = headers[headerName];
1306 this.xhr.setRequestHeader(headerName, headerValue);
1312 this.xhr.onload = function()
1314 _this.xhrOnLoad(_this.xhr);
1317 this.xhr.onerror = function()
1319 _this.xhrOnError(_this.xhr);
1322 var formData = new FormData();
1324 formData.append('returnHTML', 'NO');
1326 formData.append('crop', crop);
1328 if(typeof(file.filename) != 'undefined'){
1329 formData.append('filename', file.filename);
1332 if(typeof(file.mimetype) != 'undefined'){
1333 formData.append('mimetype', file.mimetype);
1336 if(this.fireEvent('arrange', this, formData) != false){
1337 this.xhr.send(formData);
1341 prepare : function(file)
1346 if(typeof(file) === 'string'){
1347 this.loadCanvas(file);
1351 if(!file || !this.urlAPI){
1356 this.cropType = file.type;
1360 if(this.fireEvent('prepare', this, this.file) != false){
1362 var reader = new FileReader();
1364 reader.onload = function (e) {
1365 if (e.target.error) {
1366 Roo.log(e.target.error);
1370 var buffer = e.target.result,
1371 dataView = new DataView(buffer),
1373 maxOffset = dataView.byteLength - 4,
1377 if (dataView.getUint16(0) === 0xffd8) {
1378 while (offset < maxOffset) {
1379 markerBytes = dataView.getUint16(offset);
1381 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1382 markerLength = dataView.getUint16(offset + 2) + 2;
1383 if (offset + markerLength > dataView.byteLength) {
1384 Roo.log('Invalid meta data: Invalid segment size.');
1388 if(markerBytes == 0xffe1){
1389 _this.parseExifData(
1396 offset += markerLength;
1406 var url = _this.urlAPI.createObjectURL(_this.file);
1408 _this.loadCanvas(url);
1413 reader.readAsArrayBuffer(this.file);
1419 parseExifData : function(dataView, offset, length)
1421 var tiffOffset = offset + 10,
1425 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1426 // No Exif data, might be XMP data instead
1430 // Check for the ASCII code for "Exif" (0x45786966):
1431 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1432 // No Exif data, might be XMP data instead
1435 if (tiffOffset + 8 > dataView.byteLength) {
1436 Roo.log('Invalid Exif data: Invalid segment size.');
1439 // Check for the two null bytes:
1440 if (dataView.getUint16(offset + 8) !== 0x0000) {
1441 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1444 // Check the byte alignment:
1445 switch (dataView.getUint16(tiffOffset)) {
1447 littleEndian = true;
1450 littleEndian = false;
1453 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1456 // Check for the TIFF tag marker (0x002A):
1457 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1458 Roo.log('Invalid Exif data: Missing TIFF marker.');
1461 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1462 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1467 tiffOffset + dirOffset,
1472 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1477 if (dirOffset + 6 > dataView.byteLength) {
1478 Roo.log('Invalid Exif data: Invalid directory offset.');
1481 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1482 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1483 if (dirEndOffset + 4 > dataView.byteLength) {
1484 Roo.log('Invalid Exif data: Invalid directory size.');
1487 for (i = 0; i < tagsNumber; i += 1) {
1491 dirOffset + 2 + 12 * i, // tag offset
1495 // Return the offset to the next directory:
1496 return dataView.getUint32(dirEndOffset, littleEndian);
1499 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1501 var tag = dataView.getUint16(offset, littleEndian);
1503 this.exif[tag] = this.getExifValue(
1507 dataView.getUint16(offset + 2, littleEndian), // tag type
1508 dataView.getUint32(offset + 4, littleEndian), // tag length
1513 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1515 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1524 Roo.log('Invalid Exif data: Invalid tag type.');
1528 tagSize = tagType.size * length;
1529 // Determine if the value is contained in the dataOffset bytes,
1530 // or if the value at the dataOffset is a pointer to the actual data:
1531 dataOffset = tagSize > 4 ?
1532 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1533 if (dataOffset + tagSize > dataView.byteLength) {
1534 Roo.log('Invalid Exif data: Invalid data offset.');
1538 return tagType.getValue(dataView, dataOffset, littleEndian);
1541 for (i = 0; i < length; i += 1) {
1542 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1545 if (tagType.ascii) {
1547 // Concatenate the chars:
1548 for (i = 0; i < values.length; i += 1) {
1550 // Ignore the terminating NULL byte(s):
1551 if (c === '\u0000') {
1563 Roo.apply(Roo.bootstrap.UploadCropbox, {
1565 'Orientation': 0x0112
1571 3: 180, //'bottom-right',
1572 // 4: 'bottom-left',
1574 6: 90, //'right-top',
1575 // 7: 'right-bottom',
1576 8: 270 //'left-bottom'
1580 // byte, 8-bit unsigned int:
1582 getValue: function (dataView, dataOffset) {
1583 return dataView.getUint8(dataOffset);
1587 // ascii, 8-bit byte:
1589 getValue: function (dataView, dataOffset) {
1590 return String.fromCharCode(dataView.getUint8(dataOffset));
1595 // short, 16 bit int:
1597 getValue: function (dataView, dataOffset, littleEndian) {
1598 return dataView.getUint16(dataOffset, littleEndian);
1602 // long, 32 bit int:
1604 getValue: function (dataView, dataOffset, littleEndian) {
1605 return dataView.getUint32(dataOffset, littleEndian);
1609 // rational = two long values, first is numerator, second is denominator:
1611 getValue: function (dataView, dataOffset, littleEndian) {
1612 return dataView.getUint32(dataOffset, littleEndian) /
1613 dataView.getUint32(dataOffset + 4, littleEndian);
1617 // slong, 32 bit signed int:
1619 getValue: function (dataView, dataOffset, littleEndian) {
1620 return dataView.getInt32(dataOffset, littleEndian);
1624 // srational, two slongs, first is numerator, second is denominator:
1626 getValue: function (dataView, dataOffset, littleEndian) {
1627 return dataView.getInt32(dataOffset, littleEndian) /
1628 dataView.getInt32(dataOffset + 4, littleEndian);
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-picture',
1655 cls : 'btn btn-default',
1656 html : '<i class="fa fa-picture-o"></i>'
1662 cls : 'btn-group roo-upload-cropbox-rotate-right',
1663 action : 'rotate-right',
1667 cls : 'btn btn-default',
1668 html : '<i class="fa fa-repeat"></i>'
1676 cls : 'btn-group roo-upload-cropbox-rotate-left',
1677 action : 'rotate-left',
1681 cls : 'btn btn-default',
1682 html : '<i class="fa fa-undo"></i>'
1688 cls : 'btn-group roo-upload-cropbox-download',
1689 action : 'download',
1693 cls : 'btn btn-default',
1694 html : '<i class="fa fa-download"></i>'
1700 cls : 'btn-group roo-upload-cropbox-crop',
1705 cls : 'btn btn-default',
1706 html : '<i class="fa fa-crop"></i>'
1712 cls : 'btn-group roo-upload-cropbox-trash',
1717 cls : 'btn btn-default',
1718 html : '<i class="fa fa-trash"></i>'
1724 cls : 'btn-group roo-upload-cropbox-rotate-right',
1725 action : 'rotate-right',
1729 cls : 'btn btn-default',
1730 html : '<i class="fa fa-repeat"></i>'
1738 cls : 'btn-group roo-upload-cropbox-rotate-left',
1739 action : 'rotate-left',
1743 cls : 'btn btn-default',
1744 html : '<i class="fa fa-undo"></i>'
1750 cls : 'btn-group roo-upload-cropbox-rotate-right',
1751 action : 'rotate-right',
1755 cls : 'btn btn-default',
1756 html : '<i class="fa fa-repeat"></i>'