aqui te dejo como lo hago yo
esta funcion redimensiona la imagen cambia de tamaño
Código PHP:
function redimensiona($archivo,$ancho,$alto,$color)
{
$img = @imagecreatefromjpeg($archivo);
if (!$img) {
header("HTTP/1.1 500 Internal Server Error");
echo "could not create image handle";
exit(0);
}
$width = imageSX($img);
$height = imageSY($img);
if (!$width || !$height) {
header("HTTP/1.1 500 Internal Server Error");
echo "Invalid width or height";
exit(0);
}
$target_width = $ancho;
$target_height = $alto;
$target_ratio = $target_width / $target_height;
$img_ratio = $width / $height;
if ($target_ratio > $img_ratio) {
$new_height = $target_height;
$new_width = $img_ratio * $target_height;
} else {
$new_height = $target_width / $img_ratio;
$new_width = $target_width;
}
if ($new_height > $target_height) {
$new_height = $target_height;
}
if ($new_width > $target_width) {
$new_height = $target_width;
}
$new_img = ImageCreateTrueColor($ancho, $alto);
switch($color){
case 'blanco':{$fondo = imagecolorallocate($new_img,255,255,255); break;}
case 'negro':{$fondo = imagecolorallocate($new_img,0,0,0);break;}
case 'gris':{$fondo = imagecolorallocate($new_img,102,102,102);break;}
}
if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, $fondo)) {
header("HTTP/1.1 500 Internal Server Error");
echo "Could not fill new image";
exit(0);
}
if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height)) {
header("HTTP/1.0 500 Internal Server Error");
echo "Could not resize image";
exit(0);
}
imagefill($new_img,0 ,0 ,$fondo);
// Use a output buffering to load the image into a variable
ob_start();
imagejpeg($new_img);
$imagevariable = ob_get_contents();
ob_end_clean();
return $imagevariable;
}
ahora como guardarla es facil
Código PHP:
$archivo = $_FILES["imagen"]["tmp_name"];
$nombre = $_FILES["imagen"]["name"];
// guardo en la ddbb
mysql_query("INSERT INTO fotos SET fecha_foto=NOW(),nombre_foto='$nombre'");
//guardo el archivo en la carpeta ke quieras
$id_foto = mysql_insert_id();
$nombre_arch = 'f'. $id_foto . "-" . $nombre;
$path_grande = "../fotos/productos/";
$file= fopen($path_grande . $nombre_arch ,"w");
fwrite($file,redimensiona($archivo,400,400,'blanco'));
saludos!!