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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
#######################################################################
# thumbnailing
sub is_ungainly
{
my $fn = shift; return $fn =~ /tif$/ || $fn =~ /bmp/;
}
sub thumbnail_filename
{
my ($filename) = @_;
if (is_ungainly($filename))
{
$filename =~ s/\..*$/.jpg/;
}
return lc $filename;
}
sub make_image_thumb
{
my ($args) = @_;
my $file = $args->{file};
my $key = $args->{key} || "t.";
my $maxwidth = $args->{maxwidth} || 450;
my $maxheight = $args->{maxheight} || 450;
my $filename = $file->{filename};
my $lcfilename = thumbnail_filename($filename);
use integer;
$key = "t." if (!defined($key));
print $file->{filename} if ($DEBUG);
my ($xold, $yold) = imgsize(qq!$data_path/$file->{thread}/$filename!);
my $x = $xold;
my $y = $yold;
my @convert_args = ();
push @convert_args, "$data_path/$file->{thread}/$filename";
if ($filename =~ /.gif$/) {
push @convert_args, "-coalesce";
}
push @convert_args, "-geometry";
if ($maxheight < 1 && $maxwidth < 1)
{ push @convert_args, "450x450"; }
elsif ($maxwidth < 1 || $key eq "b.")
{ push @convert_args, "450x".$maxheight; }
elsif ($maxheight < 1)
{ push @convert_args, $maxwidth; }
else
{ push @convert_args, $maxwidth."x".$maxheight; }
print "\n$filename -> .thumb/$key$lcfilename\n" if ($DEBUG);
print "old size: $xold x $yold\n" if ($DEBUG);
if ($filename =~ /.gif$/) {
push @convert_args, "+map";
}
push @convert_args, "$data_path/$file->{thread}/.thumb/$key$lcfilename";
if ($DEBUG)
{ print join " ", @convert_args; print "\n"; }
system($CONVERT_PATH, @convert_args);
system($CHMOD_PATH, 755, "$data_path/$file->{thread}/.thumb/$key$lcfilename");
}
# make a square thumbnail:
# crop bottom if portrait, crop sides if landscape
sub make_square_thumb
{
my ($file, $maxwidth, $key) = @_;
my $filename = $file->{filename};
my $lcfilename = lc($file->{filename});
my $_temp_thumb = $filename;
use integer;
$key = "t." if (!defined($key));
# x = width, y = height
my ($xold, $yold) = imgsize(qq!$data_path/$file->{thread}/$file->{filename}!);
my $x = $xold;
my $y = $yold;
my $ydiff = 0;
my $xdiff = 0;
my @convert_args;
if ($x != $y) # we must crop
{
if ($x < $y)
{
push @convert_args, "-crop", $x."x".$x;
$y = $x;
}
elsif ($y < $x)
{
$xdiff = ($x - $y) / 2;
push @convert_args, "-crop", $y."x".$y.'+'.$xdiff;
$x = $y;
}
$_temp_thumb = ".thumb/temporary.jpg";
push @convert_args, "$data_path/$file->{thread}/$filename";
push @convert_args, "$data_path/$file->{thread}/$_temp_thumb";
print "\n$filename -> .thumb/$key$lcfilename\n" if ($DEBUG);
print "old size: $xold x $yold\n" if ($DEBUG);
print "new size: $x x $y\n" if ($DEBUG);
if ($DEBUG)
{ print join " ", @convert_args; print "\n"; }
system($CONVERT_PATH, @convert_args);
for ($i = 0; $i < 4; $i++)
{
my $_temp_thumb_cropped = $_temp_thumb;
$_temp_thumb_cropped =~ s/.jpg$/-$i.jpg/;
($xold, $yold) = imgsize(qq!$data_path/$file->{thread}/$_temp_thumb_cropped!);
if ($xold == $x)
{
$_temp_thumb = $_temp_thumb_cropped;
last;
}
}
}
@convert_args = ();
push @convert_args, "-geometry";
push @convert_args, $maxwidth."x".$maxwidth;
push @convert_args, "$data_path/$file->{thread}/$_temp_thumb";
push @convert_args, "$data_path/$file->{thread}/.thumb/$key$lcfilename";
if ($DEBUG)
{ print join " ", @convert_args; print "\n"; }
system($CONVERT_PATH, @convert_args);
system($CHMOD_PATH, 755, "$data_path/$file->{thread}/.thumb/$key$lcfilename");
system($RM_PATH, "$data_path/$file->{parent_id}/.thumb/temporary.jpg");
system($RM_PATH, "$data_path/$file->{parent_id}/.thumb/temporary-0.jpg");
system($RM_PATH, "$data_path/$file->{parent_id}/.thumb/temporary-1.jpg");
system($RM_PATH, "$data_path/$file->{parent_id}/.thumb/temporary-2.jpg");
}
sub print_image_thumb
{
my ($file, $thumb_token, $string) = @_;
my $lcfilename = thumbnail_filename($file->{filename});
$thumb_token = "t." if (!defined($thumb_token));
print qq!<td align=center valign=middle>!;
my $keyword = $file->{keyword} || "details";
if ( $keyword == -1 )
{ $keyword = "details"; }
if ($string == -1)
{ print qq!<a href="$BUCKY/!.details_link().qq!/$file->{thread}">!; }
else
{ print qq!<a href="$live_path/$file->{thread}/$file->{filename}">!; }
print qq!<img class="thumb" border=0 src="$live_path/$file->{thread}/.thumb/$thumb_token!.$lcfilename.qq!">!;
print qq(</a>);
if ($string != -1)
{
print qq(<br><small>);
if (length($file->{title}))
{ print $file->{title}; }
else
{ print clean_image_filename($file->{filename}); }
print qq! (! . profile_link($file->{username}) . qq!, ! . get_age($file->{date}) . qq!)!;
print qq(</small>);
}
print qq!</a>!;
print qq!</td>!;
}
sub clean_image_filename
{
my ($filename) = @_;
return if ($filename =~ /IMG|DSC/);
$filename =~ s/[-_]/ /g;
$filename =~ s/\....$//;
return $filename;
}
sub curt_filename
{
my ($fn) = @_;
if (length($fn->{filename}) > 27 && $fn->{filename} !~ / /)
{
my $filen = substr $fn->{filename}, 0, 24;
my $filext = substr $fn->{filename}, -4, 4;
return "$filen..$filext";
}
else
{ return $fn->{filename}; }
}
sub print_flagged_jpeg
{
my ($f) = @_;
my $lcfilename = thumbnail_filename($f->{filename});
make_image_thumb(
{
file => $f,
maxwidth => 390,
# maxheight => 305,
key => "s."
} ) if (! -e qq!$data_path/$f->{thread}/.thumb/s.$lcfilename!);
print qq!<a href="$live_path/$f->{thread}/$f->{filename}"><img src="$live_path/$f->{thread}/.thumb/s.!.$lcfilename.qq!" class="thumb"></a>!;
}
#######################################################################################
# image gallery building
sub get_profile_image
{
my ($username, $prefix) = @_;
if (defined($prefix))
{
if (-e $data_path."/profile/.thumb/$prefix$username.jpg")
{ qq($live_path/profile/.thumb/$prefix$username.jpg) }
else
{ qq($live_path/profile/.thumb/$prefix)."default".qq(.jpg) }
}
elsif (-e $data_path."/profile/$username.jpg")
{ qq($live_path/profile/$username.jpg) }
else
{ -1 }
}
sub image_gallery
{
my ($files, $flagged, $many_jpgs) = @_;
my $i = 0;
my $j = 0;
print "<table border=0 cellpadding=3 cellspacing=0 width=100%>";
foreach my $f (sort { $a->{date} <=> $b->{date} } @$files)
{
next if ($f->{filename} !~ /(jpe?g|gif|png|bmp|tif)$/i);
next if (($f->{id} == $flagged->{id}) && ($many_jpgs % 3) == 1);
my $lcfilename = thumbnail_filename($f->{filename});
make_image_thumb( { file => $f, maxwidth => 145, maxheight => 120, key => "t." })
if (! -e qq($data_path/$f->{thread}/.thumb/t.).$lcfilename);
make_image_thumb( { file => $f, maxwidth => 210, maxheight => 110, key => "b." })
if (! -e qq($data_path/$f->{thread}/.thumb/b.).$lcfilename);
print "<tr>\n" if ($j == 0);
print_image_thumb($f, "t.");
print "</tr>\n" if ($j == 2);
$j = $j == 2 ? 0 : $j+1;
$i++;
}
print qq!</table>\n!;
return $i;
}
sub image_column
{
my ($files, $flagged, $many_jpgs) = @_;
my $i = 0;
my $j = 0;
if ($many_jpgs > 5)
{ $thumb_token = "t."; }
else
{ $thumb_token = "b."; }
print qq(<table border=0 cellpadding=0 cellspacing=0>);
foreach my $f (sort { $a->{date} <=> $b->{date} } @$files)
{
next if ($f->{filename} !~ /(jpe?g|gif|png|bmp|tif)$/i);
next if ($f->{id} == $flagged->{id});
my $lcfilename = thumbnail_filename($f->{filename});
make_image_thumb( { file => $f, maxwidth => 145, maxheight => 120, key => "t." })
if (! -e qq($data_path/$f->{thread}/.thumb/t.).$lcfilename);
make_image_thumb( { file => $f, maxwidth => 210, maxheight => 110, key => "b." })
if (! -e qq($data_path/$f->{thread}/.thumb/b.).$lcfilename);
if ($many_jpgs-1 == 4) # tile in a square
{
if ($i % 2 == 0)
{
print qq(<tr>\n);
print_image_thumb($f, $thumb_token);
}
else
{
print_image_thumb($f, $thumb_token);
print qq(</tr>\n);
}
}
else
{
print qq(<tr>\n);
print_image_thumb($f, $thumb_token);
print qq(</tr>\n);
}
$i++;
}
print qq!</table>\n!;
return $i;
}
sub index_photostream
{
my ($keyword,$tag) = @_;
if (2 != 1)
{
photostream({ recent => 1, vertical => 0, count => $INDEX_GALLERY_IMAGE_COUNT });
return;
}
else {
print "<table border=0 cellpadding=3 cellspacing=0 width=100%>";
$i = 4;
print <<__CROWS__;
<td align=center valign=middle><a href="/cgi-bin/bucky/details/246"><img class="thumb" border=0 src="/bucky/data/246/.thumb/t.dsc_0253.jpg"></a></a></td>
<td align=center valign=middle><a href="/cgi-bin/bucky/details/246"><img class="thumb" border=0 src="/bucky/data/246/.thumb/t.dsc_0265.jpg"></a></a></td>
<td align=center valign=middle><a href="/cgi-bin/bucky/details/246"><img class="thumb" border=0 src="/bucky/data/246/.thumb/t.dsc_0240.jpg"></a></a></td>
<td align=center valign=middle><a href="/cgi-bin/bucky/details/246"><img class="thumb" border=0 src="/bucky/data/246/.thumb/t.dsc_0234.jpg"></a></a></td>
__CROWS__
print "</table>\n";
return;
}
if (check_key($USER->{boxes}, "photostream") || ($USER == -1) )
{
if ( $keyword ne "all" )
{ photostream({ keyword => $keyword, vertical => 0, count => 4 }); }
elsif ( $tag )
{ photostream({ tag => $tag, vertical => 0, count => 4 }); }
else
{ photostream({ user => 1, vertical => 0, count => 4 }); }
}
}
sub photostream
{
my ($args) = @_;
my $vertical = $args->{vertical};
my $count = $args->{count};
my $user = $args->{user};
my $recent = $args->{recent};
my $keyword = $args->{keyword};
my $tag = $args->{tag};
if ( $recent )
{
$args->{files} = get_recent_files();
recent_image_gallery( $args );
}
elsif ( $keyword )
{
# TODO: privacy check
$args->{files} = get_keyword_files( $keyword );
if ($args->{files} != -1)
{
user_image_gallery( $args );
}
}
elsif ( $tag )
{
$args->{files} = get_tag_files( $tag );
if ($args->{files} != -1)
{
user_image_gallery( $args );
}
}
else
{
for (my $i = 0; $i < 3; $i++)
{
my $username = get_random_user();
$username = $BUCKY_ADMINISTRATOR if ($i == 2);
my ($count, $size) = count_user_files($username);
next unless ($count > 3);
$args->{files} = get_user_files( $username );
if ($args->{files} != -1)
{
user_image_gallery( $args );
return;
}
}
}
}
sub recent_image_gallery
{
my ($args) = @_;
my $files = $args->{files};
my $count = $args->{count} || 4;
my %seen;
print qq(<table border=0 cellpadding=0 cellspacing=0 width="100%" style='margin:0 0 10px 0'>);
print "<tr>";
my $i = 0;
for my $f (@$files)
{
if (lc($f->{filename}) =~ /(jpe?g|gif|png)$/) {
if (! -e qq($data_path/$f->{thread}/.thumb/t.).lc($f->{filename})) {
make_image_thumb( { file => $f, maxwidth => 145, maxheight => 120, key => "t." })
}
print_image_thumb($f, "t.", -1);
$i++;
last if ($i == $count);
}
}
print "</tr>" if (!$vertical);
print qq!</table>\n!;
}
sub user_image_gallery
{
my ($args) = @_;
my $files = $args->{files};
my $vertical = $args->{vertical} ? 1 : 0;
my $count = $args->{count} || 4;
my %seen;
print qq(<table border=0 cellpadding=3 cellspacing=3 width="100%">);
print "<tr>" if (!$vertical);
for (my $i = 0; $i < $count; $i++)
{
my $f = get_random_image($files, \%seen, $i);
last if ($f == -1);
make_image_thumb( { file => $f, maxwidth => 145, maxheight => 120, key => "t." })
if (! -e qq($data_path/$f->{thread}/.thumb/t.).lc($f->{filename}));
print "<tr>" if ($vertical);
print_image_thumb($f, "t.", -1);
print "</tr>" if ($vertical);
print "\n";
}
print "</tr>" if (!$vertical);
print qq!</table>\n!;
}
sub get_random_image
{
my ($files, $seen, $i) = @_;
my $f;
$c = 0;
while ($f = $files->[(int rand @$files)])
{
return -1 if ((++$c) == 69);
print "$i/$c: $f->{id} -- $f->{private} -- $seen->{$f->{id}}<br>" if ($DEBUG);
next if (exists($seen->{$f->{id}}));
$seen->{$f->{id}} = 1;
next if ($f->{filename} !~ /(jpe?g|gif|png|bmp|tif)$/i);
last if ( ( $USER == -1 && $f->{private} > 0 )
|| ( ref($USER) && $f->{private} > 1 ) );
last;
}
return $f;
}
sub find_jpeg
{
my ($files, $flagged_id) = @_;
my $flagged = -1;
my $fagid = -1;
my $i = 0;
my $j = 0;
foreach my $f (@$files)
{
if ($$f{filename} =~ /(jpe?g|gif|png|bmp|tif)$/i)
{
if ($$f{id} == $flagged_id || $flagged == -1)
{
$fagid = $$f{id};
$flagged = $i;
}
$j++;
}
$i++;
}
if ($flagged > -1)
{
print_flagged_jpeg($files->[$flagged])
}
return ($j, $fagid);
}
sub find_jpeg_v2
{
my ($files, $flagged_id) = @_;
my $flagged = -1;
my $fagid = -1;
my $i = 0;
my $j = 0;
foreach my $f (@$files)
{
if ($$f{filename} =~ /(jpe?g|gif|png|bmp|tif)$/i)
{
if ($$f{id} == $flagged_id || $flagged == -1)
{
$fagid = $$f{id};
$flagged = $i;
}
$j++;
}
$i++;
}
if ($flagged > -1)
{ return ($j, $files->[$flagged]); }
else
{ return ($j, -1); }
}
1;
|