

The source code for this example page is:
<?php require("$DOCUMENT_ROOT/resources/common.php"); ?>
<?php MainHeader("GD Image Library Example",
"Example of creating resized images",
"PHP, GD"); ?>
<?php MainAd(); ?>
<h2>GD Image Library Example</h2>
<h3>Original image</h3>
<p><img src="test.gif" alt="" border="0"></p>
<h3>Resized Image</h3>
<p><img src="img.php?image=test.gif&scale=4" alt="" border="0"></p>
<h3>Source</h3>
<p>The source code for this example page is:</p>
<p><?php show_source($DOCUMENT_ROOT."/".$PHP_SELF); ?></p>
<p>and the source code for the script called img.php3 that actually
does the work of generating a resized image is:</p>
<p><?php show_source($DOCUMENT_ROOT."/web/php/imagefunctions/img.php"); ?></p>
<?php MainFooter(); ?>
and the source code for the script called img.php3 that actually does the work of generating a resized image is:
<?php
// we expect $scale and $image to be defined, having been passed to us via
// query string, e.g. http://www.foo.com/img.php3?image=test.gif&scale=4
// create an image object from the source file
$srcImg = imagecreatefromgif($image);
// create a (blank) smaller image object
$srcSize = getimagesize($image);
$dstImg = imagecreate($srcSize[0]/$scale, $srcSize[1]/$scale);
// copy and resize from the source image object to the smaller blank one
imagecopyresized($dstImg, $srcImg, 0, 0, 0, 0,
$srcSize[0]/$scale, $srcSize[1]/$scale,
$srcSize[0], $srcSize[1]);
// send the smaller image object to the browser
header("Content-Type: image/gif");
imagegif($dstImg);
// clean up
imagedestroy($scrImg);
imagedestroy($dstImg);
?>