#!/usr/bin/perl # gif with local time on analog clock # If you reuse the code, I would appreciate a hello through e-mail... # mi @ alma.ch use GD; $LOGO = "/home/httpd/alma/gifs/camel-prop.png"; $W = 90; # gif Width $H = 90; # gif Height $x_offset = 0; # offset for arm center from gif center $y_offset = 0; # (use if bkgnd. image visual center not at gif's real center) $C_x = $W/2 + $x_offset; # x of center $C_y = $H/2 + $y_offset; # y of center $m_len = 45; # min arm length $h_len = 34; # hour arm length $arm_width = 2.6; @arm_color = (128, 0, 0); sub rad { # degrees to radians my $x = shift; return ($x/180) * 3.1416; } sub xy_pos { # return x y from Length and Time my $L = shift; # length my $T = shift; # time $T = $T * 6; # time to degrees return ( ($L * sin(rad $T)) , ($L * cos(rad $T)) ); # return (x, y) } # create a new image from an existing gif open (GIF,$LOGO) or die "couldn't open $LOGO: $!"; binmode GIF; $im = newFromPng GD::Image(GIF) || die "couldn't create image"; close GIF || warn "couldn't close: $!"; # allocate some colors $white = $im->colorAllocate(255,255,255); $black = $im->colorAllocate(0,0,0); $red = $im->colorAllocate(255,0,0); $blue = $im->colorAllocate(0,0,255); # create brush for drawing arms $arm_style = new GD::Image($arm_width, $arm_width); $color = $arm_style->colorAllocate(@arm_color); $arm_style->line(0,2,2,0,$color); # NE diagonal # Set the brush $im->setBrush($arm_style); ($sec, $min, $hour, $z, $z, $z, $z, $z, $z) = localtime(); if ($hour > 12) {$hour -= 12}; $hour *= 5; # convert 0-12 h. to 0-60 minutes scale $hour += $min / 12; # add min. offset from exact hour. $min += $sec / 60; # add seconds offset from exact minute. ($h_x, $h_y) = xy_pos($h_len, $hour); ($m_x, $m_y) = xy_pos($m_len, $min); # translate center coordinates to GD coordinates $h_x += $C_x; $m_x += $C_x; $h_y = $C_y - $h_y; $m_y = $C_y - $m_y; $im->line($C_x,$C_y,$h_x,$h_y,gdBrushed); $im->line($C_x,$C_y,$m_x,$m_y,gdBrushed); print "Content-type: image/png\n\n"; binmode STDOUT; # Convert the image to GIF and print it on standard output print $im->png;