Bueno aqui te dejo uno que me sirve de mucho, lo saque de funkgallery
![Pirata](http://static.forosdelweb.com/fdwtheme/images/smilies/pirate.png)
(
http://www.funkgallery.co.uk/ ) aunque me costo semanas en adaptarlo a mi formulario
![Pensando](http://static.forosdelweb.com/fdwtheme/images/smilies/scratchchin.gif)
valio la pena, si a alguien le sirve de mucho , de nada
primero lo que tienes que crear es una funcion, si tienes un archivo de funcion.php pues agregale esta funcion y si no pues crea uno con ese nombre de funcion.php
Código PHP:
<?php
//FUNCION.PHP
/* A DIE SUBROUTINE (a funky one :p)- TAKE ERROR AND MSG FROM INPUT */
function funkdie($error,$msg) {
global $myhtml;
$trcolor="#EEEEEE";
$trtext="BLACK";
$alt1="WHITE";
$boardname="yo";
echo "<p><table cellspacing=\"1\" cellpadding=\"4\" border=\"0\" width=\"100%\">
<tr bgcolor=\"$trcolor\"><td><font color=\"$trtext\">
<span class=\"ms\"><b>$error</b></span>
</font></td></tr>
<tr bgcolor=\"$alt1\"><td>
$msg
</td></tr>
</table></p>
";
//echo forum_jump();
//$myhtml->end_html();
exit;
}
// Resize uploaded avatars to a sensible size
function ResizeImage($im,$maxwidth,$maxheight,$name,$sname){
echo "Resizing thumbnail image.<br>";
if (!$im) {echo "Error passing filename.";
die;
}
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$sname);
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$sname);
}
}
function check_jpeg($f, $fix ){
# [070203]
# check for jpeg file header and footer - also try to fix it
if ( false !== (@$fd = fopen($f, 'r+b' )) ){
if ( fread($fd,2)==chr(255).chr(216) ){
fseek ( $fd, -2, SEEK_END );
if ( fread($fd,2)==chr(255).chr(217) ){
fclose($fd);
return true;
}else{
if ( $fix && fwrite($fd,chr(255).chr(217)) ){return true;}
fclose($fd);
return false;
}
}else{fclose($fd); return false;}
}else{
return false;
}
}
?>
despues en el formulario donde se inserta tiodo agregalo esta linea
<?
$un=$HTTP_POST_FILES['userfile']; ?>
<input type="file" name="userfile" size=45>
Ok durante el proceso hacemos lo copiamos este codigo que se encargara de reduicir la imagen y no te olvides de incluir en la parte de arriva la funcion.php
Código PHP:
include "funcion.php";
<?php
//pasamos las fotos al folder photos
$uploaddir = "../images/photos/";
$un=$HTTP_POST_FILES['userfile']['name'];
$un=$thingy=str_replace(" ","_",$un);
$uploadfile = $uploaddir.$un;
$uploadthumb=str_replace("photos","thumbs",$uploadfile);
$ftype=($HTTP_POST_FILES['userfile']['type']);
if ($ftype<>'image/gif' AND $ftype<>'image/jpeg' AND $ftype<>'image/pjpeg') funkdie("Error","Wrong file type - GIF or JPG ONLY.");
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print "File is valid and was successfully uploaded.<br>";
} else {
print "Error uploading file! Here's some debugging info:<br>";
print "UPF = ".$uploadfile."<br>FTYPE = ".$ftype."<br>UPTHUMB = ".$uploadthumb."<br>UN = ".$un."<br>";
print_r($_FILES);
die;
}
//chmod($uploadfile,0777);
$twidth=90;
$theight=90;
$FILENAME=$uploadfile;
$RESIZEWIDTH=$twidth;
$RESIZEHEIGHT=$theight;
echo "Uploaded $uploadfile <br>";
if ($ftype=="image/jpeg" or $ftype=="image/pjpeg") {
echo "Checking JPEG file structure.<br>";
check_jpeg($uploadfile, $fix=0 );
echo "Creating thumbnail.<br>";
$im = imagecreatefromjpeg($uploadfile);
if(!$im) {echo "Error while creating thumbnail<br>File Uploaded - $uploadfile";
die;
}
} elseif ($ftype=="image/gif") {
echo "Creating thumbnail.<br>";
$im = imagecreatefromgif($uploadfile);
if(!$im) {echo "Error while creating thumbnail";
die;
}
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$uploadfile,$uploadthumb);
?>
y donde insertas los datos coloca esto
Código PHP:
$sql = "INSERT INTO tabla ( campo1, campo2, imagen, campo4) VALUES ('$campo1','$campo2', '$un','$campo4');
if (@mysql_query($sql)) {
echo("<P>Su insercion ha sido con exito!! <a href='agregar.php'>agregar de nuevo</a> <a href='index.php'>Regresar</a></P>");
} else {
echo("<P>error al insertar: ".mysql_error()."</P>");
}
Bueno obviemente tienes que tener una carpeta donde cuardas las imagenes
crea una carpeta de "images" dentro de esa crea una que diga "photos" y otra de "thumbs" y listo .
Bueno espero te sirva de algo esto chau.