Modifying the Script - Page 16
January 12, 2001
We then have to modify our showmall.php script to
handle thumbnails:
<?php
//showmall2.php
include "./common_db.inc";
Header("Content-type: image/png");
if ($show!="") {
$image = ImageCreateFromPNG("groundfloor.png");
$shops = explode(",", urldecode($show));
$link_id = db_connect('mapping');
$gray = ImageColorAllocate($image,204,204,204);
for ($x=0;$x<count($shops);$x++) {
$query = "SELECT m_area,m_center FROM mall
WHERE m_id=".$shops[$x];
$mallResult = mysql_query($query,$link_id);
$mallRow = mysql_fetch_array($mallResult);
$center = explode(",",$mallRow[1]);
ImageFill($image,$center[0],$center[1],$gray);
}
} else {
$image = ImageCreate(100,50);
$white = ImageColorAllocate($image,255,255,255);
$black = ImageColorAllocate($image,0,0,0);
ImageString($image,5,1,1,"Error",$black);
}
if ($thumbnail=="on") {
$thumb = ImageCreate(100,100);
ImageCopyResized($thumb,$image,0,0,0,0,100,100,500,500);
ImagePNG($thumb);
} else {
ImagePNG($image);
}
ImageDestroy($image);
?>
[Lines 11 and 12 above are one line. They have been split for
formatting purposes.]
When we run our script we should see this sort of result:
How It Works
Malldetail.php writes out an HTML file that contains
the information about the shop, as well as a thumbnail image of
the mall layout. This is the line that creates the image:
<IMG SRC="showmall.php?show=<?php echo $mallRow[0]; ?>&thumbnail=on"
WIDTH=100 HEIGHT=100>
This is almost exactly as we've seen before; the only difference
is that the script has the extra argument of
thumbnail=on. In showmall.php we now
check whether to output a thumbnail or the full size image. All
we need to change is the line:
ImagePNG($image);
We check for the value of $thumbnail, and if it's
'on' we resize the mall plan from its original dimensions of
500x500 to a more compact 100x100:
if ($thumbnail=="on") {
$thumb = ImageCreate(100,100);
ImageCopyResized($thumb,$image,0,0,0,0,100,100,500,500);
ImagePNG($thumb);
} else {
ImagePNG($image);
}
If the $thumbnail variable isn't set (or is set to
anything other than "on") we display the image as normal.
Showing the Shop Detail - Page 15
Beginning PHP4
Advanced Graphics Manipulation - Page 17
|