#!/usr/bin/perl
#-------------------------------------------------------------------------------
# Copyleft (C) 2000 Motion Co., Ltd.
#
# <IMG>タグを利用してカウントを表示するCGI
#
#	用意するもの
#		*.count			: カウントを記録しておくファイル
#		*.png			: 表示する画像ファイル(0〜9を記述した一枚の画像)
#						: 画像ファイルの形式は、必ず"PNG"(Portable Network Graphics)を
#						: 使用すること。また、数字のサイズは統一すること。
#
#	使い方
#
#		HTMLの中に次のような形で記述する。
#
#		<IMG SRC="imgcnt.cgi?cnt=index&digit=6&img=counter">
#
#		パラメータの意味は、
#
#			cnt		: カウントを記録するファイル(必須項目)
#			digit	: 表示桁数
#			img		: 表示に使用する画像ファイル
#---------------------------------------------------------2000/03/30-N.Kiyotake-

#-------------------------------------------------------------------------------
#	設定	: 必要に応じて変更のこと
#-------------------------------------------------------------------------------
#	カウントファイルのあるディレクトリ
$dir		= ".";

#	カウントファイルの拡張子
$ext		= "count";

#	イメージがあるディレクトリ
$imgdir		= "./image";

#	デフォルトの桁数
$digit		= 6;

#	デフォルトの画像ファイル
$imgfile	= "counter";

#-------------------------------------------------------------------------------
#	GETのみを取得
#-------------------------------------------------------------------------------
$query = $ENV{'QUERY_STRING'};
@query = split( /&/, $query );
foreach ( @query ) {
	local( $name, $value ) = split( /=/ );
	$in{$name} = $value;
}

#-------------------------------------------------------------------------------
#	クッキーを取得
#-------------------------------------------------------------------------------
@cookie = split( /\s*;\s*/, $ENV{'HTTP_COOKIE'} );
foreach ( @cookie ) {
	( $name, $value ) = split( /=/ );
	$cookie{$name} = $value;
}

#-------------------------------------------------------------------------------
#	...?cnt=ファイル名&digit=桁数&img=画像ファイル名
#-------------------------------------------------------------------------------
$countfile = "$dir/$in{'cnt'}.count";
if ( defined( $in{'digit'} ) ) {
	$digit = $in{'digit'};
}
if ( defined( $in{'img'} ) ) {
	$imgfile = $in{'img'};
}
$countfile = "./index.count";

#-------------------------------------------------------------------------------
#	カウント
#-------------------------------------------------------------------------------
open( COUNT, "+<$countfile" ) or die "カウントファイル( $countfile )が開けません。: $!\n";
flock( COUNT, 2 );
$count = <COUNT>;
chomp( $count );

#	カウントアップを行なっていないなら、カウントする
if ( $cookie{'pngcnt'} ne 'already' ) {
	$count++;
	seek( COUNT, 0, 0 );
	print( COUNT $count );
}

flock( COUNT, 8 );
close( COUNT );

#	表示用に1文字ずつに分ける
@number = split( //, $count );
@number = reverse( @number );

#-------------------------------------------------------------------------------
#	カウント画像作成
#-------------------------------------------------------------------------------
use GD;

$point = 0;
$width = 0;
$height = 0;

#	桁数補正
if ( @number >= $digit ) {
	$digit = @number + 1;
}

#	カウンタ画像を読みこむ
open( PNG, "$imgdir/$imgfile.png" ) || die;
$img = newFromPng GD::Image(PNG) || die;
close( PNG );

#	画像サイズを取得
( $width, $height ) = $img->getBounds();
$trans = $img->transparent();
( $r, $g, $b ) = $img->rgb( $trans );
$width = $width / 10;	# 一文字の幅

#	ワークエリアを確保する
$work = new GD::Image( $width * $digit, $height );
$trans = $work->colorAllocate( $r, $g, $b );
$work->filledRectangle( 0, 0, $width * $digit-1, $height-1, $trans );
$work->transparent( $trans );
$work->interlaced( 'true' );	#	インタレース化

for( $cnt = $digit-1; $cnt >= 0; $cnt-- ) {
	#	桁の値を得る。
	$number = defined( $number[$cnt] ) ? $number[$cnt] : 0;

	#	その桁を書きこむ
	$work->copy( $img, $point, 0, $width * $number, 0, $width, $height );
	$point += $width;
}

#-------------------------------------------------------------------------------
#	画像出力
#-------------------------------------------------------------------------------
#	二重カウント防止用クッキーの出力
print( "Set-Cookie: pngcnt=already;\n" );

#	HTTPヘッダを出力
print( "Content-Type: image/png\n\n" );

#	カウントイメージを出力
print $work->png();

