'; //mrml dtd path $mrmldtd=''; //path for image upload: $ImagePath="image/"; //images extension: $ImagesExtension="gif,jpeg,jpg,tiff,tif,png,pict,pcx,bmp,pgm,ppm"; //***************VARIABLES**************** //global variable $Depth=0; $Collection=array(); $NbCollection=-1; $Algorithm=array(); $NbAlgorithm=-1; $Property=array(); $NbProperty=-1; $NbPropertyAlgo=array(); $Parent=array(); //Javascript functions //to submit the form in case of algorithm change echo " "; //***********SOCKET FUNCTIONS************ //Open a socket function connection($server,$port) { $socket = fsockopen ($server,$port, &$errno, &$errstr, 30); if (!$socket) {die("$errstr ($errno)
\n");} return $socket; } //Make a request to the server and return the answer function request($socket,$request) { fputs ($socket, $request); $answer=""; while (!feof($socket)) { $buf = fgets($socket,1); $answer .=$buf; } return $answer; } //***********REQUEST FUNCTIONS************ function MakeRandom() { //global variables global $xml,$mrmldtd; global $Algorithm,$NbAlgorithm; global $AlgorithmId,$CollectionId,$SessionId; global $server,$port,$Return; //find the Algorithm Type for ($i=0;$i<=$NbAlgorithm;$i++){ if ($Algorithm[$i]["ALGORITHM-ID"]==$AlgorithmId) {$AlgorithmType=$Algorithm[$i]["ALGORITHM-TYPE"];} } //The MRML request to fetch a random set of images $RandomRequest= $xml . $mrmldtd . " "; //Make the request $socket=connection($server,$port); $answer=request($socket,$RandomRequest); fclose($socket); //Create a xml parser, Parse the answer and free the parser $MRML_parser = xml_parser_create(); xml_set_element_handler($MRML_parser, "MRMLstart", "MRMLend"); xml_parse($MRML_parser, $answer); xml_parser_free($MRML_parser); } //make a query function MakeQuery() { //global variables global $HTTP_POST_VARS,$HTTP_POST_FILES; global $xml,$mrmldtd,$ImagePath,$phppath; global $Algorithm,$NbAlgorithm,$ImgHeight,$ImgWidth; global $AlgorithmId,$CollectionId,$SessionId,$OldCollectionId; global $server,$port,$Return,$url,$SERVER_NAME,$DOCUMENT_ROOT; global $Property,$NbProperty,$NbPropertyAlgo,$OldAlgorithm; global $imageloc,$imagerel,$nbimage,$QueryImgNb,$QueryImgRelLoc; //get the upload file name $NameImgUpload=$HTTP_POST_FILES['UserImage']['name']; //if there is a upload file... if ($NameImgUpload!="") { //check if file exist if ($HTTP_POST_FILES['UserImage']['tmp_name']=="none") { die("

file upload error: $NameImgUpload


"); } //check file extension CheckExtension($NameImgUpload); //move the file to $ImagePath directory $temp=explode(".",$NameImgUpload); $desti1=$ImagePath.$NameImgUpload; $desti2 = $ImagePath . $temp[0] . ".jpg"; if ($desti1==$desti2) {$desti2=$ImagePath . "Thumb_".$temp[0] . ".jpg";} if (!move_uploaded_file($HTTP_POST_FILES['UserImage']['tmp_name'],$desti1)) {die ("error in transfert");} //convert image to jpg with "convert" $command=escapeShellCmd("/usr/bin/convert -geometry $ImgWidthx$ImgHeight $desti1 $desti2"); system($command); } //check if the url and his extension is valid. if ($url!="") { if (!@fopen($url,"rb")){ die("

Bad url: $url


"); } //check file extension CheckExtension($url); //copy image from url $urltmp=explode('/',$url); $urltmp2=explode('.',$urltmp[count($urltmp)-1]); $dest1=$ImagePath."tmp.".$urltmp2[1]; $dest2 = $ImagePath . $urltmp2[0] . ".jpg"; if (!($fp = fopen($url,"r"))) die("Could not open src"); if (!($fp2 = fopen($dest1,"w"))) die("Could not open dest"); while ($contents = fread( $fp,4096)) { fwrite( $fp2, $contents); } fclose($fp); fclose($fp2); //convert image to jpg $command=escapeShellCmd("/usr/bin/convert -geometry $ImgWidthx$ImgHeight $dest1 $dest2"); system($command); } //find the right algorithm for ($i=0;$i<=$NbAlgorithm;$i++){ if ($Algorithm[$i]["ALGORITHM-ID"]==$AlgorithmId) { $AlgorithmType=$Algorithm[$i]["ALGORITHM-TYPE"]; $algonum=$i; } } $nbimage=0; $nbrel=0; $nbthumb=0; //find image location,relevance and thumbnail location while (list ($key, $val) = each ($HTTP_POST_VARS)) { if (is_int(strpos($key,"image_"))){ $imageloc[$nbimage]=$val; $nbimage++; } if (is_int(strpos($key,"rel_img_"))){ $imagerel[$nbrel]=$val; $nbrel++; } if (is_int(strpos($key,"thumb_img_"))){ $imagethumb[$nbthumb]=$val; $nbthumb++; } } //find the query image $QueryImgNb=0; for ($i=0;$i<$nbimage;$i++){ if ($imagerel[$i]==-1 || $imagerel[$i]==1) { $QueryImg[$QueryImgNb]=$imageloc[$i]; $QueryImgThumb[$QueryImgNb]=$imagethumb[$i]; $QueryImgRel[$QueryImgNb]=$imagerel[$i]; if ($imagerel[$i]==1) { $QueryImgRelLoc[$QueryImg[$QueryImgNb]]=1; } $QueryImgNb++; } } //The MRML request $QueryRequest= $xml . $mrmldtd . " "; //add the image location to the request for ($i=0;$i<$nbimage;$i++){ $QueryRequest .= "\n"; } //ad the image passed by url if ($url!="") { $QueryRequest .= "\n"; $QueryImg[$QueryImgNb]=$url; $QueryImgThumb[$QueryImgNb]=$phppath.$dest2; $QueryImgRel[$QueryImgNb]=1; $QueryImgNb++; } //ad the image passed by file upload if ($NameImgUpload!="") { $NameImgUploadLoc= $phppath.$desti1; $QueryRequest .= "\n"; $QueryImg[$QueryImgNb]=$NameImgUploadLoc; $QueryImgThumb[$QueryImgNb]=$phppath.$desti2; $QueryImgRel[$QueryImgNb]=1; $QueryImgNb++; } $QueryRequest .= " "; //Display QueryImg ShowQueryImg($QueryImgNb,$QueryImg,$QueryImgThumb,$QueryImgRel); echo "

QueryRequest: $QueryRequest


"; echo "

Result:

"; //Make the request $socket=connection($server,$port); $answer=request($socket,$QueryRequest); fclose($socket); //Create a xml parser, Parse the answer and free the parser $MRML_parser = xml_parser_create(); xml_set_element_handler($MRML_parser, "MRMLstart", "MRMLend"); xml_parse($MRML_parser, $answer); xml_parser_free($MRML_parser); } //get info form server function GetInfo() { global $xml,$mrmldtd,$allowUpload; global $server,$port,$name; //simple mrml request $GetInfo= $xml . $mrmldtd . " "; //Make the first request $socket=connection($server,$port); $answer=request($socket,$GetInfo); fclose($socket); //Create a xml parser, Parse the answer and free the parser $MRML_parser = xml_parser_create(); xml_set_element_handler($MRML_parser, "MRMLstart", "MRMLend"); xml_parse($MRML_parser, $answer); xml_parser_free($MRML_parser); //show collections, algorithms and ... echo "
Do not save this page (see why)


"; ShowCollection(); echo "
"; ShowAlgorithm(); echo "
"; ShowNumberReturn(); echo "
"; echo "Algorithm options:
"; ShowOptionAlgorithm(); if ($allowUpload == "yes") ShowAddImage(); else echo "

Image uploading feature disabled"; ShowButtons(); } //***********PARSING FUNCTIONS************ function MRMLstart($parser, $name, $attrs) { global $Collection,$NbCollection; global $Algorithm,$NbAlgorithm; global $Property,$NbProperty,$NbPropertyAlgo; global $SessionId,$Submit,$Depth,$Parent; //parse MRML tag switch ($name) { case "ACKNOWLEDGE-SESSION-OP": //Get session id if ($SessionId=="") { $SessionId=$attrs["SESSION-ID"]; } echo ""; break; case "COLLECTION": //Get Collection $NbCollection++; while (list ($key, $val) = each ($attrs)) { $Collection[$NbCollection][$key]=$val; } break; case "ALGORITHM": //Get alorithm if ($attrs["ALGORITHM-TYPE"]!="") { $NbProperty=-1; $Depth=0; $NbAlgorithm++; while (list ($key, $val) = each ($attrs)) { $Algorithm[$NbAlgorithm][$key]=$val; } } break; case "PROPERTY-SHEET": //Get alorithm property if ($NbAlgorithm==-1) {$NbAlgorithm=0;} $NbProperty++; $NbPropertyAlgo[$NbAlgorithm]=$NbProperty; while (list ($key, $val) = each ($attrs)) { $Property[$NbAlgorithm][$NbProperty][$key]=$val; } //save Depth and parent from the property $Property[$NbAlgorithm][$NbProperty]["DEPTH"]=$Depth; $Property[$NbAlgorithm][$NbProperty]["PARENT"]=$Parent[$Depth]; //increase Depth $Depth++; $Parent[$Depth]=$NbProperty; break; case "QUERY-RESULT-ELEMENT": //get image info global $imageloc,$imagerel,$nbimage,$QueryImgRelLoc; $ImgLoc=$attrs["IMAGE-LOCATION"]; $ImageThumb=$attrs["THUMBNAIL-LOCATION"]; $Similarity=$attrs["CALCULATED-SIMILARITY"]; //echo "ImgLoc: -$ImgLoc- , ImageThumb: -$ImageThumb-
"; if ($Submit=="Random") {ShowImage($ImgLoc,$ImageThumb,$Similarity,0);} if ($Submit=="Query" && $QueryImgRelLoc[$ImgLoc]=="") {ShowImage($ImgLoc,$ImageThumb,$Similarity,0);} if ($Submit=="Query" && $QueryImgRelLoc[$ImgLoc]!="") {ShowImage($ImgLoc,$ImageThumb,$Similarity,1);} break; } } function MRMLend($parser, $name) { global $Depth; switch ($name) { case "PROPERTY-SHEET": //decrease depth $Depth--; break; } } //***********DISPLAY FUNCTIONS************ //show the query images function ShowQueryImg($QueryImgNb,$QueryImg,$QueryImgThumb,$QueryImgRel) { //global variable global $SERVER_NAME,$DOCUMENT_ROOT,$ImgWidth,$ImgHeight; $tr=0; if ($QueryImgNb>0) { echo "

Query:

"; for ($i=0;$i<$QueryImgNb;$i++) { if ($tr%5==0) {echo "";} //get name of the image from his url //and use it for the "alt" tag $extmp=explode('/',$QueryImg[$i]); $alt=$extmp[count($extmp)-1]; //get real path of the image location $temp=substr($QueryImgThumb[$i], strlen($SERVER_NAME)+7, strlen($QueryImgThumb[$i])); //get the size of this image $size=@GetImageSize($DOCUMENT_ROOT . $temp); //calculate the right proportion for the reduce image heigh and image width if ($size[0]!="") { if ($size[0]>=$size[1]) { $TmpHeight=@(($size[1]*$ImgWidth)/$size[0]); $TmpWidth=$ImgWidth; } else { $TmpWidth=@(($size[0]*$ImgHeight)/$size[1]); $TmpHeight=$ImgHeight; } } else { $TmpHeight=$ImgHeight; $TmpWidth=$ImgWidth; } //display image and some info echo ""; $tr++; if ($i==$QueryImgNb-1) {$tr=5;} if ($tr%5==0) {echo "";} } echo "
>"; $name="image_".$i; echo ""; $name="thumb_img_".$i; echo ""; $name="rel_img_".$i; echo "
"; } } //same function but for result iamge function ShowImage($ImageLoc,$ImageThumb,$Similarity,$Rel) { global $CollectionId,$QueryImgNb,$DOCUMENT_ROOT,$SERVER_NAME; global $Return,$ImgWidth,$ImgHeight; static $nbimage=0; static $first=true; static $tmp=0; //get name of the image form his url //and use it for the "alt" tag $extmp=explode('/',$ImageLoc); $alt=$extmp[count($extmp)-1]; if ($first) {$tmp=$QueryImgNb;$first=false;} if ($nbimage==0) {echo "";} if ($nbimage%5==0) {echo "";} //get real path of the image location $temp=substr($ImageThumb, strlen($SERVER_NAME)+7, strlen($ImageThumb)); //get the size of this image $size=@GetImageSize($DOCUMENT_ROOT . $temp); if ($size[0]>=$size[1]) { $TmpHeight=@(($size[1]*$ImgWidth)/$size[0]); $TmpWidth=$ImgWidth; } else { $TmpWidth=@(($size[0]*$ImgHeight)/$size[1]); $TmpHeight=$ImgHeight; } //display image and some info echo ''; $nbimage++; if ($nbimage%5==0) {echo '';} if ($nbimage==$Return) {echo "
'; if ($Rel==1) { echo ""; echo "
"; echo "
Similarity: $Similarity
Query Image
"; } else { echo "
\"$alt\""; echo "
Similarity: $Similarity"; $name="image_".$tmp; echo ""; $name="thumb_img_".$tmp; echo ""; $name="rel_img_".$tmp; echo "
"; $tmp++; } echo '
top
";} } //show collection function ShowCollection(){ global $Collection,$NbCollection,$CollectionId; echo "Collection (details): "; //get collection name and collection ID //and show all collections if ($CollectionId!="") { for ($i=0;$i<=$NbCollection;$i++){ if ($Collection[$i]["COLLECTION-ID"]==$CollectionId) { $CollectionName=$Collection[$i]["COLLECTION-NAME"]; echo ""; } } echo '';} for ($i=0;$i<=$NbCollection;$i++){ $value=$Collection[$i]["COLLECTION-ID"]; $name=$Collection[$i]["COLLECTION-NAME"]; if ($CollectionId!=$value) {echo "";} } echo ' '; } //Display algorithm function ShowAlgorithm(){ global $Algorithm,$NbAlgorithm,$AlgorithmId; echo "Algorithm: "; //get algorithm name and algorithm ID //and show all algorithms if ($AlgorithmId!="") { for ($i=0;$i<=$NbAlgorithm;$i++){ if ($Algorithm[$i]["ALGORITHM-ID"]==$AlgorithmId) { $AlgorithmName=$Algorithm[$i]["ALGORITHM-NAME"]; echo ""; } } echo '';} for ($i=0;$i<=$NbAlgorithm;$i++){ $value=$Algorithm[$i]["ALGORITHM-ID"]; $name=$Algorithm[$i]["ALGORITHM-NAME"]; if ($AlgorithmId!=$value) {echo "";} } echo ' '; } // show property of algorithm function ShowOptionAlgorithm($Depth=0,$Parent="",$visibility="visible",$IdLayer=0){ global $Algorithm,$NbAlgorithm,$AlgorithmId; global $Property,$NbProperty,$NbPropertyAlgo,$OldAlgorithm; static $Option=0; //get algorithmID if ($AlgorithmId!="") { for ($i=0;$i<=$NbAlgorithm;$i++){ if ($Algorithm[$i]["ALGORITHM-ID"]==$AlgorithmId) {$algonum=$i;} } } else {$algonum=0;} $j=0; $InLayer=""; //for each property of the algorithm for ($i=0;$i<=$NbPropertyAlgo[$algonum];$i++){ //if depth is the actual Depth and parent are the actual parent //(mean that they are in the same "group") if ($Property[$algonum][$i]["DEPTH"]==$Depth && $Property[$algonum][$i]["PARENT"]==$Parent) { //switch the different type of property switch ($Property[$algonum][$i]["PROPERTY-SHEET-TYPE"]) { //in case of subset, go to the next depth //and set the subset as parent case "subset": $tempDepth[$j]=$Depth+1; $tempParent[$j]=$i; $j++; $visibility2=$visibility; break; //in case of pannel, go to the next depth //and set the pannel as parent case "pannel": $tempDepth[$j]=$Depth+1; $tempParent[$j]=$i; $j++; $visibility2=$visibility; break; //in case of set-element... case "set-element": $option="AlgoOption".$Option; global $$option; if ($OldAlgorithm!=$algonum) $$option=""; //get property's name $name=$Property[$algonum][$i]["CAPTION"]; $InLayer.="$name: "; $sendname=$Property[$algonum][$i]["SEND-NAME"]; $InLayer.= ""; //if the property value is yes if ($$option=="yes" || ($Property[$algonum][$i]["SEND-VALUE"]=="yes" && $$option=="") ) { //save the next depth and set the set-element as parent $tempDepth[$j]=$Depth+1; $tempParent[$j]=$i; $j++; $visibility2="visible"; //If the property have children //add the option to show/hide children if ($Property[$algonum][$i+1]["PARENT"]==$i) { $InLayer.= ""; } $InLayer.= ""; $InLayer.= ""; $InLayer.=' '; } //if the property value is no //make nearly the same as yes else { if ($$option=="" || $$option=="no" || ($Property[$algonum][$i]["SEND-VALUE"]=="no" && $$option=="")) { $visibility2="visible"; //If the property have children //add the option to show/hide children if ($Property[$algonum][$i+1]["PARENT"]==$i) { $visibility2="hidden"; $InLayer.= ""; } $InLayer.=""; $InLayer.= ""; $InLayer.= ''; } } //go to the next property $Option++; break; //in case of numeric property case "numeric": $option="AlgoOption".$Option; global $$option; //get the value $InLayer.= ""; $sendname=$Property[$algonum][$i]["SEND-NAME"]; $InLayer.= ""; if ($$option=="" || $OldAlgorithm!=$algonum) {$value=$Property[$algonum][$i]["SEND-VALUE"];} else { $optionb="AlgoOptionb".$Option; global $$optionb; $value=$$optionb; } //get name, from , to and step $name=$Property[$algonum][$i]["CAPTION"]; $InLayer.= "$name: "; $from=$Property[$algonum][$i]["FROM"]; $to=$Property[$algonum][$i]["TO"]; $step=$Property[$algonum][$i]["STEP"]; //display property $InLayer.= " "; $Option++; break; } } } //Display property if ($InLayer!="") { echo $InLayer."
"; $IdLayer++; } //make recursion for ($i=0;$i<$j;$i++) ShowOptionAlgorithm($tempDepth[$i],$tempParent[$i], $visibility2,$IdLayer); } //display the number of images the script must return function ShowNumberReturn(){ global $Return,$NbReturnImages; echo "Return "; echo " images

"; } //diplay two text input for file upload and url image function ShowAddImage() { echo "
Url of a relevant image:"; echo ""; echo '
Other relevant image: '; } //show tree buttons: Random, Query and Clear function ShowButtons() { global $server,$port,$name; echo '

(fetch a random set of images)
( launch the query)
(Clear the query)

'; echo ""; echo ""; echo ""; } //asking for server and port function AskConnection () { echo 'Connection with a GIFT server

'; echo '(You may like to start with this tutorial)

'; echo '
Server Name:
Server Port:
User Name:
By connecting to this service, you implicitly affirm that you have read and accepted our disclaimer



'; } //**********Other Function******************* //create a directory function CreateDir($dir) { if (!is_dir($dir)){ $oldumask = umask(0); mkdir ($dir,0777); umask($oldumask); } } //check if is $name have valid image extension function CheckExtension($name) { global $ImagesExtension; //get image Extension $temp=explode(".",$name); $n=count($temp); $extension=strtolower(trim($temp[$n-1])); $type=explode(",",$ImagesExtension); //check if it's a valid one $valid=false; for ($i=0;$i

error: $name
bad extension: $extension
this script supports: $ImagesExtension


"); } } //******************MAIN******************* //Form echo "
"; //in case of algorithm change, make the same request as before echo ""; if ($AlgorithmId!="" && $Submit=="") { $Submit=$OldSubmit; echo ""; } //create a directory CreateDir($ImagePath); echo ""; break; case "Query": GetInfo(); echo ""; break; } echo "
"; switch ($Submit) { case "": AskConnection(); break; case "Accept and Connect": GetInfo(); break; case "Clear": GetInfo(); break; case "Random": GetInfo(); echo " "; MakeRandom(); echo " "; MakeQuery(); echo "
"; echo '
'; /******************END OF PHP******************/ ?>