-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.php
95 lines (79 loc) · 3.23 KB
/
console.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
use Cilex\Provider\Console\Adapter\Silex\ConsoleServiceProvider,
Silex\Application,
Symfony\Component\Console\Input\InputInterface,
Symfony\Component\Console\Input\InputArgument,
Symfony\Component\Console\Input\InputOption,
Symfony\Component\Console\Output\OutputInterface;
require_once __DIR__ . '/vendor/autoload.php';
define('START_DATE', '2013-01-01');
$app = new Silex\Application;
// Console Service Provider and command-line commands
$app->register(new ConsoleServiceProvider(), array(
'console.name' => '365days.me',
'console.version' => '1.0',
));
$app['console']
->register('photo:add')
->setDescription('Generates and stores meta-data such as title, date and EXIF for a photo.')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$dialog = $app['console']->getHelperSet()->get('dialog');
// Ask which photo day are we processing for
$day = $dialog->ask(
$output,
'Which day are we processing (Number only!): ',
''
);
// Obtain title of the photo
$title = $dialog->ask(
$output,
'What would you like the photo title to be? ',
''
);
// If no day was provided, then calculate the number of day for today
if ($day == "") {
// Calculate the number of day for today
$start_date = new DateTime(START_DATE);
$today = new DateTime("now");
$interval = $today->diff($start_date);
$day = $interval->format('%a') + 1;
}
// Now calculate the date for this day based on the calculated $day
$date = date('Y-m-d', strtotime('+'.($day-1).' days', strtotime(START_DATE)));
// Path to meta data file
$meta_file_path = __DIR__.'/data/metadata/'.str_pad($day, 3, 0, STR_PAD_LEFT).".json";
// Path to image file
$image_file_path = __DIR__.'/web/assets/'.str_pad($day, 3, 0, STR_PAD_LEFT).".jpg";
if (file_exists($image_file_path))
{
$meta_info = exif_read_data($image_file_path);
$meta_data = array();
$meta_data["entry_day"] = $day;
$meta_data["entry_date"] = $date;
$meta_data["entry_title"] = strlen($title)?$title:$date;
$meta_data["image_exif"] = $meta_info;
$meta_data_json = json_encode($meta_data, JSON_PRETTY_PRINT);
// Save meta information
file_put_contents($meta_file_path, $meta_data_json);
// Generate thumbnail for archives page
$imagine = new Imagine\Gd\Imagine();
$mode = Imagine\Image\ImageInterface::THUMBNAIL_INSET;
$photo = $imagine->open($image_file_path);
$size = $photo->getSize()->scale(0.21);
$photo
->thumbnail($size, $mode)
->save(__DIR__.'/web/assets/'.str_pad($day, 3, 0, STR_PAD_LEFT)."s.jpg")
;
// Update last added num
$last_added = trim(file_get_contents(__DIR__.'/data/last_added'));
if ($day > $last_added) {
file_put_contents(__DIR__.'/data/last_added', $day);
}
$output->writeln(sprintf("\nPhoto for day #%s has been processed. See you tomorrow!", $day));
}
else {
$output->writeln(sprintf("\nERROR: Photo %s.jpg was not found.", str_pad($day, 3, 0, STR_PAD_LEFT)));
}
})
;
$app['console']->run();