[Mapserver-users] php mapscript - pasteimage

Daniel Morissette dmorissette at dmsolutions.ca
Mon May 3 12:36:17 EDT 2004

This is a multi-part message in MIME format.
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

publiek at home.nl wrote:
> I want to show several layers on top of each other.
> How do i combine the images?

Just calling $layer->draw($image) for each layer will do the trick.  You 
need to use $map->prepareImage() to create a balnk image first.

> I looked at pasteimage, but the examples provided with mapserver 4 dont work. (Would be nice if someone updated these)
> Anyone willing to provide me a simple example?

Thanks for pointing that. I have fixed the 
mapscript/php3/examples/test_layer_mask.phtml forV4.x and updated the 
copies in CVS for v4.2 and 4.3. I also attached a copy of the updated 

  Daniel Morissette               dmorissette at dmsolutions.ca
  DM Solutions Group              http://www.dmsolutions.ca/

Content-Type: text/html;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;


<BODY BGCOLOR="#888888">


<H1>PHP/MapScript layer mask test</H1>
// This example shows how to create an opaque mask layer using shapes coming
// from a map layer, and using the imageObj's pasteImage() method.
// This example uses the GMap demo's mapfile and data.

// Load MapScript module.
if (PHP_OS == "WINNT" || PHP_OS == "WIN32")

// Load MapServer .map file
// You can download the GMap demo at http://www2.dmsolutions.ca/mapserver/dl/
$map = ms_newMapObj("gmap75.map");

// Note: If you get errors with the saveWebImage() call below, then make sure
// that the directory specified by IMAGEPATH in the .MAP file exists and is
// writable by the httpd user.

// This works only with GIF or PNG

// Draw the base layers into an imageObj
$img1 = $map->prepareimage();
$layer = $map->getLayerByName("bathymetry");
$layer->set("status", 1);

//$img = $map->draw();
$url = $img1->saveWebImage();
echo "<P>Draw the base layers into an image...<p>\n";
printf("<IMG SRC=%s WIDTH=%d HEIGHT=%d>\n", $url, $map->width, $map->height);

// Use the parks layer as a mask... we only want to see the base layers
// through the area of the park polygons and the rest should be light grey.

// Start by creating a new image ... 
$img2 = $map->prepareimage();

// ... and we'll use a rectObj to draw the opaque mask background
$rect = ms_newRectObj();
$rect->setExtent(0, 0, $map->width, $map->height);

// We need to create a temporary layer and class to use in drawing the 
// filled rectangle that will be the opaque part of the mask.
$tmplayer = ms_newLayerObj($map);
$tmplayer->set("type", MS_LAYER_POLYGON);
$tmplayer->set("status", 1);
$tmplayer->set("transform", MS_FALSE);
$tmpclass = ms_newClassObj($tmplayer);
$tmpstyle = ms_newStyleObj($tmpclass);
$rect->draw($map, $tmplayer, $img2, 0, "");  // Draw rect with class 0

// Now fetch the park layer and change its only class to use color index 0
// which is the transparent background color... so the parks polygons will
// create transparent holes in the mask

$layer = $map->getLayerByName("park");
$layer->set("status", 1);

$class = $layer->getClass(0);
$style = $class->getStyle(0);


$url = $img2->saveWebImage();
echo "<P>Draw the mask into another image...<p>\n";
printf("<IMG SRC=%s WIDTH=%d HEIGHT=%d>\n", $url, $map->width, $map->height);

// Last step... combine the two images using pasteImage()
echo "<P>And combine the two with pasteImage()... you see the base layer through the holes in the mask...<p>\n";
$img1->pasteImage($img2, 0x0c0c0c);
$url = $img1->saveWebImage();
printf("<IMG SRC=%s WIDTH=%d HEIGHT=%d>\n", $url, $map->width, $map->height);



More information about the mapserver-users mailing list