-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.php
230 lines (195 loc) · 8.39 KB
/
filter.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* @author Tomiwa Adefokun <tomiwa.adefokun@reinsys.com>
* @copyright 2016-2017 Reinsys
* @uses Facebook
*/
require_once 'constants.php';
require_once __DIR__ . '/vendor/autoload.php';
$causes = array(
'quebec' => array('size'=>600, 'opacity' => 60, 'name' => 'flag_quebec', 'image' => 'flag_quebec.png', 'hash' => '#Pray4Quebec', 'title'=>'Pray for Quebec','button'=>'Post to timeline and then set as profile picture', 'contact'=>'postmediapr@gmail.com')
);
if(!isset($_GET['causeID']) || !in_array($_GET['causeID'], array_keys($causes))) {
die('Cause not found.');
exit;
}
else{
$causeID = trim(strtolower($_GET['causeID']));
$cause = (object) $causes[$causeID];
}
session_start();
$fb = new Facebook\Facebook([
'app_id' => FB_APP_ID,
'app_secret' => FB_APP_SECRET,
'default_graph_version' => 'v2.5',
]);
if(isset($_GET['logout'])){
unset($_SESSION['facebook_access_token']);
unset($_SESSION['facebook_name']);
unset($_SESSION['facebook_id']);
}
if(isset($_SESSION['facebook_access_token']) ) {
$accessToken = $_SESSION['facebook_access_token'];
$facebookName = $_SESSION['facebook_name'];
$facebookId = $_SESSION['facebook_id'];
$fb->setDefaultAccessToken($accessToken);
try {
$response = $fb->get('/me/picture/?redirect=false&type=square&width='.$cause->size.'&height='.$cause->size.'');
$pictureNode = $response->getGraphObject();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
// echo 'Graph returned an error: ' . $e->getMessage();
$outputMessage = '<div class="alert alert-warning" role="alert">An error occurred during processing.</div>';
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
$outputMessage = '<div class="alert alert-warning" role="alert">An error occurred during processing.</div>';
}
}
else{
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes', 'publish_actions']; // optional
$loginUrl = $helper->getLoginUrl(FILTER_BASE_URL.'/login-callback.php?causeID='.$causeID, $permissions);
}
if(isset($_POST['watermark'])) define('WATERMARK_OVERLAY_IMAGE', 'watermarks/'. $_POST['watermark'] . '.png');
define('WATERMARK_OVERLAY_OPACITY', $cause->opacity);
function create_watermark($source_file_path, $output_file_path)
{
list($fb_width, $fb_height, $source_type) = getimagesize($source_file_path);
$source_width = min($fb_width, $fb_height);
$source_height = min($fb_width, $fb_height);
if ($source_type === NULL) {
return false;
}
switch ($source_type) {
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif($source_file_path);
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg($source_file_path);
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng($source_file_path);
break;
default:
return false;
}
$overlay_gd_image = imagecreatefrompng(WATERMARK_OVERLAY_IMAGE);
$overlay_width = imagesx($overlay_gd_image);
$overlay_height = imagesy($overlay_gd_image);
//$overlay_width = imagesx($source_gd_image);
//$overlay_height = imagesy($source_gd_image);
$new_overlay_gd_image=ImageCreateTrueColor($source_width,$source_height);
// Transparent Background
$transparency = imagecolorallocatealpha($new_overlay_gd_image, 0, 0, 0, 127);
imagefill($new_overlay_gd_image, 0, 0, $transparency);
imagecolortransparent($new_overlay_gd_image, $transparency);
imagecopyresampled($new_overlay_gd_image,$overlay_gd_image,0,0,0,0,$source_width,$source_height,$overlay_width,$overlay_height);
imagecopymerge(
$source_gd_image,
$new_overlay_gd_image,
0,
0,
0,
0,
$source_width,
$source_height,
WATERMARK_OVERLAY_OPACITY
);
imagejpeg($source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY);
imagedestroy($source_gd_image);
imagedestroy($overlay_gd_image);
imagedestroy($new_overlay_gd_image);
}
/**
* Uploaded file processing function
*/
function process_image($facebookId, $url)
{
$temp_file_name = time() . '_' . $facebookId . '.jpg';
$url = $url;
$uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $temp_file_name;
file_put_contents($uploaded_file_path, file_get_contents($url));
list(, , $temp_type) = getimagesize($uploaded_file_path);
if ($temp_type === NULL) {
return false;
}
switch ($temp_type) {
case IMAGETYPE_GIF:
break;
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_PNG:
break;
default:
return false;
}
$processed_file_path = PROCESSED_IMAGE_DESTINATION . preg_replace('/\\.[^\\.]+$/', '.jpg', $temp_file_name);
$result = create_watermark($uploaded_file_path, $processed_file_path);
if ($result === false) {
return false;
} else {
return array($uploaded_file_path, $processed_file_path, $temp_file_name);
}
}
function thumb320($filename, $watermark){
$directory = ($watermark === true) ? 'watermarks/' : 'images/';
$thumbDirectory = $directory . 'thumb/';
$path = ($watermark === true) ? FILTER_BASE_URL .'/watermarks/' : FILTER_BASE_URL .'/images/';
$src_img_dir = $directory . $filename;
if(!file_exists($path . 'thumb/' . $filename)){
$system = explode('.',$filename);
if (preg_match('/png|PNG/',end($system))){
$src_img=imagecreatefrompng($src_img_dir);
}
else $src_img=imagecreatefromjpeg($src_img_dir);
$old_x = imagesx($src_img);
$old_y = imagesy($src_img);
$thumbImage=ImageCreateTrueColor(320,320);
$white = imagecolorallocate($thumbImage, 255, 255, 255);
imagefill($thumbImage, 0, 0, $white);
imagecopyresampled($thumbImage,$src_img,0,0,0,0,320,320,$old_x,$old_y);
if (preg_match("/png|PNG/",end($system)))
{
imagepng($thumbImage, $thumbDirectory . $filename);
} else {
imagejpeg($thumbImage, $thumbDirectory . $filename);
}
imagedestroy($thumbImage);
imagedestroy($src_img);
return $path . 'thumb/' . $filename;
}
else return $path . 'thumb/' . $filename;
}
/*
* Here is how to call the function(s)
*/
if((isset($_POST['submit']) || isset($_POST['save'])) && isset($_POST['watermark']) && isset($pictureNode) && $pictureNode->getProperty('url')){
$result = process_image($facebookId, $pictureNode->getProperty('url'));
if ($result === false) {
$outputMessage = '<div class="alert alert-warning" role="alert"><strong>Posting Failed!</strong> An error occurred during file processing.</div>';
} else {
if(isset($_POST['submit'])){
try{
$response = $fb->post('/me/photos/?url='. FILTER_BASE_URL .'/' . $result[1]);
$photoNode = $response->getGraphObject();
$publish = $fb->post('/me/feed/?link='. FILTER_BASE_URL .'/index.php?causeID='.$causeID.'&caption=Use Filter Now&picture='. thumb320($result[2], false) .'');
$publishNode = $publish->getGraphObject();
$photoUrl = 'https://www.facebook.com/photo.php?fbid='.$photoNode->getProperty('id').'&makeprofile=1&profile_id='.$facebookId.'&pp_source=photo_view';
echo '<script>window.open("'.$photoUrl.'")</script>';
$outputMessage = '<div class="alert alert-success" role="alert"><strong>Posted Successfully!</strong> Go to your <a href="http://facebook.com" target="_blank" class="alert-link">Facebook Page</a> to set it as your profile picture.</div>';
}
catch(Exceptions\FacebookResponseException $e){
$outputMessage = '<div class="alert alert-warning" role="alert"><strong>Posting Failed!</strong> We are unable to post to Facebook at the moment. Click <a href="'. FILTER_BASE_URL .'/' . $result[1] .'" target="_blank">here</a> to get your customized profile picture.</div>';
}
catch(Facebook\Exceptions\FacebookSDKException $e){
$outputMessage = '<div class="alert alert-warning" role="alert"><strong>Posting Failed!</strong> We are unable to post to Facebook at the moment. Click <a href="'. FILTER_BASE_URL .'/' . $result[1] .'" target="_blank">here</a> to get your customized profile picture.</div>';
}
}
else if(isset($_POST['save'])){
$outputMessage = '<div class="alert alert-success" role="alert"><strong>Saved Successfully!</strong> Click <a href="'. FILTER_BASE_URL .'/' . $result[1] .'" target="_blank">here</a> to get your customized profile picture.</div>';
}
@unlink($result[0]);
}
}
?>