Tampoco estas para que exijas que te ayuden, a no ser que pienses pagar o algo =/
Lo que necesitas no es tanto el uso de GD, ya que eso es lo facil.. mas bien necesitas saber que calculos hacer para que mantenga esa proporcion.
Este es un script que uso para hacer miniaturas de imagenes en una carpeta de mi pc, puedes probarlo y basarte.. aunq creo que la funcion thumb hace lo que pides.
Código PHP:
<?php
function thumb($img, $w, $h, $fill = true, $save = false) {
if (!extension_loaded('gd') && !extension_loaded('gd2')) {
trigger_error("No dispones de la libreria GD para generar la imagen.", E_USER_WARNING);
return false;
}
$imgInfo = getimagesize($img);
switch ($imgInfo[2]) {
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Tipo de imagen no reconocido.', E_USER_WARNING); break;
}
if ($imgInfo[0] <= $w && $imgInfo[1] <= $h && !$fill) {
$nHeight = $imgInfo[1];
$nWidth = $imgInfo[0];
}else{
if ($w/$imgInfo[0] < $h/$imgInfo[1]) {
$nWidth = $w;
$nHeight = $imgInfo[1]*($w/$imgInfo[0]);
}else{
$nWidth = $imgInfo[0]*($h/$imgInfo[1]);
$nHeight = $h;
}
}
$nWidth = round($nWidth);
$nHeight = round($nHeight);
$newImg = imagecreatetruecolor($nWidth, $nHeight);
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
if ($save === false) {
header("Content-type: ". $imgInfo['mime']);
switch ($imgInfo[2]) {
case 1: imagegif($newImg); break;
case 2: imagejpeg($newImg); break;
case 3: imagepng($newImg); break;
default: trigger_error('Imposible mostrar la imagen.', E_USER_WARNING); break;
}
imagedestroy($newImg);
return true;
}else{
$name = array_shift(explode(".", $img, 2)).".th.".strtolower(array_pop(explode(".", $img)));
if (!file_exists($name)) {
switch ($imgInfo[2]) {
case 1: imagegif($newImg, $name); break;
case 2: imagejpeg($newImg, $name); break;
case 3: imagepng($newImg, $name); break;
default: trigger_error('Imposible mostrar la imagen.', E_USER_WARNING); break;
}
}
return $name;
}
}
$inicio = array_sum(explode(' ', microtime()));
if ($_SERVER['argc'] < 2) {
trigger_error('Uso: thumbs.php directorio [int ancho] [int alto]', E_USER_ERROR);
return false;
}
$dir = $_SERVER['argv'][1]."/";
$width = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : 150 ;
$height = isset($_SERVER['argv'][3]) ? $_SERVER['argv'][3] : 150 ;
$handle = opendir($dir);
while ($elemento = readdir($handle)) {
if ($elemento != "." && $elemento != ".." && $elemento != "Thumbs.db" && !strpos($elemento, ".th.")) {
thumb($dir.$elemento, $width, $height, false, true);
}
}
$fin = array_sum(explode(' ', microtime()));
echo "\nCompletado en ".($fin - $inicio)." seg";
?>