2 /* This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3 and is published under the AGPL license. */
25 function __construct($width, $height, $margint, $marginr, $marginb, $marginl) {
26 $this->width = $width;
27 $this->height = $height;
28 $this->margint = $margint;
29 $this->marginr = $marginr;
30 $this->marginb = $marginb;
31 $this->marginl = $marginl;
35 $this->maxx = $this->width;
36 $this->maxy = $this->height;
38 $this->picture = imagecreatetruecolor ($this->width, $this->height);
41 function __destruct() {
43 imagedestroy ($this->picture);
47 function setViewBox($minx, $miny, $maxx, $maxy) {
48 $this->xdelta = pow(10, floor(log10($maxx)));
49 $this->maxx = ceil ($maxx / $this->xdelta) * $this->xdelta;
50 $this->minx = floor ($minx / $this->xdelta) * $this->xdelta;
52 $this->ydelta = pow(10, floor(log10($maxy)));
53 $this->maxy = ceil ($maxy / $this->ydelta) * $this->ydelta;
54 $this->miny = floor ($miny / $this->ydelta) * $this->ydelta;
59 function getViewBox() {
60 return array($this->minx, $this->miny, $this->maxx, $this->maxy);
63 function setThickness($thickness) {
64 imagesetthickness ($this->picture, $thickness);
69 function setFont($fontfile, $fontsize) {
70 $this->fontfile = $fontfile;
71 $this->fontsize = $fontsize;
76 function text($x, $y, $text, $color, $pos = "lt", $limits = array(null, null, null, null)) {
77 $bbox = imagettfbbox($this->fontsize, 0, $this->fontfile, $text);
79 switch (substr($pos, 0, 1)) {
81 $x -= $bbox[2] - $bbox[0];
84 $x -= ($bbox[2] - $bbox[0]) / 2;
91 switch (substr($pos, 1, 1)) {
93 $y -= $bbox[7] - $bbox[1];
96 $y -= ($bbox[7] - $bbox[1]) / 2;
103 list($limtop, $limright, $limbottom, $limleft) = $limits;
105 if (!is_null($limtop)) {
106 $y = max($limtop, $y);
108 if (!is_null($limright)) {
109 $x = min($limright, $x);
111 if (!is_null($limbottom)) {
112 $y = min($limbottom, $y);
114 if (!is_null($limleft)) {
115 $x = max($limleft, $x);
118 imagettftext($this->picture, $this->fontsize, 0, $x, $y, $this->color($color), $this->fontfile, $text);
121 function rect($x1, $y1, $x2, $y2, $color, $translate = false) {
122 imagefilledrectangle ($this->picture, $x1, $y1, $x2, $y2, $this->color($color));
127 function line($x1, $y1, $x2, $y2, $color) {
128 imageline ($this->picture, $x1, $y1, $x2, $y2, $this->color($color));
133 function draw($filename = null) {
134 imagepng($this->picture, $filename);
139 private $colorcache = array();
140 function color($str) {
141 if (!isset ($this->colorcache[$str])) {
142 $r = "0x" . substr($str, 0, 2);
143 $g = "0x" . substr($str, 2, 2);
144 $b = "0x" . substr($str, 4, 2);
145 $this->colorcache[$str] = imagecolorallocate ($this->picture, $r, $g, $b);
147 return $this->colorcache[$str];
150 function canvasXPos($x) {
151 return round(($x - $this->minx) * ($this->width - $this->marginr - $this->marginl) / ($this->maxx - $this->minx)) + $this->marginl;
153 function canvasYPos($y) {
154 return $this->height - round(($y - $this->miny) * ($this->height - $this->marginb - $this->margint) / ($this->maxy - $this->miny)) - $this->margint;
158 class ProfileController extends Zend_Controller_Action {
159 public function init() {
160 if (!extension_loaded('gd')) {
161 throw new Syj_Exception_NotImplemented("gd is not installed");
163 $this->_helper->SyjNoRender->disableRender();
166 public function indexAction() {
167 $url = $this->getRequest()->getUserParam('url');
169 throw new Syj_Exception_NotFound('Not Found', 404);
172 $pathMapper = new Syj_Model_PathMapper();
173 $path = new Syj_Model_Path();
174 if (!$pathMapper->findByUrl($url, $path)) {
175 if (is_numeric($url) and $pathMapper->hasexisted($url)) {
176 throw new Syj_Exception_NotFound('Gone', 410);
178 throw new Syj_Exception_NotFound('Not Found', 404);
182 $size = $this->getRequest()->getQuery('size', 'big');
183 if ($size == 'small') {
192 $file = $path->getProfileCache($size);
193 if (file_exists($file)) {
194 if (filesize($file) == 0) {
195 throw new Syj_Exception_NotImplemented("could not compute altitude profile");
197 $this->sendFile($file);
202 $service = $this->_helper->SyjAltiService->service();
203 /* we accept 2% of invalid values in the profile */
204 $profile = $path->getAltiProfile($service, 2 / 100);
205 } catch(Syj_Exception_NotImplemented $e) {
210 $canvas = $this->drawProfile($profile, $width, $height);
211 $canvas->draw($file);
212 $this->sendFile($file);
215 protected function drawProfile($profile, $width, $height) {
221 $last = end($profile);
224 $minalti = array(array(0, INF));
225 $maxalti = array(array(0, -INF));
226 foreach ($profile as $coords) {
229 if ($alti == $minalti[0][1]) {
230 $minalti []= array($coords[0], $alti);
231 } else if ($alti < $minalti[0][1]) {
232 $minalti = array(array($coords[0], $alti));
235 if ($alti == $maxalti[0][1]) {
236 $maxalti []= array($coords[0], $alti);
237 } else if ($alti > $maxalti[0][1]) {
238 $maxalti = array(array($coords[0], $alti));
242 $canvas = new Canvas($width, $height, $margint, $marginr, $marginb, $marginl);
243 $canvas->setViewBox(0, $minalti[0][1], $maxdist, $maxalti[0][1]);
245 $canvas->rect(0, 0, $width, $height, "FFFFFF");
247 $slopecolor = function ($slope) {
249 return "FF0000"; // red
250 } else if ($slope > 12) {
252 } else if ($slope > 7) {
254 } else if ($slope > 3) {
256 } else if ($slope > 0) {
257 return "FFFF00"; // yellow
258 } else if ($slope > -3) {
260 } else if ($slope > -7) {
262 } else if ($slope > -12) {
264 } else if ($slope > -18) {
267 return "0000FF"; // blue;
271 $canvas->setThickness (2);
274 foreach ($profile as $coord) {
275 if (!is_null($prev)) {
278 $prevdist = $prev[0];
279 $prevalti = $prev[1];
281 $slope = 100 * ($alti - $prevalti) / ($dist - $prevdist) ;
282 $canvas->line($canvas->canvasXPos($prevdist), $canvas->canvasYPos($prevalti),
283 $canvas->canvasXPos($dist), $canvas->canvasYPos($alti),
284 $slopecolor($slope), true);
289 $canvas->setThickness (1);
290 $canvas->setFont(APPLICATION_PATH . '/resources/' . 'DejaVuSans.ttf', 10);
292 list ($minx, $miny, $maxx, $maxy) = $canvas->getViewBox();
294 $deltax = pow(10, floor(log10($maxx - $minx)));
295 foreach (range($minx, $maxx, $deltax) as $x) {
296 $canvas->line($canvas->canvasXPos($x), $height - $marginb + 10, $canvas->canvasXPos($x), $height - $marginb, "000000");
297 if ($deltax < 1000) {
300 $text = round($x / 1000) . "km";
302 $canvas->text($canvas->canvasXPos($x), $height - $marginb + 10 + 3, $text, "000000", "cb");
304 $canvas->line($marginl, $height - $marginb, $width - $marginr, $height - $marginb, "000000");
306 $deltay = pow(10, floor(log10($maxy - $miny)));
307 foreach (range($miny, $maxy, $deltay) as $y) {
308 $canvas->line($marginl - 10, $canvas->canvasYPos($y), $marginl, $canvas->canvasYPos($y), "000000");
310 $canvas->text($marginl - 10 - 3, $canvas->canvasYPos($y), $text, "000000", "rc");
312 $canvas->line($marginl, $height - $marginb, $marginr, $margint, "000000");
314 list($minx, $miny, $maxx, $maxy) = $canvas->getViewBox();
315 $limits = array($canvas->canvasYPos($maxy) + 3, $canvas->canvasXPos($maxx) - 3, $canvas->canvasYPos($miny) - 3, $canvas->canvasXPos($minx) + 3);
318 foreach ($minalti as $coords) {
319 list ($dist, $alti) = $coords;
320 if (!is_null($prev)) {
321 $xdist = $canvas->canvasXPos($dist) - $canvas->canvasXPos($prev[0]);
322 /* second label would be less than 10px from previous one. Do
328 $text = round($alti) . "m";
329 $xpos = $canvas->canvasXPos($dist);
330 $ypos = $canvas->canvasYPos($alti);
331 $canvas->text($xpos, $ypos + 5, $text, "000000", "cb", $limits);
332 $canvas->rect($xpos - 1, $ypos - 1, $xpos + 1, $ypos + 1, "000000");
337 foreach ($maxalti as $coords) {
338 list ($dist, $alti) = $coords;
339 if (!is_null($prev)) {
340 $xdist = $canvas->canvasXPos($dist) - $canvas->canvasXPos($prev[0]);
341 /* second label would be less than 10px from previous one. Do
347 $text = round($alti) . "m";
348 $xpos = $canvas->canvasXPos($dist);
349 $ypos = $canvas->canvasYPos($alti);
350 $canvas->text($xpos, $ypos - 5, $text, "000000", "ct", $limits);
351 $canvas->rect($xpos - 1, $ypos - 1, $xpos + 1, $ypos + 1, "000000");
358 protected function sendFile($file) {
359 $lastmodified = filemtime($file);
361 $request = $this->getRequest();
362 $response = $this->getResponse();
364 $response->setHeader('Content-Type', 'image/png', true)
365 ->setHeader('Content-Length', filesize($file), true);
367 if ($request->getServer("HTTP_IF_MODIFIED_SINCE")) {
368 if ($lastmodified <= strtotime($request->getServer("HTTP_IF_MODIFIED_SINCE"))) {
369 $response->setHttpResponseCode(304);
374 // no-cache is needed otherwise IE does not try to get new version.
375 $response->setHeader ('Cache-control', 'no-cache, must-revalidate', true)
376 ->setHeader("Last-Modified", gmdate("D, d M Y H:i:s", filemtime($file)) . " GMT", true);