<?php // Plug-in 13: Make Thumbnail
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
// You will need a jpeg image file called test.jpg in this folder
$image = imagecreatefromjpeg("test.jpg");
$thumb = PIPHP_MakeThumbnail($image, 100);
imagejpeg($thumb, "thumb.jpg");
$thumb2 = PIPHP_MakeThumbnail($image, 50);
imagejpeg($thumb2, "thumb2.jpg");
echo <<<_END
Here are the original and thumbnail images:<br /><img
src='test.jpg' align='left'> <img src='thumb.jpg'>
<br /><br />This thumbnail has maximum<br />dimensions of 100
pixels.<br /><br />Here's another with a maximum<br />50 pixels
width and height:<br /><br /><img src='thumb2.jpg'>
_END;
function PIPHP_MakeThumbnail($image, $max)
{
// Plug-in 13: Make Thumbnail
//
// This plug-in takes a GD image and returns a copy as
// a thumbnail. The arguments required are:
//
// $image: The image source
// $max: Maximum width and height
$thumbw = $w = imagesx($image);
$thumbh = $h = imagesy($image);
if ($w > $h && $max < $w)
{
$thumbh = $max / $w * $h;
$thumbw = $max;
}
elseif ($h > $w && $max < $h)
{
$thumbw = $max / $h * $w;
$thumbh = $max;
}
elseif ($max < $w)
{
$thumbw = $thumbh = $max;
}
return PIPHP_ImageResize($image, $thumbw, $thumbh);
}
// The function below is repeated here
// just for the sake of this example as it
// is called by it.
function PIPHP_ImageResize($image, $w, $h)
{
// Plug-in 12: Image Resize
// This plug-in takes a GD image and resizes it to the
// required dimensions. The arguments are:
// $image: The image source
// $w: New width
// $h: New height
$oldw = imagesx($image);
$oldh = imagesy($image);
$temp = imagecreatetruecolor($w, $h);
imagecopyresampled($temp, $image, 0, 0, 0, 0,
$w, $h, $oldw, $oldh);
return $temp;
}
?>
插件说明:
插件13接受两个参数。一个是需要转换为缩略图的图像,另一个是缩略图的最大宽度或高度,据体如下:
$image 一个需要转换的GD图像。
$max 缩略图的最大宽度或高度(取决于那个比较大)