This is a very simple function, but very useful for resizing JPEG images on the fly. You need to make sure GD is enabled on your PHP installation. It will resize any JPEG picture to a specified width, very handy for eCommerce sites. It will actually fit any picture into a square thumbnail of $size by $size.
usage: processImage($file,$dest,$size)
$file – the name of the file, use full directory name
$dest – the name of the file to output
$size – WIDTH of the file.
function processImage($file,$dest,$size)
{
$src = imagecreatefromjpeg($file);
/* create thumbnail */
$thumb = imagecreatetruecolor($size,$size);
$red = 255; $green = 255; $blue = 255;
$color = imagecolorallocate( $thumb, $red, $green, $blue );
/* fill with white */
$x = 1;
$y = 1;
imagefill($thumb, $x, $y, $color);
/* get image original width and height */
$src_x = imagesx($src);
$src_y = imagesy($src);
/* scale to fit width of $size if image is wider than height, align vertical center */
if($src_x>$src_y)
{
$ratio=$size/$src_x;
$new_x=$size ;
$new_y=$src_y * $ratio;
$new_xpos=0;
$new_ypos=($size - $new_y) / 2;
}
else /* scale to fit height of $size if image is higher than wide, align vertical center */
{
$ratio=$size/$src_y;
$new_x=$src_x * $ratio ;
$new_y=$size;
$new_ypos=0;
$new_xpos=($size - $new_x) / 2;
}
/* copy to new image and save */
imagecopyresampled($thumb,$src,$new_xpos,$new_ypos,0,0,$new_x,$new_y,$src_x,$src_y);
imagejpeg($thumb,$dest,80);
}



