Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed code styling #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

[*.php]

indent_style = space
indent_size = 4
tab_width = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# IDEA Project Files
# .dots
.idea/
.php_cs.cache

# misc
vendor/
composer.lock
281 changes: 140 additions & 141 deletions Component/CSSColorParser.php

Large diffs are not rendered by default.

111 changes: 60 additions & 51 deletions Component/ImageExportService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Mapbender\PrintBundle\Component;

use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -31,8 +32,8 @@ public function export($content)
$this->requests[$i] = $layer['url'];
}

if(isset($this->data['vectorLayers'])){
foreach ($this->data['vectorLayers'] as $idx => $layer){
if (isset($this->data['vectorLayers'])) {
foreach ($this->data['vectorLayers'] as $idx => $layer) {
$this->data['vectorLayers'][$idx] = json_decode($this->data['vectorLayers'][$idx], true);
}
}
Expand All @@ -44,20 +45,19 @@ private function getImages()
{
$temp_names = array();
foreach ($this->requests as $k => $url) {

$url = strstr($url, '&WIDTH', true);
$width = '&WIDTH=' . $this->data['width'];
$height = '&HEIGHT=' . $this->data['height'];
$url .= $width . $height;

$this->container->get("logger")->debug("Image Export Request Nr.: " . $k . ' ' . $url);

$parsed = parse_url($url);
$parsed = parse_url($url);
$hostpath = $parsed['host'] . $parsed['path'];
$pos = strpos($hostpath, $this->urlHostPath);
$pos = strpos($hostpath, $this->urlHostPath);
if ($pos === 0 && ($routeStr = substr($hostpath, strlen($this->urlHostPath))) !== false) {
$attributes = $this->container->get('router')->match($routeStr);
$gets = array();
$gets = array();
parse_str($parsed['query'], $gets);
$subRequest = new Request($gets, array(), $attributes, array(), array(), array(), '');
} else {
Expand All @@ -74,19 +74,19 @@ private function getImages()
file_put_contents($imagename, $response->getContent());
$rawImage = null;
switch (trim($response->headers->get('content-type'))) {
case 'image/png' :
case 'image/png':
$rawImage = imagecreatefrompng($imagename);
break;
case 'image/png8' :
case 'image/png8':
$rawImage = imagecreatefrompng($imagename);
break;
case 'image/png; mode=24bit' :
case 'image/png; mode=24bit':
$rawImage = imagecreatefrompng($imagename);
break;
case 'image/jpeg' :
case 'image/jpeg':
$rawImage = imagecreatefromjpeg($imagename);
break;
case 'image/gif' :
case 'image/gif':
$rawImage = imagecreatefromgif($imagename);
break;
default:
Expand All @@ -107,7 +107,7 @@ private function getImages()

// Taking the painful way to alpha blending. Stupid PHP-GD
$opacity = floatVal($this->data['requests'][$k]['opacity']);
if(1.0 !== $opacity) {
if (1.0 !== $opacity) {
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$colorIn = imagecolorsforindex($image, imagecolorat($image, $x, $y));
Expand All @@ -118,7 +118,8 @@ private function getImages()
$colorIn['red'],
$colorIn['green'],
$colorIn['blue'],
$alphaOut);
$alphaOut
);
imagesetpixel($image, $x, $y, $colorOut);
imagecolordeallocate($image, $colorOut);
}
Expand Down Expand Up @@ -171,11 +172,11 @@ private function drawFeatures($finalImageName)
imagesavealpha($image, true);
imagealphablending($image, true);

foreach($this->data['vectorLayers'] as $idx => $layer) {
foreach($layer['geometries'] as $geometry) {
foreach ($this->data['vectorLayers'] as $idx => $layer) {
foreach ($layer['geometries'] as $geometry) {
$renderMethodName = 'draw' . $geometry['type'];

if(!method_exists($this, $renderMethodName)) {
if (!method_exists($this, $renderMethodName)) {
continue;
//throw new \RuntimeException('Can not draw geometries of type "' . $geometry['type'] . '".');
}
Expand All @@ -190,7 +191,7 @@ private function getColor($color, $alpha, $image)
{
list($r, $g, $b) = CSSColorParser::parse($color);

if(0 == $alpha) {
if (0 == $alpha) {
return ImageColorAllocate($image, $r, $g, $b);
} else {
$a = (1 - $alpha) * 127.0;
Expand All @@ -200,63 +201,67 @@ private function getColor($color, $alpha, $image)

private function drawPolygon($geometry, $image)
{
foreach($geometry['coordinates'] as $ring) {
if(count($ring) < 3) {
foreach ($geometry['coordinates'] as $ring) {
if (count($ring) < 3) {
continue;
}

$points = array();
foreach($ring as $c) {
foreach ($ring as $c) {
$p = $this->realWorld2mapPos($c[0], $c[1]);
$points[] = floatval($p[0]);
$points[] = floatval($p[1]);
}
imagesetthickness($image, 0);
// Filled area
if($geometry['style']['fillOpacity'] > 0){
if ($geometry['style']['fillOpacity'] > 0) {
$color = $this->getColor(
$geometry['style']['fillColor'],
$geometry['style']['fillOpacity'],
$image);
$image
);
imagefilledpolygon($image, $points, count($ring), $color);
}
// Border
$color = $this->getColor(
$geometry['style']['strokeColor'],
$geometry['style']['strokeOpacity'],
$image);
$image
);
imagesetthickness($image, $geometry['style']['strokeWidth']);
imagepolygon($image, $points, count($ring), $color);
}
}

private function drawMultiPolygon($geometry, $image)
{
foreach($geometry['coordinates'][0] as $ring) {
if(count($ring) < 3) {
foreach ($geometry['coordinates'][0] as $ring) {
if (count($ring) < 3) {
continue;
}

$points = array();
foreach($ring as $c) {
foreach ($ring as $c) {
$p = $this->realWorld2mapPos($c[0], $c[1]);
$points[] = floatval($p[0]);
$points[] = floatval($p[1]);
}
imagesetthickness($image, 0);
// Filled area
if($geometry['style']['fillOpacity'] > 0){
if ($geometry['style']['fillOpacity'] > 0) {
$color = $this->getColor(
$geometry['style']['fillColor'],
$geometry['style']['fillOpacity'],
$image);
$image
);
imagefilledpolygon($image, $points, count($ring), $color);
}
// Border
$color = $this->getColor(
$geometry['style']['strokeColor'],
$geometry['style']['strokeOpacity'],
$image);
$image
);
imagesetthickness($image, $geometry['style']['strokeWidth']);
imagepolygon($image, $points, count($ring), $color);
}
Expand All @@ -267,17 +272,19 @@ private function drawLineString($geometry, $image)
$color = $this->getColor(
$geometry['style']['strokeColor'],
$geometry['style']['strokeOpacity'],
$image);
$image
);
imagesetthickness($image, $geometry['style']['strokeWidth']);

for($i = 1; $i < count($geometry['coordinates']); $i++) {

for ($i = 1; $i < count($geometry['coordinates']); $i++) {
$from = $this->realWorld2mapPos(
$geometry['coordinates'][$i - 1][0],
$geometry['coordinates'][$i - 1][1]);
$geometry['coordinates'][$i - 1][1]
);
$to = $this->realWorld2mapPos(
$geometry['coordinates'][$i][0],
$geometry['coordinates'][$i][1]);
$geometry['coordinates'][$i][1]
);

imageline($image, $from[0], $from[1], $to[0], $to[1], $color);
}
Expand All @@ -289,38 +296,40 @@ private function drawPoint($geometry, $image)

$p = $this->realWorld2mapPos($c[0], $c[1]);

if(isset($geometry['style']['label'])){
if (isset($geometry['style']['label'])) {
// draw label with white halo
$color = $this->getColor('#ff0000', 1, $image);
$bgcolor = $this->getColor('#ffffff', 1, $image);
$fontPath = $this->container->getParameter('kernel.root_dir') . '/Resources/MapbenderPrintBundle/fonts/';
$font = $fontPath . 'OpenSans-Bold.ttf';
imagettftext($image, 14, 0, $p[0], $p[1]+1, $bgcolor, $font, $geometry['style']['label']);
imagettftext($image, 14, 0, $p[0], $p[1]-1, $bgcolor, $font, $geometry['style']['label']);
imagettftext($image, 14, 0, $p[0]-1, $p[1], $bgcolor, $font, $geometry['style']['label']);
imagettftext($image, 14, 0, $p[0]+1, $p[1], $bgcolor, $font, $geometry['style']['label']);
imagettftext($image, 14, 0, $p[0], $p[1] + 1, $bgcolor, $font, $geometry['style']['label']);
imagettftext($image, 14, 0, $p[0], $p[1] - 1, $bgcolor, $font, $geometry['style']['label']);
imagettftext($image, 14, 0, $p[0] - 1, $p[1], $bgcolor, $font, $geometry['style']['label']);
imagettftext($image, 14, 0, $p[0] + 1, $p[1], $bgcolor, $font, $geometry['style']['label']);
imagettftext($image, 14, 0, $p[0], $p[1], $color, $font, $geometry['style']['label']);
return;
}

$radius = $geometry['style']['pointRadius'];
// Filled circle
if($geometry['style']['fillOpacity'] > 0){
if ($geometry['style']['fillOpacity'] > 0) {
$color = $this->getColor(
$geometry['style']['fillColor'],
$geometry['style']['fillOpacity'],
$image);
imagefilledellipse($image, $p[0], $p[1], 2*$radius, 2*$radius, $color);
$image
);
imagefilledellipse($image, $p[0], $p[1], 2 * $radius, 2 * $radius, $color);
}
// Circle border
$color = $this->getColor(
$geometry['style']['strokeColor'],
$geometry['style']['strokeOpacity'],
$image);
imageellipse($image, $p[0], $p[1], 2*$radius, 2*$radius, $color);
$image
);
imageellipse($image, $p[0], $p[1], 2 * $radius, 2 * $radius, $color);
}

private function realWorld2mapPos($rw_x,$rw_y)
private function realWorld2mapPos($rw_x, $rw_y)
{
$quality = 72;
$map_width = $this->data['extentwidth'];
Expand All @@ -336,14 +345,14 @@ private function realWorld2mapPos($rw_x,$rw_y)
$maxX = $centerx + $map_width * 0.5;
$maxY = $centery + $map_height * 0.5;

$extentx = $maxX - $minX ;
$extenty = $maxY - $minY ;
$extentx = $maxX - $minX;
$extenty = $maxY - $minY;

$pixPos_x = (($rw_x - $minX)/$extentx) * $width;
$pixPos_y = (($maxY - $rw_y)/$extenty) * $height;
$pixPos_x = (($rw_x - $minX) / $extentx) * $width;
$pixPos_y = (($maxY - $rw_y) / $extenty) * $height;

$pixPos = array($pixPos_x, $pixPos_y);

return $pixPos;
return $pixPos;
}
}
27 changes: 14 additions & 13 deletions Component/OdgParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Mapbender\PrintBundle\Component;

use Symfony\Component\Config\Definition\Exception\Exception;
Expand Down Expand Up @@ -26,7 +27,7 @@ private function readOdgFile($template, $file)
$resource_dir = $this->container->getParameter('kernel.root_dir') . '/Resources/MapbenderPrintBundle';
$odgfile = $resource_dir . '/templates/' . $template . '.odg';

if(!is_file($odgfile)){
if (!is_file($odgfile)) {
throw new Exception("Print template '$template' doesn't exists.");
}

Expand Down Expand Up @@ -113,21 +114,21 @@ public function getConf($template)
if ($name == '') {
continue;
}
$width = $node->getAttribute('svg:width');
$width = $node->getAttribute('svg:width');
$height = $node->getAttribute('svg:height');
$x = $node->getAttribute('svg:x');
$y = $node->getAttribute('svg:y');
$field = array(
'font' => 'Arial',
'width' => substr($width, 0, -2) * 10,
'height' => substr($height, 0, -2) * 10,
'x' => substr($x, 0, -2) * 10,
'y' => substr($y, 0, -2) * 10,
$x = $node->getAttribute('svg:x');
$y = $node->getAttribute('svg:y');
$field = array(
'font' => 'Arial',
'width' => substr($width, 0, -2) * 10,
'height' => substr($height, 0, -2) * 10,
'x' => substr($x, 0, -2) * 10,
'y' => substr($y, 0, -2) * 10,
);

// Recognize font name and size
$textParagraph = $xpath->query("draw:text-box/text:p", $node)->item(0);
$textNode = $xpath->query("draw:text-box/text:p/text:span", $node)->item(0);
$textNode = $xpath->query("draw:text-box/text:p/text:span", $node)->item(0);
if ($textNode) {
$style = $textNode->getAttribute('text:style-name');
} elseif ($textParagraph) {
Expand All @@ -140,8 +141,8 @@ public function getConf($template)
}
$field['fontsize'] = $fontsize != '' ? $fontsize : '10pt';
$field['color'] = $color != '' ? $color : '#000000';
$data['fields'][ $name ] = $field;
$data['fields'][$name] = $field;
}
return $data;
}
}
}
Loading