Skip to content

Instantly share code, notes, and snippets.

@ulimueller
Created April 5, 2011 06:41
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 ulimueller/903135 to your computer and use it in GitHub Desktop.
Save ulimueller/903135 to your computer and use it in GitHub Desktop.
<?php
/**
* Provides an http interface to wkhtmltopdf / wkhtmltoimage.
*
* We use this code to get a combined image of all map layers and
* tiles of an OpenLayers page
*
* In order for wkhtmltoimage to capture the right extent your
* OpenLayers map must have a fixed size.
*
*
* @copyright 2011 geOps
* @license http://www.geops.de/license.txt http://www.zend.com/license/3_0.txt PHP License 3.0
* @version $Id$
* @link http://www.geops.de
* @package
* @authors nico mandery, uli mueller
*/
$MAP_URL = "http://fledermaus6.geops.de/print";
$WKHTMLTOPDF = "/opt/install/wkhtmltoimage-amd64";
$TMPDIR = "/tmp/";
// defaults
$params = array(
"zoom" => 6,
"lat" => 6621292.72182,
"lon" => 1001876.417,
"layers" => "0BFTTTT",
"format" => "jpg",
"width" => 400,
"height" => 400
);
// fetch numeric params
foreach (array('zoom', 'lat', 'lon', 'width', 'height') as $p) {
if (isset($_GET[$p]) && is_numeric($_GET[$p])) {
$params[$p] = $_GET[$p];
}
}
if (isset($_GET['format'])) {
if (in_array($_GET['format'], array('png', 'jpg'))) {
$params["format"] = $_GET['format'];
}
}
if (!empty($_GET['layers'])) {
$params['layers'] = $_GET['layers'];
}
// build the query string for the map fetching
$get_params = array();
foreach(array('zoom', 'lat', 'lon', 'layers') as $p) {
$get_params[$p] = $params[$p];
}
$query = $MAP_URL . "?" . http_build_query($get_params);
// create temp file
$tmpfile = tempnam($TMPDIR, 'wkpdf');
@unlink($tmpfile);
// build the command
$cmd = $WKHTMLTOPDF . " " ;
$cmd .= "--height " . $params['height'] . " ";
$cmd .= "--width " . $params['width'] . " ";
$cmd .= "-f " . $params['format'] . " ";
$cmd .= "\"" . $query . "\" ";
$cmd .= "\"".$tmpfile."\"";
system($cmd);
header("Content-Type: image/".$params['format']);
@readfile($tmpfile);
@unlink($tmpfile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment