/* KOIVI JavaScript Image Cropper Copyright (C) 2004 Justin Koivisto This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. Justin Koivisto justin.koivisto@gmail.com http://www.koivi.com */ // variables that will be used within this script (can only set one image for cropping at a time) var x1=y1=-1; // x,y position of the first click in relation to the image var x2=y2=-1; // x,y position of the second click in relation to the image var posx=posy=posxF=posyF=-1; // the x,y position in relation to the image's parent for selection area var cropw=croph=-1; // width and height of crop area var imgx=imgy=-1; // x,y position of the image in relation to its absolutely-positioned parent var divx=divy=-1; // x,y position of the image's absolutely-positioned parent. var poffx1=poffy1=-1; // the page offset (how far down or right have we scrolled) for first click var poffx2=poffy2=-1; // the page offset (how far down or right have we scrolled) for first click var selElem=null; // the selection area div element var abselemdet=false; // absolute-position element detected in the path? var oldshad=''; var allowselect=false; var afterselect=''; var started=false; function imgLocation(elem) { var opera=(navigator.userAgent.toLowerCase().indexOf("opera") != -1); // get image location relative to its absolutely-positioned parent (or document) // at first click (should this happen both times??) if(imgx == -1 && imgy == -1){ // only if these are set to -1 (not calculated yet) obj=document.getElementById(elem); while(obj.offsetParent){ // check if this element has absolute positioning first var absposcheck=false; if(typeof(obj.currentStyle)!='undefined' && obj.currentStyle.position){ // this is MSIE, we need to work differently if( obj.currentStyle && obj.currentStyle.position == 'absolute'){ absposcheck=true; if(abselemdet) abselemdet=false; else abselemdet=true; } }else{ // others browsers if(window.getComputedStyle(obj, '').getPropertyValue('position') == 'absolute'){ absposcheck=true; if(abselemdet) abselemdet=false; else abselemdet=true; } } if(absposcheck){ // this is absolutely positioned // need to know the position in relation to the document as well while(obj.offsetParent){ // this gives the element relative to the document divy += obj.offsetTop; divx += obj.offsetLeft; obj = obj.offsetParent; } break; } imgy = obj.offsetTop; imgx = obj.offsetLeft; obj = obj.offsetParent; } if(abselemdet){ divy+=15; } } // calculate the current page offset (to compensate for scrolling!) if(typeof(window.pageXOffset)=='undefined'){ // MSIE browsers if(x1==-1 && y1==-1){ // this is the first click poffx1=document.body.scrollLeft; poffy1=document.body.scrollTop; }else{ // second click poffx2=document.body.scrollLeft; poffy2=document.body.scrollTop; } }else{ // others if(x1==-1 && y1==-1){ // this is the first click poffx1=window.pageXOffset; poffy1=window.pageYOffset; }else{ // second click poffx2=window.pageXOffset; poffy2=window.pageYOffset; } } } function getImageCropSelectionPoint(elem,evnt) { var opera=(navigator.userAgent.toLowerCase().indexOf("opera") != -1); if (started==false) return; imgLocation(elem); if(x1==-1 && y1==-1){ // this is the first click posx=evnt.clientX + poffx1 - divx; posy=evnt.clientY + poffy1 - divy; posxF=posx; posyF=posy; x1 = posx - imgx; y1 = posy - imgy; if(opera && abselemdet){ y1+=11; } }else{ // this is the second click x2 = evnt.clientX + poffx2 - divx - imgx; y2 = evnt.clientY + poffy2 - divy - imgy; if(opera && abselemdet){ y2+=11; } // since each click defines a corner, decide which ones were // used so the user doesn't have to click in any certain order if(x1>x2){ // right side was clicked first cropw = x1-x2; posxF=posx-cropw; }else{ cropw = x2-x1; } if(y1>y2){ // bottom was clicked first croph = y1-y2; posyF=posy-croph; }else{ croph = y2-y1; } oriw=document.getElementById(elem).style.width; oriw=oriw.substr(0,oriw.length-2); orih=document.getElementById(elem).style.height; orih=orih.substr(0,orih.length-2); kr=document.getElementById('kr'); if (kr.checked==true) { if (croph && (oriw < orih)) cropw = Math.round((croph / orih) * oriw); else croph = Math.round((cropw / oriw) * orih); } selElem = document.getElementById('imgJSselbox'); selElem.style.width = (cropw)+'px'; selElem.style.height = (croph)+'px'; selElem.style.left = posxF+'px'; selElem.style.top = posyF+'px'; selElem.style.visibility = 'visible'; selElem.innerHTML=cropw+' x '+croph; } } function StartSelectionPoint(elem,evnt) { if (allowselect==false) return; started=true; //clearImageCropArea(); getImageCropSelectionPoint(elem,evnt); } function EndSelectionPoint(elem,evnt) { if (allowselect==false) return; getImageCropSelectionPoint(elem,evnt); started=false; shadding(''); eval(afterselect+'();'); } function shadding(e) { if (oldshad!='') oldshad.style.background='url(none)'; oldshad=e; if (e!='') oldshad.style.background='url(img/shading.png)'; } function GetFilename(vvvv) { filenameee=document.getElementById(vvvv+'filename').value; // image name: I don't want to deal with "http://www.exmaple.com" stuff! filenameee=filenameee.replace(/^[a-z]+\:\/\/[^\/]+/,""); // in case there is a query string for the image (used in some systems to prevent image caching) filenameee=filenameee.replace(/\?.*$/,""); return filenameee; } function setImageCropAreaSubmit(e){ shadding(e); allowselect=true; afterselect='ImageCropArea'; } function ImageCropArea() { filename=GetFilename(''); // these 2 lines are in case the area wasn't selected from top left to bottom right. if(x1>x2) x1=x2; if(y1>y2) y1=y2; AJAXRequest('lib.commun.php','AfterCrop','POST','zc=1&sx='+x1+'&sy='+y1+'&sw='+cropw+'&sh='+croph+'&src='+filename); } function setImageSizeAreaSubmit(e){ shadding(e); imgLocation('crpImg'); posx=imgx + poffx1 - divx; posy=imgy + poffy1 - divy; posxF=posx; posyF=posy; x1 = posx - imgx; y1 = posy - imgy; allowselect=true; afterselect='ImageSizeArea'; } function ImageSizeArea() { filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','w='+cropw+'&h='+croph+'&src='+filename); } function setImageRotateSubmit(e) { shadding(e); filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','ra='+document.getElementById('angle').value+'&src='+filename); } function setImageContrastSubmit(e,s) { shadding(e); filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','fltr[]=cont|'+((s=='up')?'+':'-')+'10&src='+filename); } function setImageLightSubmit(e,s) { shadding(e); filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','fltr[]=brit|'+((s=='up')?'+':'-')+'10&src='+filename); } function setImageNBSubmit(e) { shadding(e); filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','fltr[]=gray&src='+filename); } function setImageFlipSubmit(e,s) { shadding(e); filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','fltr[]=flip|'+s+'&src='+filename); } function setImageBevelSubmit(e) { shadding(e); filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','fltr[]=bvl|10|222222|CCCCCC&src='+filename); } function setImageShadowSubmit(e) { shadding(e); filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','fltr[]=drop|5|5|222222|225&src='+filename); } function setImageSaveAreaSubmit(e) { shadding(e); ori_filename=GetFilename('ori_'); filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','save=ok&src='+filename+'&ori='+ori_filename); } function setImageUndoAreaSubmit(e) { if (!confirm('Annuler ?')) return; shadding(e); ori_filename=GetFilename('ori_'); filename=GetFilename(''); AJAXRequest('lib.commun.php','AfterCrop','POST','save=nok&src='+filename+'&ori='+ori_filename); } function AfterCrop(r) { var r=r.split("|"); document.getElementById('crpImg').style.background='url('+r[0]+')'; document.getElementById('crpImg').style.width=r[1]+'px'; document.getElementById('crpImg').style.height=r[2]+'px'; clearImageCropArea(); Load('hidden'); shadding(''); allowselect=false; } function clearImageCropArea(){ posyF=posxF=x1=x2=y1=y2=imgx=imgy=divx=divy=cropw=croph=poffx1=poffx2=poffy1=poffy2=posx=posy=-1; if(selElem){ selElem.style.width = 0; selElem.style.height = 0; selElem.style.left = 0; selElem.style.top = 0; selElem.style.visibility = 'hidden'; selElem = null; } } function Load(v) { document.getElementById('divload').style.visibility =v; document.getElementById('toolbar').style.visibility =(v=='')?'hidden':''; } function AJAXRequest(page,retfonc,methode,data) { Load(''); var xhr_object = null; if(window.XMLHttpRequest) // Firefox xhr_object = new XMLHttpRequest(); else if(window.ActiveXObject) // Internet Explorer xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); else { // XMLHttpRequest non supporté par le navigateur alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); return; } if (data=="") data=null; if(methode == "GET" && data != null) { page += "?"+data; data = null; } xhr_object.open(methode, page, true); xhr_object.onreadystatechange = function() { if(xhr_object.readyState == 4) { var RetAjax=xhr_object.responseText; eval(retfonc+'(RetAjax);'); } } if(methode == "POST") xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr_object.send(data); }