public
Description: Automatic approximation of typical Photoshop actions
Homepage: http://rcrowley.org/2007/11/08/introducing-curvr/
Clone URL: git://github.com/rcrowley/curvr.git
curvr / curvrmail.php
100755 146 lines (119 sloc) 3.891 kb
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/php
<?php
 
#
# curvr
# Richard Crowley <r@rcrowley.org>
#
 
# This works with my Nokia N73. I don't care if it works on your phone.
# Tread lightly.
 
# How should we upload?
$UPLOAD_METHOD = 'email'; # or 'api'
 
# Secret email address from Flickr
# Used if UPLOAD_METHOD is 'email'
$FLICKRMAIL = 'secret-flickrmail-address-goes-here';
 
# Flickr API token
# Used if UPLOAD_METHOD is 'api'
$FLICKR_TOKEN = 'flickr-api-token-goes-here';
 
# Dopplr API token
# If set, photos will be geotagged using FLICKR_TOKEN
# UPLOAD_METHOD must be 'api' if this is set
$DOPPLR_TOKEN = 'dopplr-api-token-goes-here';
 
$CURVR_BIN = '/home/rcrowley/bin/curvr';
$CURVR_PROCESS = 'curve';
$CURVR_VERSION = '0.2';
 
# Tags to add to every photo
$TAGS = "curvr curvr:process=$CURVR_PROCESS curvr:version=$CURVR_VERSION";
 
# Get the raw email body from procmail
$raw = file_get_contents('php://stdin');
file_put_contents('/tmp/debug_' . time(), $raw);
 
# Process each line
$lines = explode("\n", $raw);
$boundary = false;
$content_type = false;
$blank = false;
$body = array();
$jpeg = array();
foreach ($lines as $line) {
 
# First we have to find the boundary
if (false === $boundary && '--' == substr($line, 0, 2)) {
$boundary = $line;
}
 
# Seeing another boundary indicates the end of a part
else if ($boundary == $line) {
$content_type = false;
$blank = false;
}
 
# Note the content type if this line specifies it
else if (preg_match('/^Content-Type: ([a-z\/]+).*$/', $line, $match)) {
$content_type = $match[1];
}
 
# Now find the blank line that indicates the beginning of content
else if (!$blank && 0 == strlen(trim($line))) {
$blank = true;
}
 
# The body
else if ($blank && 'text/plain' == $content_type) {
$body[] = $line;
}
 
# The JPEG attachment
else if ($blank && 'image/jpeg' == $content_type) {
$jpeg[] = $line;
}
 
}
 
# Save the JPEG to be processed and emailed again
$ts = time();
@unlink("/tmp/curvrmail.$ts.jpg");
file_put_contents("/tmp/curvrmail.$ts.jpg",
base64_decode(implode('', $jpeg)), 0);
 
# Run curvr on the JPEG
`$CURVR_BIN /tmp/curvrmail.$ts.jpg $CURVR_PROCESS /tmp/curvrmail.{$ts}_curve.jpg`;
 
# Get location info from Dopplr
# At a bear minimum, use the city and non-US country as tags
$tags = '';
$geo = array();
if (isset($DOPPLR_TOKEN) && '' != $DOPPLR_TOKEN) {
$xml = simplexml_load_file(
"https://www.dopplr.com/api/traveller_info?token=$DOPPLR_TOKEN");
$tags = strtolower(preg_replace('/\s+/', '',
(string)$xml->traveller->current_city->name));
if ('US' != (string)$xml->traveller->current_city->country_code) {
$tags .= ' ' . strtolower(preg_replace('/\s+/', '',
(string)$xml->traveller->current_city->country));
}
$geo['lat'] = (double)$xml->traveller->current_city->latitude;
$geo['long'] = (double)$xml->traveller->current_city->longitude;
if ((bool)$xml->traveller->travel_today) {
$tags .= ' dopplr:trip=' . (string)$xml->traveller->current_trip->id;
}
}
 
# Email to Flickr
if ('email' == $UPLOAD_METHOD) {
$subject = implode(' ', $body);
if (false === strpos($subject, 'tags:')) {
$subject .= ' tags:';
}
mail($FLICKRMAIL, "$subject $tags $TAGS",
"\r\n--deadbeef-for-dinner\r\n" .
"Content-Type: text/plain; " .
"charset=\"utf-8\"\r\n" .
"Content-Transfer-Encoding: 7bit\r\n\r\n" .
"\r\n\r\n--deadbeef-for-dinner\r\n" .
"Content-Type: image/jpeg; filename=\"photo.jpg\"\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment\r\n\r\n" .
base64_encode(file_get_contents("/tmp/curvrmail.{$ts}_curve.jpg")) .
"\r\n\r\n--deadbeef-for-dinner--\r\n",
"From: Nobody <nobody@rcrowley.org>\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; " .
"boundary=\"deadbeef-for-dinner\"\r\n" .
"Content-Transfer-Encoding: 7bit\r\n");
}
 
# Upload using the API
else if ('api' == $UPLOAD_METHOD) {
 
# Upload
###
 
# Really geotag
###
 
}
 
?>