Du behøver slet ikke nogle eksterne libraries til det. PHP's indbyggede GD lib, kan sagtens klare skærene. Eksemplet herunder stammer fra kommentarerne i manualen, tilpasset herfra, så den også gemmer/udskriver billedet.
/**
* Resize an image and keep the proportions
* @author Allison Beckwith <allison@planetargon.com>
* @param string $filename
* @param string $new_filename
* @param integer $max_width
* @param integer $max_height
* @return true
*/
function resizeImage($filename, $new_filename, $max_width, $max_height)
{
list($orig_width, $orig_height) = getimagesize($filename);
$width = $orig_width;
$height = $orig_height;
# taller
if ($height > $max_height) {
$width = ($max_height / $height) * $width;
$height = $max_height;
}
# wider
if ($width > $max_width) {
$height = ($max_width / $width) * $height;
$width = $max_width;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0,
$width, $height, $orig_width, $orig_height);
// Gem i fil
imagepng($image_p, $new_filename);
// Udskriv til browseren
#header('Content-Type: image/png');
#imagepng($image_p);
imagedestroy($image_p);
return true;
}
Link til manualen:
http://php.net/manual/en/function.imagecopyresized.php#50432