Skip to content

Commit

Permalink
Added Map class and updated generating and uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
ovidiuch committed Feb 28, 2012
1 parent 6df1bf4 commit c33786e
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 82 deletions.
81 changes: 3 additions & 78 deletions generate.php
@@ -1,81 +1,6 @@
<?php

include('city.php');
include('map.php');

// Init

date_default_timezone_set('Europe/London');

// Cities

$cities = City::get('cities.txt');

// Img

$img = imagecreatetruecolor(300, ceil(count($cities) / 2) * 46 + 6);

// Background

$bg = imagecolorallocate($img, 10, 10, 10);

imagefill($img, 0, 0, $bg);

// Font

$bold_font = 'OpenSans-Bold.ttf';
$light_font = 'OpenSans-Light.ttf';

$m_size = 10;
$c_size = 14;

// Text

$i = 0;
$x = 0;
$y = 0;

foreach($cities as $city)
{
$light = $city->get_light();

$w = 75 + (180 * $light);
$g = 75 + (150 * $light);

$white = imagecolorallocate($img, $w, $w, $w);
$gray = imagecolorallocate($img, $g, $g, $g);
$dark = imagecolorallocate($img, 75, 75, 75);

if($i && $i % 2 == 0)
{
$x = 0;
$y += 46;
}
imagettftext($img, $m_size, 0, 10 + $x, 20 + $y, $gray, "fonts/{$city->font}", $city->get_message());
imagettftext($img, $c_size, 0, 10 + $x, 40 + $y, $dark, "fonts/$light_font", '#');
imagettftext($img, $c_size, 0, 22 + $x, 40 + $y, $white, "fonts/$bold_font", $city->name);

$i++;
$x += 140;
}

// Save

if(!empty($_GET['path']))
{
if(!imagejpeg($img, $_GET['path'], 100))
{
exit;
}
}

// Header

header('Content-Type: image/jpeg');

// Print

imagejpeg($img, null, 100);

// Destroy

imagedestroy($img);
$map = new Map;
$map->output();
110 changes: 110 additions & 0 deletions map.php
@@ -0,0 +1,110 @@
<?php

include('city.php');

class Map
{
private $cities;
private $image;

private $bold = 'OpenSans-Bold.ttf';
private $light = 'OpenSans-Light.ttf';

private $big = 14;
private $small = 10;

public function generate()
{
date_default_timezone_set('Europe/London');

$this->cities = City::get('cities.txt');

$this->create_image();

$this->create_background();

$this->create_text();
}

public function output($quality = 100)
{
if(!$this->image)
{
$this->generate();
}
header('Content-Type: image/jpeg');

imagejpeg($this->image, null, $quality);

imagedestroy($this->image);
}

public function save($path, $quality = 100)
{
if(!$this->image)
{
$this->generate();
}
return imagejpeg($this->image, $path, $quality);
}

private function create_image()
{
$this->image = imagecreatetruecolor
(
300, ceil(count($this->cities) / 2) * 46 + 6
);
}

private function create_background()
{
$bg = imagecolorallocate($this->image, 10, 10, 10);

imagefill($this->image, 0, 0, $bg);
}

private function create_text()
{
$x = 10;
$y = 20;

foreach($this->cities as $k => $city)
{
$light = $city->get_light();

$g = 75 + (150 * $light);
$w = 75 + (180 * $light);

$dark = imagecolorallocate($this->image, 75, 75, 75);
$medium = imagecolorallocate($this->image, $g, $g, $g);
$light = imagecolorallocate($this->image, $w, $w, $w);

if($k && $k % 2 == 0)
{
$x = 10;
$y += 46;
}
$this->write
(
$city->get_message(), $city->font, $this->small, $medium, $x, $y
);
$this->write
(
'#', $this->light, $this->big, $dark, $x, $y + 20
);
$this->write
(
$city->name, $this->bold, $this->big, $light, $x + 12, $y + 20
);
$x += 140;
}
}

private function write($text, $font, $size, $color, $x, $y)
{
imagettftext
(
$this->image, $size, 0, $x, $y, $color, "fonts/$font", $text
);
}
}
10 changes: 6 additions & 4 deletions upload.php
@@ -1,9 +1,11 @@
<?php

include('map.php');

// Lib

require('lib/tmhOAuth/tmhOAuth.php');
require('lib/tmhOAuth/tmhUtilities.php');
include('lib/tmhOAuth/tmhOAuth.php');
include('lib/tmhOAuth/tmhUtilities.php');

// Auth

Expand All @@ -24,9 +26,9 @@

// Generate

$url = 'http://localhost/gutenHashTag/generate.php';
$map = new Map();

if(!file_get_contents("$url?path=$path") || !$path = realpath($path))
if(!$map->save($path) || !$path = realpath($path))
{
exit;
}
Expand Down

0 comments on commit c33786e

Please sign in to comment.