Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision ad048c73

Von Sven Schöling vor mehr als 9 Jahren hinzugefügt

  • ID ad048c7322ac24524dace99d1bbb09d5e14bc213
  • Vorgänger 7b24fbab
  • Nachfolger 3dfcde62

Image Maps ohne GD

GD verliert bei Palette pngs die Transparenz, und Stackoverflow findet
ad hoc nichts wie das zu fixen ist.

Unterschiede anzeigen:

scripts/image_maps.pl
1 1
#!/usr/bin/perl
2 2

  
3 3
use strict;
4
use GD;
5 4
use Getopt::Long;
6 5
use File::Basename;
7 6

  
8

  
9 7
my $css_file   = 'generated.css';
10 8
my $image_file = 'generated.png';
11 9
my $class_for_map = 'icon';
12 10

  
11
my $convert_bin  = 'convert';
12
my $identify_bin = 'identify';
13

  
13 14
GetOptions(
14 15
  'css-out=s'    => \$css_file,
15 16
  'image-out=s'  => \$image_file,
......
17 18
);
18 19

  
19 20
my @files = @ARGV;
20
my @gd_images;
21

  
22
GD::Image->trueColor(1);
21
my @images;
23 22

  
24 23
# read files
25 24

  
26
for my $filename (@files) {
27
  my $image = GD::Image->newFromPng($filename);
25
for my $filename (sort @files) {
26
   my $image = `$identify_bin $filename`;
28 27
   if (!defined $image) {
29
     warn "warning: could not load image '$filename'. skpping...";
28
     warn "warning: could not identify image '$filename'. skpping...";
30 29
     next;
31 30
   }
32
  push @gd_images, {
33
    gd       => $image,
31
  $image =~ /^(?<filename>\S+) \s (?<type>\S+) \s (?<width>\d+) x (?<height>\d+)/x;
32
  push @images, {
34 33
    filename => $filename,
34
    type     => $+{type},
35
    width    => $+{width},
36
    height   => $+{height},
35 37
  };
36 38
}
37 39

  
38 40
# make target layout
39 41
# for simplification thi will check if all the  images have the same dimensions
40 42
# and croak if not
41
my $first_height = $gd_images[0]->{gd}->height;
42
my $first_width  = $gd_images[0]->{gd}->width;
43
my $first_height = $images[0]->{height};
44
my $first_width  = $images[0]->{width};
43 45

  
44 46
use Data::Dumper;
45 47

  
46
for my $img (@gd_images) {
47
  die 'heights are not equal' if $first_height != $img->{gd}->height;
48
  die 'widths are not equal'  if $first_width  != $img->{gd}->width;
48
for my $img (@images) {
49
  die 'heights are not equal' if $first_height != $img->{height};
50
  die 'widths are not equal'  if $first_width  != $img->{width};
49 51
}
50 52

  
51 53
# all equal? nice.
52 54
# we'll be lazy and just put them all together left-to-right
53 55
my $new_height = $first_height;
54
my $new_width  = $first_width * @gd_images;
56
my $new_width  = $first_width * @images;
55 57

  
56
my $new_image = GD::Image->new($new_width, $new_height, 1);
57 58
# now copy them all together, and keep a referende to;
58 59

  
59
$new_image->saveAlpha(1);
60
$new_image->alphaBlending(0);
60
my $convert_string = "$convert_bin ";
61 61

  
62 62
my $h_offset = 0;
63
for (@gd_images) {
63
for (@images) {
64 64
  $_->{h_offset} = $h_offset;
65 65
  $_->{v_offset} = 0;
66
  $new_image->copy($_->{gd}, $_->{h_offset}, $_->{v_offset}, 0, 0, $_->{gd}->width, $_->{gd}->height);
66
  $convert_string .= ' +append ' . $_->{filename};
67 67
} continue {
68
  $h_offset += $_->{gd}->width;
68
  $h_offset += $_->{width};
69 69
}
70 70

  
71
$convert_string .= " -background none +append $image_file";
72

  
71 73
# now write that png...
72
{
73
  open my $file, '>:raw', $image_file or die "can't write to $image_file";
74
  print $file $new_image->png;
75
}
74
system($convert_string);
76 75

  
77 76
# make css file
78 77
{
79 78
  open my $file, ">", $css_file or die "can't write too $css_file";
80 79
  print $file ".$class_for_map { background: url(../$image_file) ${first_width}px 0px no-repeat; padding: 0; width: ${first_width}px; height: ${first_height}px; }\n";
81 80

  
82
  for (@gd_images) {
81
  for (@images) {
83 82
    my $name = fileparse($_->{filename}, ".png");
84 83

  
85 84
    # the full grammar for valid css class names is completely bonkers (to put it mildly).

Auch abrufbar als: Unified diff