⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch6.htm

📁 美国Macmillan出版社编写的Perl教程《Perl CGI Web Pages for WINNT》
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<P>

There are also the many users who search the Web with the graphics

option on their browsers turned off to save time. It is important

to remember this when designing a page with an image map so that

you also include alternatives for these users. You can include

this script, bi_modal.pl, which will screen the user's browser,

and return either the image map page, or a nonimage alternative.

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

     # bi_modal.pl

     print &quot;Content-type: text/html\n\n&quot;;

     # decide if the browser is text or graphics 

     if ( $ENV{HTTP_USER_AGENT} =~ /Lynx|LineMode|W3/i ) {

     print &lt;&lt;EOG; # this is the non-image page

     &lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Page Title&lt;/TITLE&gt;&lt;/HEAD&gt;

     &lt;BODY&gt;&lt;H2&gt;Choices - Text Version&lt;/H2&gt;

     &lt;A HREF=&quot;http://my_server.com/choice1.htm&quot;&gt;

     Choice One&lt;/A&gt;

     &lt;A HREF=&quot;http://my_server.com/choice2.htm&quot;&gt; 

Choice Two&lt;/A&gt;

     &lt;A HREF=&quot;http://my_server.com/choice3.htm&quot;&gt;

     Choice Three&lt;/A&gt;

     &lt;BR&gt;&lt;H&gt;&lt;/BODY&gt;&lt;/HTML&gt;

     EOG

     }

     else {

     print &lt;&lt;EOI; # this is the image map page

     &lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Page Title&lt;/TITLE&gt;&lt;/HEAD&gt;

     &lt;BODY&gt;&lt;H2&gt;Choices - Image Map Version&lt;/H2&gt;

     &lt;A HREF=&quot;http://my_server.com/choice.map&quot;&gt;

     &lt;IMG src=&quot;http://my_server.com/choice.gif&gt;&lt;/A&gt;

     &lt;BR&gt;&lt;H&gt;&lt;/BODY&gt;&lt;/HTML&gt;

     EOI

     }

     exit;

</PRE>

</BLOCKQUOTE>

<P>

where the text choices are found in the files choice1.html, choice2.html,

and choice3.html. The file choice.map is the map file, and choice.gif

is the graphics file that is being used as the image map. These

different file types are explained in depth later in this chapter.

<P>

The actual Perl scripts that are used with image maps are too

advanced for the scope of this book, but most HTTP servers come

with a Perl script that handles image maps.

<P>

Normally, what happens in an image map is that you make an

<BLOCKQUOTE>

<PRE>

&lt;A HREF=&quot;pic-name.map&quot;&gt;&lt;IMG SRC=&quot;picture.gif&quot; ISMAP&gt;&lt;/a&gt;

</PRE>

</BLOCKQUOTE>

<P>

and the server will check the &quot;pic-name.map&quot; file for

the coordinates corresponding to each document, like so

<BLOCKQUOTE>

<PRE>

RECT     4&Oslash;&Oslash;,3&Oslash;&Oslash; 5&Oslash;&Oslash;,35&Oslash;    newpage.html

CIRCLE   2&Oslash;&Oslash;,1&Oslash;&Oslash;,5&Oslash;         anotherpage.html

</PRE>

</BLOCKQUOTE>

<P>

There is usually space for polygons as well. The server would

normally do all of this work. There is, however, another way.

If you make a form using

<BLOCKQUOTE>

<PRE>

&lt;FORM METHOD=POST

ACTION=&quot;http://www.my_server.com/cgi-bin/script.pl&gt; 

</PRE>

</BLOCKQUOTE>

<P>

You can enter a line like so:

<BLOCKQUOTE>

<PRE>

&lt;INPUT TYPE=&quot;IMAGE&quot; NAME=&quot;picture.html&quot; IMG

SRC=&quot;http://www.yourserver.com/picture.gif&quot; BORDER=&quot;&Oslash;&quot; HEIGHT=5&Oslash; WIDTH=155&gt;

</PRE>

</BLOCKQUOTE>

<P>

If the picture is selected, it will be passed as form data to

the CGI program in the form of picture.html.<I>x</I>=500 picture.html.<I>y</I>=200,

where 500 and 200 are the clicked <I>x </I>and <I>y</I> coordinates,

respectively. The CGI program then has to handle the <I>x</I>

and <I>y</I> material, check the map file, and figure out the

coordinate information. 

<H3><A NAME="TheThreeStepstoDefininganImageMap">

The Three Steps to Defining an Image Map</A></H3>

<P>

There are three basic steps that each image map has to go through

to be completely functional. The first is to create a graphic

that will be used for the image map. The second is to write a

file that will store the destinations of the &quot;hot spots&quot;

specified in the image map. Finally, in the third step, you must

make sure there is a reference to this file in your HTML document.

<P>

A quick overview of these steps will give you the gist of what

you need to know. Then we can examine each area in greater depth.

<H4>How to Create an Image Map</H4>

<P>

The first step in defining an image map is to select an appropriate

graphic to use as your image. The best graphics are those that

are easy to understand, small in file size (meaning they will

load quickly) and have well-defined areas from which the user

can choose.

<P>

If the graphic you want to use is not divided in an obvious way,

then you should alter it so that these borders are readily apparent

to the user. In these separate areas you will be selecting the

area that will have the link, called a &quot;hot spot.&quot; These

hot spots are pixels that have been specified to have an URL,

which is called the same way as an HTML link can call an URL.

<H4>Writing a Map File</H4>

<P>

Once the image is designed and divided, you have to create a map

file of the different destinations of the image map's hot spots.

In this file, you designate the hot spots' locations and what

shape these areas will have under the image map. The standard

format for a map file is as follows:

<BLOCKQUOTE>

<PRE>

default default-URL

     rect URL UL-corner LR-corner

     poly URL POINT1 POINT2 POINT3

     circle URL CENTER EDGE-POINT

</PRE>

</BLOCKQUOTE>

<P>

where the default-URL is the location of the default file if the

user selects an area of the active image map that does not have

a specified destination. The commands stand for the different

shapes: rect means a rectangle, poly means a polygon, and circle

means a circle. The commands after the shape operators define

the area of that shape. If two areas overlap, then the first area

defined will be the one to which the image map link responds.

<P>

Each server has its own shape designations, so check your documentation

carefully when you are writing your image map file.

<H4>Referencing the Image Map File</H4>

<P>

The final phase of image map design requires you to reference

your image map in the HTML document displaying the image map.

This is done by using an HTML tag that identifies the image map

file location, and has this general format:

<BLOCKQUOTE>

<PRE>

&lt;A HREF=&quot;http://my_server.com/cgi-bin/image.map&quot;&gt;

&lt;IMG SRC=&quot;image_map.gif&quot; ISMAP&gt;&lt;/A&gt;

</PRE>

</BLOCKQUOTE>

<P>

where an &lt;A HREF&gt; tag with the map file location is associated

with an &lt;IMG&gt; tag of the image map itself. Different servers

have different requirements for how this information should be

sent, so be sure to read your HTTP server's documentation.

<H4>A Sample Image Map</H4>

<P>

To solidify a basic understanding of the image map process before

you move into the related Perl scripts, this example will illustrate

the steps you've just covered.

<P>

The image you are going to use is shown in Figure 6.6. It is a

simple map of three crests.

<P>

<A HREF="f6-6.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f6-6.gif"><B>Figure 6.6 :</B> <I>An image of three crests for an image map</I>.</A>

<P>

The shape underneath each of the crests is a rectangle, and the

pixel locations for the upper-left and lower-right corners to

define are 33,69/137,197 for the first crest, 175,69/279,197 for

the second, and 309,69/419,197 for the third.

<P>

This HTML document is from a medieval Web game that uses these

pixel coordinates to link the map definition to the image and

its assigned links.

<BLOCKQUOTE>

<PRE>

&lt;HTML&gt;

&lt;HEAD&gt;

&lt;TITLE&gt;

Choose Your Army

&lt;/TITLE&gt;

&lt;/HEAD&gt;

&lt;BODY&gt;

&lt;H2&gt;

Choose your army!

&lt;/H2&gt; 

&lt;A HREF=&quot;http://www.my_server/cgi-bin/game/crests.map&quot;&gt;

&lt;IMG src=&quot;http://www.my_server/game/crests.gif&quot; ISMAP&gt;&lt;/A&gt;

&lt;P&gt;

&lt;HR&gt;

&lt;/BODY&gt;

&lt;/HTML&gt;

</PRE>

</BLOCKQUOTE>

<P>

where the &lt;A HREF&gt; address is the location of the map file

and the &lt;IMG&gt; address in the location of the image, crests.gif,

is used as the map.

<P>

There is a call in the &lt;A HREF&gt; tag to the file crests.map,

which is the image map file defining the various links. 

<BLOCKQUOTE>

<PRE>

# crests.map

     default http://www.my_server.com/cgi-bin/game/armies.html

	 rect http://www.my_server.com/cgi-bin/game/army_one.html 33,69 137,197

     rect http://www.my_server.com/cgi-bin/game/army_two.html 175,69 279,197

     rect http://www.my_server.com/cgi-bin/game/army_three.html 3&Oslash;9,69 419,197

</PRE>

</BLOCKQUOTE>

<P>

When this HTML file is called up with the associated map file,

you get something like the figure shown in Figure 6.7.

<P>

<A HREF="f6-7.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f6-7.gif"><B>Figure 6.7 :</B> <I>A sample image map</I>.</A>

<P>

So, now that you've made a brief pass over the different facets

of the image map, let's review them in greater depth.

<H3><A NAME="DividinganImageMap">

Dividing an Image Map</A></H3>

<P>

To demonstrate dividing up an image we'll use the graphic in Figure

6.8. Using an actual map is a good example to work with, because

the original borders create ready-made sections. This is a map

of Canada, which is divided into the various regions, or provinces,

that make up the country. With this map the user can select a

province and get more information about that area.

<P>

<A HREF="f6-8.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f6-8.gif"><B>Figure 6.8 :</B> <I>A map of Canada</I>.</A>

<H3><A NAME="TheCGIImageMapMethods">

The CGI Image Map Methods</A></H3>

<P>

There are several ways to send the image map coordinates to the

CGI. Each of these ways has its merits, but the method you choose

will be based on what your https require.

<P>

After the graphic, or image, is clearly divided, the hot spots

must be defined. This is done by selecting the <I>x</I> and <I>y</I>

coordinates for each hot spot. Once the hot spots are defined,

you only have to test the image map before you add it to your

Web page.

<P>

Once you have an appropriate graphic for your image map you have

to determine the size of your image. The important size when dealing

with image maps is not the memory size, but the pixel size. This

example is 560 pixels by 480 pixels. Pixels are always measured

from the upper-left and lower-right of the screen. The uppermost

pixel location has an <I>x</I> location of 0 and a <I>y</I> location

of 0, which is written in pixel coordinate notation like this:

0,0. The <I>x </I>coordinate increases going from left to right

on the image and the <I>y</I> coordinate increases going from

top to bottom. A hot spot that was located 300 pixels to the left

and 250 pixels down would have the <I>x</I>,<I>y </I>coordinates

of 300,250.

<P>

An easy way to determine the location of a pixel for a hot spot

is to use a graphics viewer. Viewing the image you have selected

for your image map, the cursor will pass over the hot spot and

display the <I>x</I> and <I>y</I> coordinates. You also can use

this Perl script to produce an HTML document that will display

the <I>x</I> and <I>y</I> coordinates of an image that is clicked

on with the cursor.

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

     # imap.pl

     push (@INC, &quot;your_directory_location/cgi-bin&quot;);

     require(&quot;CGI-lib.pl&quot;);

     &amp;parse(*location);

     print &amp;header;

     print &quot;&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;\n&quot;;

     print &quot;Finding Hot Spots&lt;/HEAD&gt;&lt;/TITLE&gt;\n&quot;;

     print &quot;&lt;BODY&gt;&lt;H2&gt;/n&quot;;

     print &quot;Your X &amp; Y Coordinates Are:&lt;/H2&gt;\n&quot;;

     print &quot;&lt;P&gt;&lt;HR&gt;&lt;P&gt;/n&quot;;

     print &quot;X Coordinate is $location{'xyhot.x'}&lt;BR&gt;/n&quot;;

     print &quot;Y Coordinate is $location{'xyhot.y'}&lt;BR&gt;/n&quot;;

     print &quot;&lt;/BODY&gt;&lt;/HTML&gt;&quot;;     

</PRE>

</BLOCKQUOTE>

<P>

The result is something like that seen in Figure 6.9.

<P>

<A HREF="f6-9.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f6-9.gif"><B>Figure 6.9 :</B> <I>The results of an x,y coordinate search</I>.</A>

<P>

It is important not to limit the size of your image maps the same

way you limit the size of other images on your Web pages. The

image map has to maintain a certain pixel size which can be hindered

if you set an image size for it. If the user's browser cannot

immediately display your entire image map, the user will be able

to scroll around to view the entire map.

<P>

To send these coordinates, the user clicks on the hot spot and

that location is sent to an image map script. Many servers have

a standard image map script that you can use, so, again, be sure

to check your server's documentation. You can also use one of

the many freeware and shareware programs for creating image maps,

like Map Edit and Web Hotspots, both of which are on the CD-ROM

included with this book.

<H2><A NAME="Conclusion"><FONT SIZE=5 COLOR=#FF0000>

Conclusion</FONT></A></H2>

<P>

These programs are all basic to using Perl with the CGI, and use

a basic call and response cycle. The user, either the Web browser

or a human being, makes a call to the Perl script on your server

through the CGI, and the Perl script sends a response, again through

the CGI. Understanding this cycle is very important to the effective

operation of your Perl scripts through the CGI. Although the scripts

in this chapter operate on a single data request, the scripts

in the next chapter often rely on multiple requests for data being

passed from the user to the Perl script, and vice versa.

<HR>



<CENTER><P><A HREF="ch5.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch5.htm"><IMG SRC="PC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/PC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="#CONTENTS"><IMG SRC="CC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/CC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="contents.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/contents.htm"><IMG SRC="HB.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/HB.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="ch7.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch7.htm"><IMG SRC="NC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/NC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<HR WIDTH="100%"></P></CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -