Changes between Version 15 and Version 16 of Printing

Show
Ignore:
Timestamp:
09/29/09 12:21:48 (4 years ago)
Author:
hostgis
Comment:

updated the PHP tile-stitching stuff to fix bugs

Legend:

Unmodified
Added
Removed
Modified
  • Printing

    v15 v16  
    6666 
    6767{{{ 
    68   // printMap() assumes that an iframe named 'map' contains a Map object named 'map' 
    69   // It should be very easy to change those first two lines to accommodate your situation. 
    70   function printMap() { 
    71     var mapview  = document.getElementById('map').contentWindow.map; 
    72     var printurl = 'print.php'; 
    73     var size     = mapview.getSize(); 
     68// this assumes that the Map object is a JavaScript variable named "map" 
     69// changed 20090928: 
     70// * fixed the weird references about iframes, since they're not the usual use case 
     71// * added the Please Wait window 
     72// * used newer-style OpenLayers.Request.POST() for final AJAX call 
     73var print_wait_win = null; 
     74function PrintMap() { 
     75    //-- post a wait message 
     76    print_wait_win = window.open("pleasewait.html", "print_wait_win", "scrollbars=no, status=0, height=5, width=10, resizable=1"); 
     77 
    7478    // go through all layers, and collect a list of objects 
    7579    // each object is a tile's URL and the tile's pixel location relative to the viewport 
     80    var size  = map.getSize(); 
    7681    var tiles = []; 
    77     for (layername in mapview.layers) { 
    78       // if the layer isn't visible at this range, or is turned off, skip it 
    79       var layer = mapview.layers[layername]; 
    80       if (!layer.getVisibility()) continue; 
    81       if (!layer.calculateInRange()) continue; 
    82       // iterate through their grid's tiles, collecting each tile's extent and pixel location at this moment 
    83       for (tilerow in layer.grid) { 
    84       for (tilei in layer.grid[tilerow]) { 
    85         var tile     = layer.grid[tilerow][tilei] 
    86         var url      = layer.getURL(tile.bounds); 
    87         var position = tile.position; 
    88         var opacity  = layer.opacity ? parseInt(100*layer.opacity) : 100; 
    89         tiles[tiles.length] = {url:url, x:position.x, y:position.y, opacity:opacity}; 
    90       } 
    91       } 
     82    for (layername in map.layers) { 
     83        // if the layer isn't visible at this range, or is turned off, skip it 
     84        var layer = map.layers[layername]; 
     85        if (!layer.getVisibility()) continue; 
     86        if (!layer.calculateInRange()) continue; 
     87        // iterate through their grid's tiles, collecting each tile's extent and pixel location at this moment 
     88        for (tilerow in layer.grid) { 
     89            for (tilei in layer.grid[tilerow]) { 
     90                var tile     = layer.grid[tilerow][tilei] 
     91                var url      = layer.getURL(tile.bounds); 
     92                var position = tile.position; 
     93                var opacity  = layer.opacity ? parseInt(100*layer.opacity) : 100; 
     94                tiles[tiles.length] = {url:url, x:position.x, y:position.y, opacity:opacity}; 
     95            } 
     96        } 
    9297    } 
     98 
    9399    // hand off the list to our server-side script, which will do the heavy lifting 
    94100    var tiles_json = JSON.stringify(tiles); 
    95101    var printparams = 'width='+size.w + '&height='+size.h + '&tiles='+escape(tiles_json) ; 
    96     new OpenLayers.Ajax.Request(printurl, {method:'post', parameters:printparams, onComplete:printDone }); 
    97     // if using OpenLayers 2.7 or later use this request (printDone function not necessary): 
    98     // OpenLayers.Request.POST({url:printurl, data:OpenLayers.Util.getParameterString({width:size.w,  
    99     // height:size.h, tiles:tiles_json}), headers:{"Content-Type":"application/x-www-form-urlencoded"}, 
    100     // callback: function(request) {window.open(request.responseText);}}); 
    101   } 
    102  
    103   // when the request is ready (the image has been generated) this callback expects the output 
    104   // to have been simply an URL indicating where to find the generated document. 
    105   function printDone(request) { 
    106      var url = request.responseText; 
    107      window.open(url); 
    108   } 
     102    OpenLayers.Request.POST( 
     103      { url:'lib/print.php', 
     104        data:OpenLayers.Util.getParameterString({width:size.w,height:size.h,tiles:tiles_json}), 
     105        headers:{'Content-Type':'application/x-www-form-urlencoded'}, 
     106        callback: function(request) { 
     107           print_wait_win.close(); 
     108           window.open(request.responseText); 
     109        } 
     110      } 
     111    ); 
     112} 
    109113}}} 
    110114 
    111  
    112115Server-side code for print.php. This requires that PHP have been compiled with GD support, which is the default for virtually all OSs. json_decode requires PHP 5.2.0 or later. 
    113  
    114116{{{ 
    115117  <?php 
     118  $TEMP_DIR = '/var/www/htdocs/tmp/'; 
     119  $TEMP_URL = '/tmp/'; 
    116120  // fetch the request params, and generate the name of the tempfile and its URL 
    117121  $width    = @$_REQUEST['width'];  if (!$width) $width = 1024; 
     
    120124  //$tiles    = json_decode(stripslashes(@$_REQUEST['tiles'])); // use this if you use magic_quotes_gpc 
    121125  $random   = md5(microtime().mt_rand()); 
    122   $file     = sprintf("%s/%s.jpg", TEMP_DIR, $random ); 
    123   $url      = sprintf("%s/%s.jpg", TEMP_URL, $random ); 
     126  $file     = sprintf("%s/%s.jpg", $TEMP_DIR, $random ); 
     127  $url      = sprintf("%s/%s.jpg", $TEMP_URL, $random ); 
    124128 
    125129  // lay down an image canvas