img.pl
250 lines of code
1
#!/usr/bin/perl
2
3
use strict;
4
#use warnings;
5
use CGI::Carp qw(fatalsToBrowser);
6
use GD;
7
8
binmode STDOUT;
9
GD::Image->trueColor(1);
10
11
############################################################################################
12
############################################################################################
13
############################################################################################
14
15
use lib "/var/www/html/Pm";
16
17
use Bc_misc;
18
use Bc_sql;
19
use Bc_dir;
20
use Html;
21
use Security;
22
23
Security::count_hits();
24
25
my $DEBUG = 0;
26
27
my $f = Bc_misc::get_param("i"); # filename
28
29
my $gs = Bc_misc::get_param("gs"); # toggle greyscale on/off
30
my $s = Bc_misc::get_param("s"); # resize image (ss, s, u, i, logo, t, dp)
31
if (not $s) { $s = ""; }
32
# $s can be:
33
#   ss = smaller than small (12x12)
34
#   s = small (14x14)
35
#   u = user? (16x16)
36
#   i = icon (24x24)
37
#   dbl = double icon size (48x48)
38
#   t = thumbnailed (125x75)
39
#   dp = display picture size (0x65)
40
#   dpm = display picture size mini (0x32)
41
#   logo = logo size (0x40)
42
# else
43
#   returns image at actual size
44
45
my $wm = Bc_misc::get_param("wm"); # insert a little 'watermark' @ top right (overrides $wmi)
46
my $wmi = Bc_misc::get_param("wmi"); # insert an image @ top right (overrides $wm)
47
my $wm4 = Bc_misc::get_param("wm4"); # insert another kind of little 'watermark' @ top right
48
# $wm4 will be a comma separated list but, the numbers will be used to determine the following:
49
#   gender
50
#   subscription level
51
#   security level
52
#   banned status
53
# and the correct images will be applied as a watermark
54
55
my $graphic;
56
my $share = "/var/www/html/images/";
57
if ($f =~ /^\//) { $share = ""; }
58
my $baseimgshare = "/var/www/html/images/";
59
my $prepend = "";
60
61
$f = $share . $prepend . $f;
62
63
if ($DEBUG) {
64
  print Html::pre_html_header({type=>"text/plain", skipmaintcheck=>1});
65
66
  print "DEBUG MODE:\n";
67
  print "f: $f\n";
68
  print "s: $s (";
69
  if ($s eq "ss")      { print "12, 12"; }    # smaller than small
70
  elsif ($s eq "s")    { print "14, 14"; }    # small
71
  elsif ($s eq "u")    { print "16, 16"; }    # ?? not sure what U stands for.  user??
72
  elsif ($s eq "i")    { print "24, 24"; }    # icon
73
  elsif ($s eq "dbl")  { print "48, 48"; }    # double icon size
74
  elsif ($s eq "logo") { print "0, 40"; }     # logo
75
  elsif ($s eq "t")    { print "125, 75"; }   # thumbnail
76
  elsif ($s eq "dp")   { print "0, 64"; }     # display pic
77
  elsif ($s eq "dpm")  { print "0, 32"; }     # display pic
78
  else                 { print "full size"; } # this SHOULD size the image to full size
79
  print ")\n";
80
  print "wm: $wm\n";
81
  print "wm4: $wm4\n";
82
  $graphic = resize($f, 0, 0); # this loads the fullsized version, to ensure we find the file and can read it
83
  if ($graphic) {
84
    print "got a graphic! " . $graphic->width . " x " . $graphic->height;
85
  } else {
86
    print "did not get a graphic!";
87
  }
88
} else {
89
  # now, see if the file exists or not
90
  if (Bc_dir::file_exists($f)) {
91
    # it exists, now "size" it according to $s
92
    # and should respect the image's aspect ratio
93
    if ($s eq "ss")      { $graphic = resize($f, 12, 12); } # smaller than small
94
    elsif ($s eq "s")    { $graphic = resize($f, 14, 14); } # small
95
    elsif ($s eq "u")    { $graphic = resize($f, 16, 16); } # ?? not sure what U stands for.  user??
96
    elsif ($s eq "i")    { $graphic = resize($f, 24, 24); } # icon
97
    elsif ($s eq "logo") { $graphic = resize($f, 40, 40); } # logo
98
    elsif ($s eq "t")    { $graphic = resize($f, 125, 75); } # thumbnail
99
    elsif ($s eq "dp")   { $graphic = resize($f, 65, 65); } # display pic
100
    elsif ($s eq "dpm")  { $graphic = resize($f, 32, 32); } # display pic
101
    else                 { $graphic = resize($f, 0, 0); } # this SHOULD size the image to full size
102
103
    if ($wm and not $wmi) {
104
      my @wms = split(",", $wm);
105
      if (@wms) {
106
        my ($w, $h) = $graphic->getBounds;
107
        my $posy = 1;
108
        my $numLen = 0;
109
110
        # now we got the first top right position
111
        # plot the watermarks
112
        foreach my $wm_item (@wms) {
113
          my $posx = $w - 7;
114
          my $ocenterx = 2;
115
          my $ocentery = 4;
116
          my $owidth = 10;
117
118
          if ($wm_item ne "0" and $wm_item ne "") {
119
            if ($wm_item > 99) {
120
              $wm_item = "99+";
121
              $posx -= 9;
122
              $ocenterx += 4;
123
              $owidth += 9;
124
            } else { # wm_item 99 or less (or maybe it's text?)
125
              if ($wm_item) {
126
                if ($wm_item >= 10 and $wm_item ne "99+") {
127
                  $posx -= 5;
128
                  $ocenterx += 3;
129
                  $owidth += 3;
130
                }
131
              }
132
            } # end if $wm_item > 99 else
133
134
            my $white = $graphic->colorAllocate(255, 255, 255);
135
            my $darkred = $graphic->colorAllocate(200, 0, 0);
136
            my $red = $graphic->colorAllocate(255, 0, 0);
137
            $graphic->arc($posx+$ocenterx, $posy+$ocentery, $owidth, 10, 0, 360, $darkred);
138
            $graphic->fillToBorder($posx+2, $posy+4, $darkred, $red);
139
            $graphic->string(gdTinyFont, $posx, $posy, $wm_item, $white);
140
            $posy += 13;
141
          } # end if wm_item ne 0
142
        } # end foreach
143
      } # end if @wms
144
    } # end if $wm
145
146
    if ($wm4) {
147
      my @wms = split(",", $wm4);
148
      if (@wms) {
149
        my ($sex, $sec, $paid, $ban);
150
        my $hw = 12;
151
        if ($wms[0] eq 1)
152
          { $sex = resize($baseimgshare . "site/male.png", $hw, $hw); } else
153
          { $sex = resize($baseimgshare . "site/female.png", $hw, $hw); }
154
155
        if ($wms[1] eq get_security("superadministrator"))
156
          { $sec = resize($baseimgshare . "site/sec_superadmin.png", $hw, $hw); }
157
        elsif ($wms[1] eq get_security("administrator"))
158
          { $sec = resize($baseimgshare . "site/sec_admin.png", $hw, $hw); }
159
        elsif ($wms[1] eq get_security("moderator"))
160
          { $sec = resize($baseimgshare . "site/sec_mod.png", $hw, $hw); }
161
        else
162
          { $sec = resize($baseimgshare . "site/sec_user.png", $hw, $hw); }
163
164
        if ($wms[2] eq '4')
165
          { $paid = resize($baseimgshare . "site/paid-vip.png", $hw, $hw); }
166
        elsif ($wms[2] eq '3')
167
          { $paid = resize($baseimgshare . "site/paid-yr.png", $hw, $hw); }
168
        elsif ($wms[2] eq '2')
169
          { $paid = resize($baseimgshare . "site/paid-mm.png", $hw, $hw); }
170
        else
171
          { $paid = resize($baseimgshare . "site/paid-no.png", $hw, $hw); }
172
173
        if ($wms[3] eq 1)
174
          { $ban = resize($baseimgshare . "site/bullet_delete.png", $hw, $hw); }
175
          { $ban = resize($baseimgshare . "misc/ok.png", $hw, $hw); }
176
177
        # now, insert the "icons"
178
        # sec, paid, ban, sex
179
        if ($sec) { $graphic->copy($sec, $graphic->width - $sec->width - 1, 1, 0, 0, $sec->width, $sec->height); }
180
        if ($paid) { $graphic->copy($paid, $graphic->width - $paid->width - 1, 14, 0, 0, $paid->width, $paid->height); }
181
        if ($ban) { $graphic->copy($ban, $graphic->width - $ban->width - 1, 28, 0, 0, $ban->width, $ban->height); }
182
        if ($sex) { $graphic->copy($sex, $graphic->width - $sex->width - 1, 42, 0, 0, $sex->width, $sex->height); }
183
      } # end if @wms
184
    } # end if wm4
185
186
    if ($wmi and not $wm) {
187
      my ($w, $h) = $graphic->getBounds;
188
      my $hw = 0;
189
      if ($s eq "ss")      { $hw = 6; } # smaller than small
190
      elsif ($s eq "s")    { $hw = 7; } # small
191
      elsif ($s eq "u")    { $hw = 8; } # ?? not sure what U stands for.  user??
192
      elsif ($s eq "i")    { $hw = 10; } # icon
193
      elsif ($s eq "logo") { $hw = 20; } # logo
194
      elsif ($s eq "t")    { $hw = 37; } # thumbnail
195
      elsif ($s eq "dp")   { $hw = 32; } # display pic
196
      else                 { $hw = 12; } # this SHOULD size the image to full size
197
198
      my $posy = 1;
199
      my $numLen = 0;
200
      my $img = resize($baseimgshare . "/" . $wmi, $hw, $hw);
201
      $graphic->copy($img, $graphic->width - $img->width - 1, 1, 0, 0, $img->width, $img->height);
202
    } # end if wmi
203
204
    print Html::pre_html_header({type=>"image/png", skipmaintcheck=>1});
205
    print $graphic->png;
206
  } else { # ...file does not exist
207
    my $img = resize($baseimgshare . "/site/broken_img.png", 24, 24);
208
    #print "cache-control: no-cache, no-store\n";
209
    #print "content-type: image/png\n\n";
210
    print Html::pre_html_header({
211
                           "type"=>"image/png",
212
                           "cache-control"=>"no-cache, no-store",
213
                           "skipmaintcheck"=>1
214
                          }, "", $img->png);
215
    print $img->png;
216
  }
217
}
218
219
#########################
220
221
222
exit 1;
223
224
############################################################################################
225
############################################################################################
226
############################################################################################
227
228
sub resize {
229
  my ($inputfile, $width, $height) = @_;
230
  my $gdo = GD::Image->new($inputfile);
231
  if (not $gdo) { return 0; }
232
233
  if (not $width) { $width = $gdo->width; }
234
  if (not $height) { $height = $gdo->height; }
235
236
  my $k_h = $height / $gdo->height;
237
  my $k_w = $width / $gdo->width;
238
  my $k   = ($k_h < $k_w ? $k_h : $k_w);
239
  $height = int($gdo->height * $k);
240
  $width  = int($gdo->width * $k);
241
242
  my $image = GD::Image->new($width, $height);
243
  $image->alphaBlending(0);
244
  $image->saveAlpha(1);
245
  $image->copyResampled($gdo, 0, 0, 0, 0, $width, $height, $gdo->width, $gdo->height);
246
247
  if ($gs) { $image->grayscale(); }
248
249
  return $image;
250
}