动态生成的GIF是OSX High Sierra和Safari

时间:2017-11-25 19:36:07

标签: macos perl safari gif macos-high-sierra

我有一个Perl函数可以动态生成验证码作为GIF图像。这是由此函数生成的实际GIF文件...

GIF file

在High Sierra的最新Safari(和Safari技术预览版)v12(Mac OS X版本10.13.x)中,同样的GIF显示为损坏的图像图标:

enter image description here

它也不会在High Sierra的预览应用程序版本中显示,也不会在"预览"中显示。 "获取信息"窗口。

这个Perl已经存在多年,到目前为止,已经在所有浏览器上工作。 High Sierra中的Firefox和Chrome在正确显示此GIF方面没有任何问题。

的Perl:

sub image {
  my ($key) = @_;
  $key =~ s/\D//g;
  my $buff;
  my $bpx = 7;
  my $pal = "\x33\x33\x33" .
            "\xff\xff\xff" .
            "\x66\x66\x66" .
            "\x00\x00\x00";

  initcmx();

  my $dh = int(length($dot) / $dw);
  my $ch = int(length($cmx{0}) / $cw);
  my $len = length($key);
  my $w = $len * $cw * $dw;
  my $h = $ch * $dh;
  my $blmt = 2**$bpx - 3;

# GIF header
  $buff .= 'GIF87a';
  $buff .= pack 'v2', $w, $h;
  $buff .= pack 'C', 0xF0 + $bpx - 1;
  $buff .= "\0";
  $buff .= "\0";
  $buff .= $pal;
  $buff .= "\0" x ((2**$bpx * 3) - length $pal);
  $buff .= ',';
  $buff .= "\0\0\0\0";
  $buff .= pack 'v2', $w, $h;
  $buff .= "\0";
  $buff .= pack 'C', $bpx;
#
# Build image
#
  my $img;
  for (my $y = 0; $y < $h; $y++) {
    my $cy = int($y / $dh) % $ch;
    my $dy = $y % $dh;
    for (my $x = 0; $x < $w; $x += $dw) {
      my $cx = int($x / $dw) % $cw;
      my $i = int($x / $dw / $cw);
      my $c = substr $key, $i, 1;
      my $d = substr $cmx{$c}, $cy * $cw + $cx, 1;
      my $di = ($d eq 'X') ? $dot : $nodot;
      $di = substr $di, $dy * $dw, $dw;
      for ($i = 0; $i < length $di; $i++) {
        $c = ord substr $di, $i, 1;
        $c = chr $c;
        $img .= $c;
      }
    }
  }
#
# Pack image
#
  my $cnt;
  for (my $i = 0; $i < length $img; $i += $cnt) {
    $cnt = (length $img) - $i;
    if ($cnt > $blmt) {
      $cnt = $blmt;
    }
    $buff .= pack 'C', $cnt + 1;
    $buff .= substr $img, $i, $cnt;
    $buff .= pack 'C', 2**$bpx;
  }
  $buff .= pack 'C', 2**$bpx + 1;
  $buff .= "\0";
  $buff .= ';';
  return "Content-type: image/gif\nExpires: Wed, 05-Jan-2007 01:01:01 GMT\n\n".$buff;
}
# ====================================================================== initcmx
#
# Quick and dirty solution. The matrix must be loadable
#

sub initcmx {

  $cw = 6;

$cmx{'0'} = qq~
......
.XXX..
X...X.
X...X.
X...X.
X...X.
X...X.
.XXX..
......
~;
$cmx{'1'} = qq~
..X...
.XX...
..X...
..X...
..X...
..X...
.XXX..
......
......
~;
if (rand() < 0.5) {
$cmx{'1'} = qq~
......
...X..
..XX..
...X..
...X..
...X..
...X..
...X..
..XXX.
~;
}
$cmx{'2'} = qq~
......
......
.XXX..
X...X.
....X.
..XX..
.X....
X.....
XXXXX.
~;
if (rand() < 0.3) {
$cmx{'2'} = qq~
.XXX..
X...X.
....X.
..XX..
.X....
X.....
XXXXX.
......
......
~;
}
if (rand() > 0.7) {
$cmx{'2'} = qq~
......
XXXX..
....X.
....X.
.XXX..
X.....
X.....
XXXXX.
......
~;
}
$cmx{'3'} = qq~
XXXXX.
....X.
...X..
..XX..
....X.
X...X.
.XXX..
......
......
~;
$cmx{'4'} = qq~
......
...X..
..XX..
.X.X..
X..X..
XXXXX.
...X..
...X..
......
~;
if (rand() < 0.5) {
$cmx{'4'} = qq~
....X.
...XX.
..X.X.
.X..X.
XXXXX.
....X.
....X.
......
......
~;
}
$cmx{'5'} = qq~
......
......
XXXXX.
X.....
XXXX..
....X.
....X.
X...X.
.XXX..
~;
$cmx{'6'} = qq~
..XXX.
.X....
X.....
XXXX..
X...X.
X...X.
.XXX..
......
......
~;
if (rand() < 0.5) {
$cmx{'6'} = qq~
......
......
..XXX.
.X....
X.....
XXXX..
X...X.
X...X.
.XXX..
~;
}
$cmx{'7'} = qq~
......
XXXXX.
....X.
...X..
..X...
.X....
.X....
.X....
......
~;
if (rand() < 0.5) {
$cmx{'7'} = qq~
......
......
XXXXXX
.....X
....X.
...X..
..X...
.X....
X.....
~;
}
$cmx{'8'} = qq~
.XXX..
X...X.
X...X.
.XXX..
X...X.
X...X.
.XXX..
......
......
~;
$cmx{'9'} = qq~
......
......
.XXX..
X...X.
X...X.
.XXXX.
....X.
...X..
XXX...
~;
  for (my $i = 0; $i < 10; ++$i) {
    $cmx{$i} =~ s/\n//g;
    $cmx{$i} =~ s/\r//g;
  }
  $dot = qq~
\1\2\1
\2\3\2
\1\2\1
~;
  $nodot = qq~
\0\0\0
\0\0\0
\0\0\0
~;
 $dot =~ s/\n//g;
 $dot =~ s/\r//g;
 $nodot =~ s/\n//g;
 $nodot =~ s/\r//g;
 $dw = 3;
}

High Sierra一般是否存在GIF87a问题,或者这个特定文件的生成方式有问题?谷歌搜索一直没有结果,因为大多数人只报告动画GIF的各种问题。

编辑:我重写了我的Perl以创建PNG而不是GIF文件。但是,由于我仍然对导致此问题的原因感到好奇,我还是留下了这个问题。

0 个答案:

没有答案