To create a thumbnail, you pass the function PIPHP_MakeThumbnail()a GD image object
and the maximum value of the greater dimension for the thumbnail. For example, the
following code loads in the image in test.jpgusing the imagecreatefromjpeg()function,
and then passes it to the plug-in, along with a maximum dimension of 100. The function
then returns the new thumbnail to the string variable $thumb, which is then saved to the file
thumb.jpgusing the imagejpeg()function.
$image = imagecreatefromjpeg("test.jpg");
$thumb = PIPHP_MakeThumbnail($image, 100);
imagejpeg($thumb, "thumb.jpg");
You can also output the thumbnail straight to the browser by first sending the correct
header, like this:
$image = imagecreatefromjpeg("test.jpg");
header("Content-type: image/jpeg");
imagejpeg(PIPHP_MakeThumbnail($image, 100));
The PHP GD library is so powerful that it can perform a variety of image manipulations you
would normally only find in a graphics program. In fact, you could probably build quite an
advanced image editor using them.
<?php
function PIPHP_MakeThumbnail($image, $max)
{
$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);
}
?>
To perform an Edge Detect transformation on a file called photo.jpg,
you could use the following code, which will load a GD image object using the
imagecreatefromjpeg()function, and save the transformed image with the function
imagejpeg()
<?php
function PIPHP_ImageAlter($image, $effect)
{
switch($effect)
{
case 1: imageconvolution($image, array(array(-1, -1, -1),
array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
break;
case 2: imagefilter($image,
IMG_FILTER_GAUSSIAN_BLUR); break;
case 3: imagefilter($image,
IMG_FILTER_BRIGHTNESS, 20); break;
case 4: imagefilter($image,
IMG_FILTER_BRIGHTNESS, -20); break;
case 5: imagefilter($image,
IMG_FILTER_CONTRAST, -20); break;
case 6: imagefilter($image,
IMG_FILTER_CONTRAST, 20); break;
case 7: imagefilter($image,
IMG_FILTER_GRAYSCALE); break;
case 8: imagefilter($image,
IMG_FILTER_NEGATE); break;
case 9: imagefilter($image,
IMG_FILTER_COLORIZE, 128, 0, 0, 50); break;
C h a p t e r 4 : I m a g e H a n d l i n g 71 C h a p t e r 4 : I m a g e H a n d l i n g 71
case 10: imagefilter($image,
IMG_FILTER_COLORIZE, 0, 128, 0, 50); break;
case 11: imagefilter($image,
IMG_FILTER_COLORIZE, 0, 0, 128, 50); break;
case 12: imagefilter($image,
IMG_FILTER_EDGEDETECT); break;
case 13: imagefilter($image,
IMG_FILTER_EMBOSS); break;
case 14: imagefilter($image,
IMG_FILTER_MEAN_REMOVAL); break;
}
return $image;
}
?>
and the maximum value of the greater dimension for the thumbnail. For example, the
following code loads in the image in test.jpgusing the imagecreatefromjpeg()function,
and then passes it to the plug-in, along with a maximum dimension of 100. The function
then returns the new thumbnail to the string variable $thumb, which is then saved to the file
thumb.jpgusing the imagejpeg()function.
$image = imagecreatefromjpeg("test.jpg");
$thumb = PIPHP_MakeThumbnail($image, 100);
imagejpeg($thumb, "thumb.jpg");
You can also output the thumbnail straight to the browser by first sending the correct
header, like this:
$image = imagecreatefromjpeg("test.jpg");
header("Content-type: image/jpeg");
imagejpeg(PIPHP_MakeThumbnail($image, 100));
The PHP GD library is so powerful that it can perform a variety of image manipulations you
would normally only find in a graphics program. In fact, you could probably build quite an
advanced image editor using them.
<?php
function PIPHP_MakeThumbnail($image, $max)
{
$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);
}
?>
To perform an Edge Detect transformation on a file called photo.jpg,
you could use the following code, which will load a GD image object using the
imagecreatefromjpeg()function, and save the transformed image with the function
imagejpeg()
<?php
function PIPHP_ImageAlter($image, $effect)
{
switch($effect)
{
case 1: imageconvolution($image, array(array(-1, -1, -1),
array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
break;
case 2: imagefilter($image,
IMG_FILTER_GAUSSIAN_BLUR); break;
case 3: imagefilter($image,
IMG_FILTER_BRIGHTNESS, 20); break;
case 4: imagefilter($image,
IMG_FILTER_BRIGHTNESS, -20); break;
case 5: imagefilter($image,
IMG_FILTER_CONTRAST, -20); break;
case 6: imagefilter($image,
IMG_FILTER_CONTRAST, 20); break;
case 7: imagefilter($image,
IMG_FILTER_GRAYSCALE); break;
case 8: imagefilter($image,
IMG_FILTER_NEGATE); break;
case 9: imagefilter($image,
IMG_FILTER_COLORIZE, 128, 0, 0, 50); break;
C h a p t e r 4 : I m a g e H a n d l i n g 71 C h a p t e r 4 : I m a g e H a n d l i n g 71
case 10: imagefilter($image,
IMG_FILTER_COLORIZE, 0, 128, 0, 50); break;
case 11: imagefilter($image,
IMG_FILTER_COLORIZE, 0, 0, 128, 50); break;
case 12: imagefilter($image,
IMG_FILTER_EDGEDETECT); break;
case 13: imagefilter($image,
IMG_FILTER_EMBOSS); break;
case 14: imagefilter($image,
IMG_FILTER_MEAN_REMOVAL); break;
}
return $image;
}
?>