Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

A new type of quote script

by mousey (Scribe)
on Oct 02, 2002 at 16:47 UTC ( #202317=CUFP: print w/replies, xml ) Need Help??

Want a new quote script? This will output a picture of a quote in a quote file with quotes on seperate lines. It will even auto-size itself to the quote.
#!/usr/bin/perl use CGI::Carp; use GD; my $quote = &getquote; my $img = GD::Image->new(length($quote)*8+30,46); my $white = $img->colorAllocate(255,255,255); my $black = $img->colorAllocate(0,0,0); $img->transparent($white); $img->string(gdLargeFont,15,15,$quote,$black); print "Content-type: image/jpeg\n\n"; binmode(STDOUT); print STDOUT $img->jpeg; sub getquote { open FILE, 'quotes.txt' or die "Can't open quotes.txt: $!\n"; my @quotes = <FILE>; close FILE; chomp(@quotes); my $quote = $quotes[rand @quotes]; return $quote; }
update: made the script much faster by having it output in jpeg format.

Replies are listed 'Best First'.
Re: A new type of quote script
by PodMaster (Abbot) on Oct 03, 2002 at 10:44 UTC
    I wrote one too (once upon a time) whilst learning about GD, but I was just reading off *DATA
    #!C:/perl/bin/perl #!/usr/bin/perl BEGIN { unless( -t STDIN){ # if run on commandline, none of this require CGI::Carp; CGI::Carp::carpout(\*LOGGY) if open(LOGGY,'>>'.__FILE__.'.err.log'); CGI::Carp->import( 'fatalsToBrowser' ); } } use strict; use warnings; use GD; use constant PI => 3.141519; # close enough use GD::Text::Wrap; use GD::Text::Align; my $quote = [ grep { $_ } map { chomp; $_ } <DATA> ]; $quote = $$quote[rand @$quote]; my $author = substr $quote, rindex($quote,'--'), length($quote) -rindex($quote, '--'), ''; #my $gd = GD::Image->new(800,600); my $gd = GD::Image->new(800,600); $gd->colorAllocate(255, 255, 255); my $black = $gd->colorAllocate(0, 0, 0); my $red = $gd->colorAllocate(255, 0, 0); my $blue = $gd->colorAllocate(0, 0, 255); #$gd->transparent($red); $gd->interlaced('true'); $quote = GD::Text::Wrap->new( $gd, width => 800, # has no effect (a bug) line_space => 4, color => $black, text => $quote, preserve_nl => 1, ); $quote->set_font(gdLargeFont); $quote->set(align => 'center', width => 12 * 30); $quote->draw(10,10); $author = GD::Text::Align->new($gd, valign => 'base', halign => 'right', text => $author, ); $author->set_font(gdLargeFont); # STUPID API, always have to pass x,y, otherwise its undef my($left, $top, $right, $bottom) = $quote->get_bounds(10,10); $author->draw( $right , $bottom + 20, PI, $blue); $gd->rectangle( $left -10, $top -10, $right +10, $bottom + 35 , $red); # resize the image (even if it don't affect file-size much for png's) my $gd2 = GD::Image->new( $right + 12, $bottom + 37 ); #$image->copy(sourceImage,dstX,dstY,srcX,srcY,width,height) object $gd2->copy( $gd, 0,0,0,0, $right + 12, $bottom + 37 ); print qq{Expires: Thu, 01 Jan 1970 00:00:00 GMT Date: Fri, 02 Jan 1970 00:00:00 GMT Content-Type: Image/Png\r\n\r\n}; binmode(STDOUT); print $gd2->png(); __END__ A good way to threaten somebody is to light a stick of dynamite. Then + you call the guy and hold the burning fuse up to the phone. "Hear th +at?" you say. "That's dynamite, baby." -- Deep Thoughts by Jack Hande +y (SNL) Who ate my stick of butter?! -- butterman Once while walking through the mall a guy came up to me and said "Hey +, how's it going?". So I grabbed his arm and twisted it up behind his + head and said "Now whose asking the questions?" -- Deep Thoughts by +Jack Handey (SNL) The memories of my family outings are still a source of strength to me +. I remember we'd all pile into the car - I forget what kind it was - + and drive and drive. I'm not sure where we'd go, but I think there w +ere some trees there. The smell of something was strong in the air as + we played whatever sport we played. I remember a bigger, older guy w +e called "Dad." We'd eat some stuff, or not, and then I think we went + home. I guess some things never leave you. -- Deep Thoughts by Jack +Handey (SNL) Marta says the interesting thing about fly-fishing is that it's two l +ives connected by a thin strand. Come on, Marta. Grow up. -- Deep Tho +ughts by Jack Handey (SNL)

    ** The Third rule of perl club is a statement of fact: pod is sexy.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://202317]
Approved by ignatz
Front-paged by jarich
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2017-05-30 05:51 GMT
Find Nodes?
    Voting Booth?