load($template_path.$template);
$quality = (float)$_REQUEST['quality'];
$preserveScale = $_REQUEST['scale'];
$print_info = json_decode(urldecode($_REQUEST['layers']), true);
# Add some bounds...
if($quality <= 0) { $quality = 1; }
if($quality > 3) { $quality = 3; }
if( $mode == "feature_report") {
$mapfile = getMapfile($mapbook, $_REQUEST['src']);
$mapObj = ms_newMapObj($CONFIGURATION['root'].$mapfile);
$path = explode('/', $_REQUEST['src']);
$layer = false;
if($path[1] == 'all') {
$layer = $mapObj->getLayer(0); # Don't use all...
} else {
$layer = $mapObj->getLayerByName($path[1]);
}
# get the query information and set it on the layer
$query_info = $template_info->getElementsByTagName('query')->item(0);
$qItem = $query_info->getAttribute('item');
$layer->set('filteritem', $qItem);
$layer->setFilter(str_replace('%qstring%', $_REQUEST[$qItem], $query_info->getAttribute('string')));
$shape = false;
$layer->set('template', 'dummy.html');
$layer->set('status', MS_DEFAULT);
$layer->queryByRect($mapObj->extent);
$layer->open();
$result = $layer->getResult(0);
$shape = $layer->getShape($result);
$wkt = $shape->toWkt();
$sketches_json[0]['wkt'] = $wkt;
$sketches_json[0]['stroke'] = "yellow";
$sketches_json[0]['opacity'] = ".8";
$extent = array();
$padding_percent = ((float)$shape->bounds->maxx - (float)$shape->bounds->minx) * $padding/100;
$extent[0] = (float)$shape->bounds->minx - $padding_percent;
$extent[1] = (float)$shape->bounds->miny - $padding_percent;
$extent[2] = (float)$shape->bounds->maxx + $padding_percent;
$extent[3] = (float)$shape->bounds->maxy + $padding_percent;
$layer->close();
}
# The image width is effected by the placement on the page and the print quality
$mapImageWidth = (float)$CONFIGURATION['html_image_width'] * $quality;
$mapImageHeight = (float)$CONFIGURATION['html_image_height'] * $quality;
$image_extent = preserveScale($preserveScale, $mapImageWidth, $mapImageHeight, $extent, $quality, $units);
#$debug = array('mw' => $mapImageWidth, 'mh' => $mapImageHeight, 'ext' => $image_extent, 'layers' => $layers_json, 'sketches' => $sketches_json);
#foreach($debug as $k => $v) {
# print $k;
# print ':';
# print json_encode($v);
# print '
';
#}
$printImage = renderImage($mapbook, $print_info, $mapImageWidth, $mapImageHeight, $image_extent, $DEBUG); #, $sketches_json, $vector_json);
imagejpeg($printImage, $tempDir.$uniqueId.'.jpg');
header("Content-type: application/xml");
print "";
print "
";
if($CONFIGURATION['print_image'] == 1) {
printf('View Image
', $uniqueId);
}
if($CONFIGURATION['print_html'] == 1) {
$lines = file('../../conf/'.$CONFIGURATION['html_template']);
$htmlOut = "";
foreach($lines as $line) {
$line = str_replace('%MAP%', sprintf('download.php?id=%s&ext=jpg', $uniqueId), $line);
$htmlOut = $htmlOut . $line;
}
foreach($_REQUEST as $k => $v) {
$htmlOut = str_replace('%'.$k.'%', urldecode(utf8_decode($v)), $htmlOut);
}
$out = fopen($tempDir.$uniqueId.'.html', 'w');
fwrite($out, $htmlOut);
fclose($out);
printf('View HTML
', $uniqueId);
}
if($CONFIGURATION['print_pdf'] == 1) {
# Open the PDF Template
$pdf = new FPDI();
# force everything to points.
$pdf->k = 1.0;
# load the template...
$page_info = $template_info->getElementsByTagName('page')->item(0);
$templateSize = array(((float)$page_info->getAttribute('w'))*72.0,((float)$page_info->getAttribute('h'))*72.0);
$template_file = $template_info->getElementsByTagName('template')->item(0)->firstChild->nodeValue;
$pdf->setSourceFile($template_path.$template_file);
$pdf->SetAutoPageBreak(false);
$pdf->addPage('P', $templateSize);
$tplidx = $pdf->importPage(1, '/MediaBox');
$pdf->useTemplate($tplidx, 0, 0, $templateSize[0], $templateSize[1]);
# Set the map location
$map_info = $template_info->getElementsByTagName('map')->item(0);
$imageW = round(((float)$map_info->getAttribute('w'))*72.0);
$imageH = round(((float)$map_info->getAttribute('h'))*72.0);
$imageX = round(((float)$map_info->getAttribute('x'))*72.0);
$imageY = round(((float)$map_info->getAttribute('y'))*72.0);
$pdf_extent = preserveScale($preserveScale, $imageW*$quality, $imageH*$quality, $extent, $quality, $units);
if($DEBUG) {
echo "Image W: ".$imageW."
";
echo "Image H: ".$imageH."
";
echo "Image X: ".$imageX."
";
echo "Image Y: ".$imageY."
";
echo "Quality: ".$quality."
";
echo "Extent: ".implode(',',$extent)."
";
echo "PDF Extent: ".implode(',',$pdf_extent)."
";
echo "
";
echo "Ground W: ".($pdf_extent[2] - $pdf_extent[0])."
";
echo "Ground H: ".($pdf_extent[3] - $pdf_extent[1])."
";
}
imagejpeg(renderImage($mapbook, $print_info, $imageW*$quality, $imageH*$quality, $pdf_extent), $tempDir.$uniqueId.'_pdf.jpg');
$pdf->Image($tempDir.$uniqueId.'_pdf.jpg', $imageX, $imageY, $imageW, $imageH);
# Render the text fields
$pdf->SetTextColor(0,0,0);
$texts = $template_info->getElementsByTagName('text');
for($i = 0; $i < $texts->length; $i++) {
$text = $texts->item($i);
# Set the font size
$pdf->SetFont('Helvetica', '', (float)$text->getAttribute('size'));
# Process the string
if($mode == "feature_report") {
$printString = $text->getAttribute('content').' '.$text->getAttribute('src');
foreach($shape->values as $k => $v) {
$printString = str_replace('%'.$k.'%', $v, $printString);
}
} else {
$printString = $text->getAttribute('content');
foreach($_REQUEST as $k => $v) {
//LK Check for hidden date field, if set to "true", add the date to the map
if($k == "date" && $v == "true"){
$v = "Printed " . date("m/d/Y") . " ";
}
$printString = str_replace('%'.$k.'%', urldecode(utf8_decode($v)), $printString);
}
}
# Put the string on the form
$pdf->SetXY(((float)$text->getAttribute('x'))*72.0, ((float)$text->getAttribute('y'))*72.0);
$pdf->Cell(0,.25,$printString);
}
$pdf->Output($tempDir.$uniqueId.'.pdf');
printf('Download PDF
', $uniqueId);
if($DEBUG) {
printf('Download PDF JPEG
', $uniqueId);
}
}
print "]]>";
exit(0);
# Get the extent with the scale preserved
function preserveScale($scale_setting, $width, $height, $extent, $dpi_factor, $ground_units) {
$dpi = 72.0 * $dpi_factor;
$minx = $extent[0];
$miny = $extent[1];
$maxx = $extent[2];
$maxy = $extent[3];
# Step Uno: Calculate the scales, choose the "densist"
$scale = ($maxx - $minx) / $width;
$scaleY = ($maxy - $miny) / $height;
if($scaleY < $scale) {
$scale = $scaleY;
}
# Step Zwei: Recalculate the extent
$centerX = ($maxx + $minx) / 2.0;
$centerY = ($maxy + $miny) / 2.0;
$inches_per_unit = 39.3701; #meters
if($ground_units == 'ft') {
$inches_per_unit = 12.0;
}
if(isset($scale_setting) and $scale_setting != 'map') {
$normScale = floatval($scale_setting);
if($normScale <= 0.0) {
$normScale = 1.0; # we'll just fix this if it's null.
}
if($normScale > 1.0) {
$normScale = 1.0 / floatval($scale_setting);
}
$scale = 1.0 / ($normScale * $inches_per_unit * $dpi);
$scale = $scale * (72.6/72.0); # magic number?
}
$scale = $scale * .5;
$minx = $centerX - $scale * $width;
$maxx = $centerX + $scale * $width;
$miny = $centerY - $scale * $height;
$maxy = $centerY + $scale * $height;
return array($minx, $miny, $maxx, $maxy);
}
?>