$tmp=explode('/',$phpurl);
//$phpname=$tmp[count($tmp)-1];
$phpname=Client.php;
$phppath=substr($phpurl,0,strlen($phpurl)-strlen($phpname));
// Do you want to allow image upload? ("yes" or "no")
$allowUpload="yes";
//$allowUpload="no";
//Images Sizes
$ImgHeight=120;
$ImgWidth=120;
//Nb Return Images
$NbReturnImages="10,20,50,100,140";
//xml version
$xml='';
//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 "
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 "
";
}
}
//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 '
";}
}
//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 '