Bob
Your skill will accomplish
what the force of many cannot
Perl Monks 

Imager: slice an image to clickable html map

by zentara
Log in | Create a new user |  The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | Q&A | Library | 
 | Obfuscation | Poetry | Cool Uses For Perl | Snippets | Code | Craft | 
 | Perl News | Reviews | Tutorials | Newest Nodes | Offering Plate | 

on Nov 29, 2003 at 22:01 UTC print w/ replies, xml ) Need Help??

Description: This script takes a jpg, and breaks it into tiles. You can set the tile size, and everything else is automatic. It will create the tiles, and a sample html page. Imager works better than GD on jpgs, and I use jpgs because they are smaller. The script could be easily modified to do any other extension which Imager supports. You can see a sample clickable map here and a demonstration of GD's problem can be seen at this node
#!/usr/bin/perl
use warnings;
use strict;
use Imager;

my $file = shift || die "need filename\n";
my $tempname = $file;
$tempname =~ s/^(.+)(\.\w+)$/$1/;
print "$tempname\n";

#set tile size adjustment 
my $x = 100;
my $y = 100;

my $image = Imager->new();
$image->open(file=>$file, type=>'jpeg') or die $image->errstr();
my $width = $image->getwidth();
my $height = $image->getheight();

print "width->$width   height->$height\n";

my $rows = int($height/$y +1) - 1; #make it 0 based 
my $cols = int($width/$x + 1) - 1;
print "rows->$rows  cols->$cols\n";

foreach my $row(0..$rows){
    foreach my $col(0..$cols){
      my $imageout = Imager->new(xsize=>$x, ysize=>$y, type=>'direct')
+;


      $imageout = $image->crop(left=>$col*$y, right=>$col*$y+$y,
                                top=>$row*$x, bottom=>$row*$x+$x);

      my $tilename = $tempname .'-'.$row.'-'.$col.'.jpg';

      $imageout->write(file=>$tilename, type=>'jpeg')
           or die "Cannot write: ",$imageout->errstr;


    }
}

######################################################################
+ 
#make an html page with tiles reassembled and clickable 

open(HT,">$tempname-jpg.html") or warn $!;

print HT "<html><body><h1>$tempname.jpg</h1>";
print HT "<table border=0 cellspacing=0 cellpadding=0>";

#putting border=0 in the IMG<> makes it seemless 
#I have border=1 for demonstration 
foreach my $row(0..$rows){
  print HT "<tr>";
         foreach my $col(0..$cols){
         print HT "<td><a href= $tempname-$row-$col.html> 
                   <IMG SRC=$tempname-$row-$col.jpg border=1 HEIGHT=$y
+ WIDTH=$x  
                   alt=$tempname-$row-$col.jpg></a></td>"
     }
  print HT "</tr>";
}
print HT "</table></body></html>";


comment on Imager: slice an image to clickable html map
Download Code

Back to Snippets Section

Login:
Password
remember me
password reminder
Create A New User
Node Status
Node Type: snippet
help
Chatterbox
<theorbtwo> (I got them to put it in a box for me, and then I left the blasted box on the table when we left.)
<castaway> damn, Im really brain damaged tonight.. just tried to hit CTRL-a in a console several times, before realising that wasnt the one running screen
<theorbtwo> "side-bars"? It's just a table.
<blue_cowdawg> I didn't say it was going to be "great steak." It'll pass but it won't Peter Luger's! :-)
<Corion> castaway: And there I was thinking that screen was your shell :)
castaway builds gaim.rpm for the 5th time,,
<sporty> orb: sorry to hear.
<castaway> yeah, me too Corion, thus the confusion
<sporty> castaway: another solution would have been a "readmore" :)
<Intrepid> All well, Happy MoDa all.

How do I use this? | Other CB clients
Other Users
Others romping around the monastery: (28)
Corion
demerphq
Juerd
Mr. Muskrat
jmcnamara
thraxil
theorbtwo
atcroft
kudra
zentara
Petruchio
barrd
castaway
fglock
Nkuvu
blue_cowdawg
guha
sporty
xtype
jacques
Old_Gray_Bear
iguanodon
ambrus
AltBlue
biosysadmin
fizbin
Golo
Guildencrantz
As of 2004-05-09 20:01 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Snippets Section
Code Catacombs
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Outside Links
Find Nodes
Nodes You Wrote
Super Search
Perl Monks User Search
Newest Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Quests
Editor Requests
Buy PerlMonks Gear
Perl Monks Merchandise
Random Node
Voting Booth
How long have you been using Perl?
Perl???
Using? I don't use..
0-1 years
1-3 years
2-5 years
5-8 years
8-10 years
10-15 years
> 15 years
It was sort of my idea
[results]
[84 votes] [past polls]