-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.class.php
476 lines (337 loc) · 13.6 KB
/
generate.class.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
class PrintGenerator
{
private $clockImagePath;
private $locationPinImagePath;
private $speechBubbleImagePath;
private $heartShapeImagePath;
private $logoImagePath;
private $defaultProfilePicturePath;
private $defaultPhotoPath;
private $Font;
private $BoldFont;
private $username = "";
private $location = "";
private $caption = "";
private $link = "";
private $profilePictureURL;
private $photoURL;
private $creationTime;
private $likes;
private $logo;
private $canvas;
private $white;
private $blue;
private $paleBlue;
private $grey;
private $darkGrey;
public function __construct($username, $location, $caption, $link, $profilePictureURL, $photoURL, $creationTime, $likes, $logo)
{
if (!empty($username)) $this->username = $username;
if (!empty($location)) $this->location = $location;
if (!empty($caption)) $this->caption = $caption;
if (!empty($link)) $this->link = $link;
$this->profilePictureURL = $profilePictureURL;
$this->photoURL = $photoURL;
$this->creationTime = $creationTime;
$this->likes = $likes;
$this->logo = $logo;
$this->clockImagePath = $_SERVER['DOCUMENT_ROOT'].'/assets/images/clock.png';
$this->locationPinImagePath = $_SERVER['DOCUMENT_ROOT'].'/assets/images/locationPin.png';
$this->speechBubbleImagePath = $_SERVER['DOCUMENT_ROOT'].'/assets/images/speechBubble.png';
$this->heartShapeImagePath = $_SERVER['DOCUMENT_ROOT']."/assets/images/heartShape.png";
$this->logoImagePath = $_SERVER['DOCUMENT_ROOT'].'/assets/logo/'.$logo.'.png';
$this->defaultProfilePicturePath = $_SERVER['DOCUMENT_ROOT']."/assets/images/defaultProfilePicture.jpg";
$this->defaultPhotoPath = $_SERVER['DOCUMENT_ROOT']."/assets/images/defaultPhoto.jpg";
$this->Font = $_SERVER['DOCUMENT_ROOT']."/assets/fonts/Helvetica.ttf";
$this->BoldFont = $_SERVER['DOCUMENT_ROOT']."/assets/fonts/HelveticaBold.ttf";
$this->canvas = imagecreatetruecolor(640, 960);
$this->white = imagecolorallocate($this->canvas, 255, 255, 255);
$this->blue = imagecolorallocate($this->canvas, 46, 94, 134);
$this->paleBlue = imagecolorallocate($this->canvas, 83, 152, 209);
$this->grey = imagecolorallocate($this->canvas, 150, 150, 150);
$this->darkGrey = imagecolorallocate($this->canvas, 70, 70, 70);
}
public function getPrintJpeg()
{
imageantialias($this->canvas, true);
$this->draw();
// Start a new output buffer
ob_start();
imagejpeg($this->canvas, NULL, 100);
$print = ob_get_contents();
// Stop this output buffer
ob_end_clean();
imagedestroy($this->canvas);
return $print;
}
public function decodeCharacters($string)
{
return iconv("UTF-8", "ISO-8859-1", $string);
}
public function imageFromUrl($url)
{
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_CONNECTTIMEOUT => 5
));
$image = curl_exec($curl);
curl_close($curl);
return $image;
}
public function formattedCreationTime()
{
if (empty($this->creationTime)) $creationTimeFormatted = "ERR:OR PM";
else $creationTimeFormatted = gmdate('g:i A', $this->creationTime);
return $creationTimeFormatted;
}
private function formatInput()
{
$username = $this->username;
$location = $this->location;
$caption = $this->caption;
/* Check that the strings aren't too long and clean up input*/
$username = (strlen($username) > 22) ? substr($username, 0, 20).'...' : $username;
$location = (strlen($location) > 33) ? substr($location, 0, 30).'...' : $location;
// Get rid of any special characters that could cause problems
$username = stripslashes($username);
$location = stripslashes($location);
$caption = stripslashes($caption);
$this->username = $this->decodeCharacters($username);
$this->location = $this->decodeCharacters($location);
$this->caption = $this->decodeCharacters($caption);
/* echo "input formatted!".$this->username.$this->location.$this->caption; */
}
private function draw()
{
$clockImagePath = $this->clockImagePath;
$locationPinImagePath = $this->locationPinImagePath;
$speechBubbleImagePath = $this->speechBubbleImagePath;
$heartShapeImagePath = $this->heartShapeImagePath;
$logoImagePath = $this->logoImagePath;
$defaultProfilePicturePath = $this->defaultProfilePicturePath;
$defaultPhotoPath = $this->defaultPhotoPath;
$Font = $this->Font;
$BoldFont = $this->BoldFont;
$username = $this->username;
$location = $this->location;
$caption = $this->caption;
$link = $this->link;
$profilePictureURL = $this->profilePictureURL;
$photoURL = $this->photoURL;
$creationTime = $this->creationTime;
$likes = $this->likes;
$logo = $this->logo;
$canvas = $this->canvas;
$white = $this->white;
$blue = $this->blue;
$paleBlue = $this->paleBlue;
$grey = $this->grey;
$darkGrey = $this->darkGrey;
$this->formatInput();
# ! Draw the white background of the image
imagefilledrectangle($canvas, 0, 0, 640, 960, $white);
// Draw the profile picture
// If the links to the photos are incomplete default to local images
$profilePicture = (empty($profilePictureURL)) ? imagecreatefromjpeg($defaultProfilePicturePath) : imagecreatefromstring($this->imageFromUrl($profilePictureURL));
imagecopyresized($canvas, $profilePicture, 30, 50, 0, 0, 50, 50, 150, 150);
imagedestroy($profilePicture);
# ! Draw the photo
// If the links to the photos are incomplete default to local images
$photo = (empty($photoURL)) ? imagecreatefromjpeg($defaultPhotoPath) : imagecreatefromstring(($this->imageFromUrl($photoURL)));
imagecopyresized($canvas, $photo, 30, 110, 0, 0, 580, 580, 612, 612);
imagedestroy($photo);
// If there is a location add it
$creationTimeFormatted = $this->formattedCreationTime();
if (!empty($location)) {
// Location Pin
$locationPinImage = imagecreatefrompng($locationPinImagePath);
imagecopy($canvas, $locationPinImage, 90, 77, 0, 0, 16, 23);
imagedestroy($locationPinImage);
// Location
imagettftext($canvas, 21, 0, 110, 100, $paleBlue, $BoldFont, $location);
// Username
imagettftext($canvas, 21, 0, 90, 70, $blue, $BoldFont, $username);
// Creation Time
$dimensions = imagettfbbox(21, 0, $BoldFont, $creationTimeFormatted);
$textWidth = abs($dimensions[4] - $dimensions[0]);
$horizontalPositionOfCreationTimeText = (611 - $textWidth);
imagettftext($canvas, 21, 0, $horizontalPositionOfCreationTimeText, 70, $grey, $BoldFont, $creationTimeFormatted);
// Add the clock image
$clockImage = imagecreatefrompng($clockImagePath);
imagecopy($canvas, $clockImage, ($horizontalPositionOfCreationTimeText - 25), 50, 0, 0, 18, 18);
imagedestroy($clockImage);
}
else {
// Username
imagettftext($canvas, 21, 0, 90, 92, $blue, $BoldFont, $username);
// Creation Time
$dimensions = imagettfbbox(21, 0, $BoldFont, $creationTimeFormatted);
$textWidth = abs($dimensions[4] - $dimensions[0]);
$horizontalPositionOfCreationTimeText = (608 - $textWidth);
imagettftext($canvas, 21, 0, $horizontalPositionOfCreationTimeText, 90, $grey, $BoldFont, $creationTimeFormatted);
// Add the clock image
$clockImage = imagecreatefrompng($clockImagePath);
imagecopy($canvas, $clockImage, ($horizontalPositionOfCreationTimeText - 25), 72, 0, 0, 18, 18);
imagedestroy($clockImage);
}
# ! Likes
$likes = intval($likes);
if ($likes>0) {
// Add the heart shape image
$heartShapeImage = imagecreatefrompng($heartShapeImagePath);
imagecopy($canvas, $heartShapeImage, 30, 708, 0, 0, 24, 24);
imagedestroy($heartShapeImage);
// Add the number of likes - fix singular/plural issue
$formattedNumberOfLikes = ($likes>1) ? "$likes likes" : "$likes like";
imagettftext($canvas, 21, 0, 60, 730, $blue, $BoldFont, $formattedNumberOfLikes);
}
# ! Comments
if (!empty($caption)) {
// Move the line lower if there is a 'likes' section above it
$heightDifferenceDueToLikes = ($likes>0) ? 35 : 0;
// Add the speech bubble image
$speechBubbleImage = imagecreatefrompng($speechBubbleImagePath);
imagecopy($canvas, $speechBubbleImage, 30, (710 + $heightDifferenceDueToLikes), 0, 0, 24, 24);
imagedestroy($speechBubbleImage);
// Add the comment - Username
$dimensions = imagettfbbox(21, 0, $BoldFont, $username);
$textWidth = abs($dimensions[4] - $dimensions[0]);
imagettftext($canvas, 21, 0, 60, (730 + $heightDifferenceDueToLikes), $blue, $BoldFont, $username);
// Calculate where to append the rest of the comment
$horizontalPositionOfComment = ($textWidth + 60 + 10);
$dimensions = imagettfbbox(21, 0, $Font, $caption);
$farRightXCoordinate = abs($dimensions[4] - $dimensions[0]) + $horizontalPositionOfComment;
if ($farRightXCoordinate <= (600 - 10)) {
// Comment fits on one line - no worries here!
// Add the rest of the comment
imagettftext($canvas, 22, 0, $horizontalPositionOfComment, (730 + $heightDifferenceDueToLikes), $darkGrey, $Font, $caption);
}
else {
// We are going to have to bodge it about a bit...
// If there are no likes then we have an extra line available
$maximumNumberOfLinesForComments = ($likes==0) ? "3" : "2";
$lineCount;
$firstLine;
$secondeLine;
$words = explode(" ", $caption);
// First line of the comment
$index = 0;
$farRightXCoordinate;
do {
// Add add a word to the first line
$firstLine .= $words[$index].' ';
// Increment the index value by 1
$index ++;
// Calculate the new far right position
$dimensions = imagettfbbox(21, 0, $Font, $firstLine);
$farRightXCoordinate = abs($dimensions[4] - $dimensions[0]) + $horizontalPositionOfComment;
}
while ($farRightXCoordinate <= (600 - 10));
$firstLine = array_slice($words, 0, ($index-1));
$firstLine = implode(' ', $firstLine);
imagettftext($canvas, 22, 0, $horizontalPositionOfComment, (730 + $heightDifferenceDueToLikes), $darkGrey, $Font, $firstLine);
// Second line of the comment
// Remove the words the were printed on the first line from the the array
$words = array_slice($words, ($index - 1));
// We are now starting the second line
$lineCount = 2;
for ($i = 0; $i < count($words); $i++) {
// Working out what the string will be next loop
$string = $secondeLine.$words[$i].$words[($i + 1)];
// Working out how big the box will be next loop
$dimensions = imagettfbbox(21, 0, $Font, $string);
// Working out the far right coordinate next loop
$farRightXCoordinate = abs($dimensions[4] - $dimensions[0]) + 60;
// If the string will be too long return
if ($farRightXCoordinate >= (600 - 10)) {
if ($lineCount==$maximumNumberOfLinesForComments) {
$secondeLine = substr($secondeLine, 0, -3)."...";
break;
}
$secondeLine .= PHP_EOL;
$lineCount ++;
}
// Append the word to the line
$secondeLine .= $words[$i].' ';
}
imagettftext($canvas, 22, 0, 60, (768 + $heightDifferenceDueToLikes), $darkGrey, $Font, $secondeLine);
}
}
# ! Link to Instergram
$dimensions = imagettfbbox(18, 0, $BoldFont, $link);
$textWidth = abs($dimensions[4] - $dimensions[0]);
$horizontalPositionOfCreationTimeText = (608 - $textWidth);
imagettftext($canvas, 18, 0, $horizontalPositionOfCreationTimeText, 910, $darkGrey, $BoldFont, $link);
# ! Taken on...
if (!empty($creationTime)) {
$takenOnDate = date("d/m/y", $creationTime);
$takenOnDate = "TAKEN ON $takenOnDate";
$dimensions = imagettfbbox(18, 0, $BoldFont, $takenOnDate);
$textWidth = abs($dimensions[4] - $dimensions[0]);
$horizontalPositionOfCreationTimeText = (608 - $textWidth);
imagettftext($canvas, 18, 0, $horizontalPositionOfCreationTimeText, 880, $darkGrey, $BoldFont, $takenOnDate);
}
# ! Add the logo
if (!empty($logo)) {
// Make the name all lowercase
$logo = strtolower($logo);
$logoImagePath = 'logo/'.$logo.'.png';
// Check that the logo exists
if (file_exists($logoImagePath)) {
$logoImage = imagecreatefrompng($logoImagePath);
list($width, $height) = getimagesize($logoImagePath);
imagecopy($canvas, $logoImage, 30, (910 - $height), 0, 0, $width, $height);
imagedestroy($logoImage);
}
}
}
}
# ! Instagram print from link
function instagramMediaIdFromLink($link)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "http://api.instagram.com/oembed?url=$link",
));
$responce = curl_exec($curl);
curl_close($curl);
$data = json_decode($responce);
return $data->media_id;
}
function instagramPrintFromMediaId($mediaId, $clientId)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://api.instagram.com/v1/media/$mediaId?client_id=$clientId",
));
$responce = curl_exec($curl);
curl_close($curl);
$media = json_decode($responce);
$username = $media->data->user->username;
$profilePictureURL = $media->data->user->profile_picture;
$photoURL = $media->data->images->standard_resolution->url;
$creationTime = $media->data->created_time;
$location = $media->data->location->name;
$caption = $media->data->caption->text;
$likes = $media->data->likes->count;
$link = $media->data->link;
$logo = "";
$printGenerator = new PrintGenerator($username, $location, $caption, $link, $profilePictureURL, $photoURL, $creationTime, $likes, $logo);
$print = $printGenerator->getPrintJpeg();
return $print;
}
function instagramPrintFromLink($link, $clientId)
{
if (empty($link)) die("No Link!");
$mediaId = instagramMediaIdFromLink($link);
if (empty($mediaId)) die("Invalid Media ID!");
$print = instagramPrintFromMediaId($mediaId, $clientId);
return $print;
}
?>