Skip to content

Instantly share code, notes, and snippets.

@bartvde
Created May 3, 2016 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartvde/efa701f7e420d5852cd00d2e9f47de24 to your computer and use it in GitHub Desktop.
Save bartvde/efa701f7e420d5852cd00d2e9f47de24 to your computer and use it in GitHub Desktop.
<?php
function createLegendGraphicRequest ($oLayer, $nScale, $nWidth=18, $nHeight=12)
{
/* required parameters: version, request, layer
* uses width, height, version, sld_body, scale
* sld,style,featuretype,rule,exceptions not used
* format is png
*/
if ( !($sName = $oLayer->getmetadata("wms_name")) ) $sName = $oLayer->name;
$szSLDBody = urlencode( $oLayer->getMetaData("wms_sld_body") );
if ( trim($szSLDBody) != "" ) { $szSLDBody = "&SLD_BODY=".$szSLDBody; }
$szSLDURL = urlencode( $oLayer->getMetaData("wms_sld_url") );
if (trim($szSLDURL) == "" )
{
$szWMSStyle = $oLayer->getMetaData("wms_style");
$szSLDURL = $oLayer->getMetaData("wms_style_".$szWMSStyle."_sld");
}
if ( trim($szSLDURL) != "" ) { $szSLDURL = "&SLD=".$szSLDURL; }
// bartvde, dit anders oplossen, waarschijnlijk kijken naar wms_sld_body
//$sSLDBody = urlencode($oLayer->generateSLD());
//$request = trim($oLayer->connection)."&request=getlegendgraphic&scale=".$nScale."&width=".$nWidth."&height=".$nHeight;
$request = trim($oLayer->connection)."&request=GetLegendGraphic&scale=".$nScale;
$request .= "&version=1.1.1&layer=".$sName."&format=image/png".$szSLDBody.$szSLDURL;
return $request;
}
function getTitle ($oLayer)
{
// returns title for layer
if ($title = $oLayer->getmetadata("wms_title")) return $title;
else return $oLayer->name;
}
function createNewImage ($nWidth, $nHeight)
{
// returns new blank image with size nWidth x nHeight
$img = imagecreatetruecolor ($nWidth,$nHeight);
$background = ImageColorAllocate ($img, 255, 255, 255);
imagefill($img,0,0,$background);
return ($img);
}
function createLegendImage ( $oMapSession, $aLayers=null, $nMaxHeight=1000, $nIconWidth=18, $nIconHeight=12, $nSpacing=5 )
{
// output: image containing all the legendimages from getlegendgraphic-requests
// input:
// oMapSession is the map session ;-)
// nMaxHeight is the max. height of the outputimage, this is only a HINT,
// if one of the getlegendgraphics is larger, the output image will be larger than nMaxHeight
// nIconWidth, nIconHeight are used in the getlegendgraphic-request
// nSpacing is the space between legendimages
$oMap = $oMapSession->oMap;
$aCurrentSize = array(0,0,0,0,0); // topleft X,Y, bottomright X,Y, total imageheight
$aOrder = $oMap->getlayersdrawingorder();
$nFontType = $_SESSION['gszCorePath']."/../etc/Vera.ttf";
$aDontShow = array("ExpressionBuilderAnnotation", "LabelAnnotation", "MapNotesAnnotation");
for ($x=count($aOrder); $x>0; $x--)
{
$oLayer = $oMap->getlayer($aOrder[$x-1]);
if ( ($oLayer->status == MS_ON)
&& (!$aLayers || in_array($oLayer->name,$aLayers) )
&& (!in_array($oLayer->name,$aDontShow)) )
{
if ($oLayer->connectiontype == MS_WMS)
{
include_once(COMMON."/http/HTTPClient.php");
$oHTTPClient = new HTTPClient("", 30);
$legendurl = $oLayer->getmetadata("wms_style_default_legendurl");
if ($legendurl != "")
{
$aStyle = explode(" ", $legendurl);
if (is_array($aStyle) && isset($aStyle[3]) && $aStyle[3] != "")
{
$sRequest = $aStyle[3];
$mimeType = $aStyle[2];
// overrule naar png ivm Ionic bug
// http://support.ionicsoft.com/index.php?pid=viewticket&id=7CEA3D
if ($mimeType == 'application/octet_stream') $mimeType = 'image/png';
}
else
{
$sRequest = createLegendGraphicRequest($oLayer, $oMap->scale, $nIconWidth, $nIconHeight);
$mimeType = 'image/png';
}
}
else
{
$sRequest = createLegendGraphicRequest($oLayer, $oMap->scale, $nIconWidth, $nIconHeight);
$mimeType = 'image/png';
}
if ($mimeType == 'image/png') $szFileExt = 'png';
if ($mimeType == 'image/gif') $szFileExt = 'gif';
$szFilename = tempnam($_SESSION['gszTmpPath'], "GetLegendGraphic".".".$szFileExt);
$fpOut = fopen($szFilename, 'w');
$oHTTPClient->doGET($sRequest, $fpOut);
//if ($img = @imagecreatefrompng($sRequest))
if ($mimeType == 'image/png')
{
$img = @imagecreatefrompng($szFilename);
}
else if ($mimeType == 'image/gif')
{
$img = @imagecreatefromgif($szFilename);
}
if ($img)
{
$sTitle = getTitle($oLayer);
// set size titletext
$aImageText = imagettfbbox ( 10, 0, $nFontType, $sTitle );
$nTextWidth = $aImageText[2] - $aImageText[0]+$nSpacing;
// $nTextHeight = $aImageText[1] - $aImageText[7];
$nTextHeight = 18; // font is always the same, so just set this fixed
// set Y (height)
$aCurrentSize[1] = $aCurrentSize[3];
$aCurrentSize[3] += ($nSizeY = imagesy($img)) + $nTextHeight + $nSpacing*2;
if ($aCurrentSize[3] > $nMaxHeight)
{
if (($nSizeY+$nTextHeight+$nSpacing*2) > $nMaxHeight) $nMaxHeight = $nSizeY+$nTextHeight+$nSpacing*3;
$aCurrentSize[0] = $aCurrentSize[2]+$nSpacing;
$aCurrentSize[1] = 0;//$nSpacing;
$aCurrentSize[2] += $nSpacing;
$aCurrentSize[3] = $nSizeY + $nTextHeight + $nSpacing*2;
$aCurrentSize[4] = $nMaxHeight;
}
else if ($aCurrentSize[4] < $nMaxHeight) $aCurrentSize[4] = $aCurrentSize[3];
// set X (width)
if ( ($nTextWidth)+($nSpacing /* *2 */ ) > $aCurrentSize[2]-$aCurrentSize[0] )
$aCurrentSize[2] = $aCurrentSize[0] + $nTextWidth + $nSpacing;
if ( ( $nSizeX = imagesx($img) )+($nSpacing*2 ) > $aCurrentSize[2]-$aCurrentSize[0] )
$aCurrentSize[2] = $aCurrentSize[0] + $nSizeX + $nSpacing*2;
// create new image
// bartvde, 23-01-06, clean up first, otherwise lots of mem consumption!
if ($leg_img) { imagedestroy($leg_img); }
$leg_img = createNewImage($aCurrentSize[2],$aCurrentSize[4]);
// get 'old' image and copy into new
if ($old_img)
{
imagecopy($leg_img,$old_img ,0,0, 0,0 ,imagesx($old_img), imagesy($old_img));
imagedestroy($old_img);
}
// copy current layer into image
$black = ImageColorAllocate ($leg_img, 0, 0, 0);
ImageTTFText ($leg_img, 10, 0, $aCurrentSize[0]+$nSpacing, $aCurrentSize[1]+$nSpacing+$nTextHeight, $black, $nFontType, $sTitle);
imagecopy($leg_img, $img, $aCurrentSize[0]+$nSpacing, $aCurrentSize[1]+$nTextHeight+$nSpacing*2 , 0, 0, $nSizeX ,$nSizeY);
imagedestroy($img);
$old_img = createNewImage($aCurrentSize[2],$aCurrentSize[4]);
imagecopy($old_img,$leg_img,0,0,0,0,$aCurrentSize[2],$aCurrentSize[4]);
} // else geen img (geen ondersteuning getlegendgraphic?)
} // else geen wms (toch titel tonen?)
} // else niet tonen
}
return ($leg_img);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment