#!/usr/bin/perl -w # (C) 2004 F.Hauri - felix@f-hauri.ch # # Warning!!! # This script in only a prototype! # The goal of this script is to play with libGD. # # DO NOT USE ON PUBLIC SERVER!!! # # There is lot of security holes!!! # use CGI qw(:standard :html3); use CGI::Carp qw(fatalsToBrowser); use GD; my $query = new CGI; my $imagesdir="/data/picturefiles"; my $Title="Titre de l'album"; my $bgcolor="#8AB6E7"; my $owidth=640; my $oheight=480; my $twidth=70; my $theight=70; my $nbr=60; my @files; my $back; my $imgnr; sub readparam { $imgnr=$1 if path_info() =~ /\/(\d+).jpg$/i; ($owidth,$oheight)=($1,$2) if path_info() =~ /\/(\d+)x(\d+)\//i; $back=$1 if path_info() =~ /\/(\#+[a-f0-9]{6})\//i; } sub listfiles { opendir DH,$imagesdir or die "Can't open dir \`\`$imagesdir''"; @files=sort { (stat($imagesdir."/".$a))[9] <=> (stat($imagesdir."/".$b))[9] } grep {/.jpg$/i} readdir DH; closedir DH; } sub mpict { my $img1=GD::Image->newFromJpeg($imagesdir."/".$files[$imgnr]); my $img2=GD::Image->new($owidth,$oheight); my ($width,$height) = $img1->getBounds(); my $rap; $rap=$oheight/$height if $height*$owidth/$oheight >=$width; $rap=$owidth/$width if $width > $height*$owidth/$oheight; my $neww=sprintf "%.0f", $width*$rap; my $newh=sprintf "%.0f", $height*$rap; my $hoff=sprintf "%.0f", ($owidth-$neww)/2; my $voff=sprintf "%.0f", ($oheight-$newh)/2; if (defined $back) { my ($red,$gre,$blu)=(0,0,0); ($red,$gre,$blu)=(hex($1),hex($2),hex($3)) if $back =~ /^\#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i; my $color=$img2->colorAllocate($red,$gre,$blu); $img2->filledRectangle(0,0,$owidth,$oheight,$color); } $img2->copyResized($img1,$hoff,$voff,0,0,$neww,$newh,$width,$height); return $img2->jpeg(60); } sub imgindex { $_=$bgcolor;s/\#/\%23/; my ($urlcolor,$crt,@out)=($_,0); push @out, start_html({-bgcolor=>$bgcolor},$Title).h1($Title). start_form(-method=>"POST",-action=>script_name); $crt=param("crt") if param("crt"); print STDERR "Crt: ".$crt."\n"; $crt=$crt+$nbr if param("suivantes"); $crt=$crt-$nbr if param("précédentes"); $crt=$crt+$#files+1 if $crt < 0; $crt=$crt-$#files-1 if $crt > $#files; my $first=$crt-$nbr/2; $first=$first+$#files+1 if $first < 0; push @out, hidden(-name=>"crt",-value=>$crt,-override=>1). submit("précédentes").submit("suivantes").hr; for (1..$nbr) { push @out, do { $first != $crt ? a({-href=>script_name."?crt=".$first}, img({-border=>0, -src=>script_name."/".$urlcolor."/". $twidth."x".$theight."/".$first++.",jpg"})) : img({-border=>1, -src=>script_name."/".$urlcolor."/". $twidth."x".$theight."/".$first++.",jpg"}); }; $first=0 if $first > $#files; } push @out, hr.img({-src=>script_name."/".$urlcolor."/". $owidth."x".$oheight."/".$crt.",jpg"}); push @out, end_form.end_html; return join "\n",@out; } listfiles();readparam(); if (defined $imgnr) { print header('image/jpeg').mpict; } else { print header().imgindex(); }