I am trying to get an image of my OpenLayers application. I am using this http://www.mapsportal.org/olprint/ and http://trac.osgeo.org/openlayers/wiki/TileStitchingPrinting as a guide.

When I click my print button I get a print failed message and when I go to the TMP folder (c:\wamp\www\tmp) there are IMG files and a JPEG but the JPEG is all black.

  • I am using PHP on the server
    – GreyHippo
    Jun 19, 2014 at 12:28
  • I can see that the JSON tile array is correct. It is downloading the tiles but when it assembles them together something goes awry. The funny thing is it says printing failed but I can see that it has created the JPEG file.
    – GreyHippo
    Jun 19, 2014 at 17:04

1 Answer 1


I've modified the provided script as follows (imagecopymerge_alpha function). It seems to be working but I keep looking for a better way to handle transparency.

$TEMP_DIR = dirname(__FILE__) . '/tmp';
$TEMP_URL = '/tmp';

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity){
    $w = imagesx($src_im);
    $h = imagesy($src_im);
    imagecopymerge($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity);

// fetch the request params, and generate the name of the tempfile and its URL
$width    = @$_REQUEST['width'];  if (!$width) $width = 1024;
$height   = @$_REQUEST['height']; if (!$height) $height = 768;
$tiles    = json_decode(@$_REQUEST['tiles']);
//$tiles    = json_decode(stripslashes(@$_REQUEST['tiles'])); // use this if you use magic_quotes_gpc
$random   = md5(microtime().mt_rand());
$file     = sprintf("%s/%s.jpg", $TEMP_DIR, $random );
$url      = sprintf("%s/%s.jpg", $TEMP_URL, $random );

// lay down an image canvas
// Notice: in MapServer if you have set a background color
// (eg. IMAGECOLOR 60 100 145) that color is your transparent value
// $transparent = imagecolorallocatealpha($image,60,100,145,127);
$image = imagecreatetruecolor($width,$height);
imagefill($image,0,0, imagecolorallocate($image,255,255,255) ); // fill with white

// loop through the tiles, blitting each one onto the canvas
foreach ($tiles as $tile) {

    // try to convert relative URLs into full URLs
    // this could probably use some improvement
    $tile->url = urldecode($tile->url);
    if (substr($tile->url,0,4)!=='http') {
        $tile->url = preg_replace('/^\.\//',dirname($_SERVER['REQUEST_URI']).'/',$tile->url);
        $tile->url = preg_replace('/^\.\.\//',dirname($_SERVER['REQUEST_URI']).'/../',$tile->url);
        $tile->url = sprintf("%s://%s:%d/%s", isset($_SERVER['HTTPS'])?'https':'http', $_SERVER['SERVER_ADDR'], $_SERVER['SERVER_PORT'], $tile->url);
    $tile->url = str_replace(' ','+',$tile->url);

    // fetch the tile into a temp file, and analyze its type; bail if it's invalid
    $tempfile =  sprintf("%s/%s.img", $TEMP_DIR, md5(microtime().mt_rand()) );
    list($tilewidth,$tileheight,$tileformat) = @getimagesize($tempfile);
    if (!$tileformat) continue;

    // load the tempfile's image, and blit it onto the canvas
    switch ($tileformat) {
        case IMAGETYPE_GIF:
        $tileimage = imagecreatefromgif($tempfile);
        case IMAGETYPE_JPEG:
        $tileimage = imagecreatefromjpeg($tempfile);
        case IMAGETYPE_PNG:
        $tileimage = imagecreatefrompng($tempfile);

    imagecopymerge_alpha($image, $tileimage, $tile->x, $tile->y, 0, 0, $tilewidth, $tileheight, $tile->opacity);

// save to disk and tell the client where they can pick it up
print $url;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.