Leser: 24
$gd->font('./res/verdana.ttf');
$gd->font('Verdana');
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
sub generate_image { require GD::Simple; my ($email, $settings) = @_; my $image_width = get_img_width($email); my $gd_img = GD::Simple->new($image_width,15,1); my $ttf = catfile( $FindBin::RealBin, 'res', 'INFECTED.ttf' ); $gd_img->font($ttf); $gd_img->transparent('white'); $gd_img->moveTo(3,11); $gd_img->fgcolor('blue'); #dimgray $gd_img->fontsize(10); $gd_img->string($email); return $gd_img->png(); }
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
#!/usr/bin/perl # vi:ts=4 sw=4 et: use strict; use warnings; # Pfad zu System's GD::Simple einbinden use lib qw( /usr/lib/perl5/5.10.1/i686-linux); use GD::Simple; use File::Spec::Functions qw( catfile ); use File::Basename qw( basename ); #> global variables #> ---------------------------------------------------------------------------- # in diesen Verzeichnis wird rekursiv nach .ttf Dateien gesucht my @font_dirs = qw( /usr/share/fonts /home/webster ); # Hiermit werden nur definierte Dateien aus den Treffern verwendet my @font_files = qw( arial.ttf verdana.ttf INFECTED.ttf ); my $image_file = '/tmp/gd-simple.png'; my $text = 'Franz rast im verwahrlosten Taxi durch die Stadt.'; my ( $x, $y ) = ( 20, 20 ); my $fontsize = 14; my $gd; my @fonts; #> sub routines #> ---------------------------------------------------------------------------- sub _fetch_fonts { my ( $dirsR, $fontsR ) = @_; my @wanted = @$fontsR; # quickhack; abs. Pfade zu *.ttf ermitteln; my @fonts = qx{ /usr/bin/find @$dirsR -iname "*.ttf" }; chomp @fonts; # soll's was bestimmtes sein? if ( @wanted ) { my %seen; @seen{@wanted} = (); # suchen, unerwuenschtes rauswerfen @fonts = grep { exists $seen{ basename( $_ ) } } @fonts; } die "(E) no fonts found/matched...\n" if !@fonts; return @fonts; } sub init { @fonts = _fetch_fonts( \@font_dirs, \@font_files ); $gd = GD::Simple->new(800, $fontsize * 3 * @fonts ); $gd->fontsize($fontsize); } sub fill_image { my $i = 0; for my $font ( undef, @fonts ) { # erste Ausgabe mit Standard-Font $gd->font( $font ) if 0 != $i; # Dateipfad als Ausgabe; vordefinierter Text für Standard-Font my $str = sprintf "%3d: %s", $i++, $font || $text ; # Positionieren $gd->moveTo( $x, $y ); $y += int($fontsize*1.5 + 0.5); # Debug output auf STDERR warn "$str\n"; $gd->string( $str ) or warn "->string() failed: $@\n"; } } sub write_file { my ( $file ) = @_; # schreibe Datei open my $wh, '>', $file or die "open $file: $!\n"; print $wh $gd->png or die "write to $file: $!\n"; close $wh or die "close $file: $!\n"; } #> main script #> ---------------------------------------------------------------------------- init(); fill_image(); write_file( $image_file ); __END__
QuoteQuelle: http://search.cpan.org/dist/GD/GD/Simple.pm#Method...$img->string($string)
(…)
This method returns a polygon indicating the bounding box of the rendered text. If an error occurred (such as invalid font specification) it returns undef and an error message in $@.
QuoteDas tuts aber nicht. (verdana.ttf befindet sich in einem Ordner res relativ zum Script).
1 2 3 4
use FindBin; use File::Spec::Functions; my $ttf = file( $FindBin::RealBin, 'res', 'verdana.ttf' );