Here is an extract from my code that creates thumbnails based on a specific size.
Notice that I use djpeg, pnmscale and cjpeg utilities to change the image.
// get contents of a file into a string
$fd = fopen( $File, "r");
$data = addslashes(fread($fd, filesize($File)));
fclose($fd);
$size=getimagesize($File);
// Resize Original Picture to 3 specified sizes that can be worked.
// produce smaller image!
if (($size[0] > 630) && ($size[1]>400))
{
//Reduce the size of the original!
if($File_type=="image/jpeg" || $File_type=="image/pjpeg")
{
system("djpeg -pnm $File | pnmscale -xsize 420 | cjpeg > $File.tmb");
$fd = fopen( "$File.tmb", "r");
$data = addslashes(fread($fd, filesize("$File.tmb")));
fclose($fd);
unlink("$File.tmb");
}
}
$Comment = addslashes($Comment);
$Caption = addslashes($Caption);
//produce thumbnail
if($File_type=="image/jpeg" || $File_type=="image/pjpeg")
{
system("djpeg -pnm $File | pnmscale -xy 190 162| cjpeg > $File.tmb");
$fd = fopen( "$File.tmb", "r");
$tmb = addslashes(fread($fd, filesize("$File.tmb")));
fclose($fd);
unlink("$File.tmb");
}