Bueno creo que ya estubo bien de tantos articulos sobre psp y otras cosas y es tiempo de volver a lo mio, los tutoriales con mas codigo que palabras, aunque de repente pondre uno que ptro post de posicionamiento :P,
Requisitos:
1.- Analicemos el problema tomando como base el articulo Manejo de archivos con PHP y MySQL en el cual explico como guardar archivos en MySQL, asi que tomaremos la tabla de ese ejemplo.
Bueno ya fue mucha platica mejor vemos el codigo por partes, al final lo pondre todo junto:
$id_doc=$_REQUEST['id']; $qry="Select * from tbl_documentos where id_documento=$id "; $res=mysql_query($qry) or die(mysql_error()." qry::$qry");
if (mysql_numrows($res)>0) {
$obj=mysql_fetch_object($res);
$im= imagecreatefromstring($obj->contenido);
}else{
$im= imagecreatefromgif("media/images/nophoto.gif");
}
header("Content-type: {$obj->tipo}");
$width = imagesx($im);
$height = imagesy($im);
$imgw = 100;
$imgh = $height / $width * $imgw;
$thumb=imagecreatetruecolor($imgw,$imgh);
$back = imagecolorallocate($thumb, 255, 255, 255);
imagefill ( $thumb, 0, 0, $back );
ImageCopyResized($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im)); $out = ImagejpeG($thumb); imagedestroy ($im); imagedestroy ($thumb); print $out;
Aquie esta el codigo completo
//OBTENEMOS EL ID
$id_doc=$_REQUEST['id'];
//CONSTRUIMOS LA CONSULTA PARA OBTENER EL DOCUMENTO
$qry="Select * from tbl_documentos where id_documento=$id ";
//EJECUTAMOS LA CONSULTA
$res=mysql_query($qry) or die(mysql_error()." qry::$qry");
//VERIFICAMOS QUE LA IMAGEN EXISTE
if (mysql_numrows($res)>0) {
$obj=mysql_fetch_object($res);
//CREAMOS UNA IMAGEN CON LA FUNCION DE GD imagecreatefromstring
//YA QUE ESTA PUEDE LEER EL CAMPO BLOB QUE ESTAMOS OBTENIENDO
$im= imagecreatefromstring($obj->contenido);
}else{
//SI NO EXISTE USAMOS ALGUNA OTRA
$im= imagecreatefromgif("media/images/nophoto.gif");
}
//OBTENEMOS EL TIPO MIME DEL ARCHIVO ASI EL NAVEGADOR SABRA DE QUE SE TRATA
header("Content-type: {$obj->tipo}");
//OBTENEMOS LAS MEDIDAS ACTUALES DE LA IMAGEN
$width = imagesx($im);
$height = imagesy($im);
// ESTABLECEMOS EL TAMAÑO DEL THUMBNAIL
$imgw = 100;
//CALCULAMOS EL ALTO DE LA IMAGEN PARA MANTER EL ASPECTO
$imgh = $height / $width * $imgw;
// CREAMOS UNA NUEVA IMAGEN UTILIZANDO LAS NUEVAS MEDIDAS
$thumb=imagecreatetruecolor($imgw,$imgh);
//CREAMOS UN COLOR PARA EL FONDO
//ESTO ES IMPORTANTE PORQUE SI LA IMAGEN CONTIENE FONDO BLANCO
//SOLO OBTENDRIAMOS UNA IMEGEN NEGRA
$back = imagecolorallocate($thumb, 255, 255, 255);
// RELLENAMOS EL LA IMAGEN CON EL COLOR QUE CREAMOS EN EL PASO ANTERIOR
imagefill ( $thumb, 0, 0, $back );
// COPIAMOS LA IMAGEN ORIGINA AL THUMBNAIL
ImageCopyResized($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im));
//CREAMOS UNA IMAGEN TIPO JPEG
$out = ImagejpeG($thumb);
// LIMPIAMOS LA MEMORIA
imagedestroy ($im);
imagedestroy ($thumb);
//Y POR ULTIMO SIMPLEMENTE IMPRIMIMOS EL CONTENIDO DEL ARCHIVO
print $out;
Guardamos el codigo con el nombre que queramos yo por ejemplo thumbnail.php y lo usamos de la siguiente manera
<img src="http://blog.deliriumlabs.net/wp-admin/thumbnail.php?id_documento=1" />
Espero les sea util este articulo,
Si te gusto este post y te sientes dadivoso,Invitame un cafe
Dante Robles desde
como siempre muy buen articulo
Saludos
Dante
[Responder]
Andrés Borbón desde
¡Caramba! Debo ponerme a estudiar PHP.
[Responder]
Ruben Omar desde
Andres: pues solo eta de que pidan su tutorial hacerca de que les gustaria, y me doy el tiempo para hacerlo :D
[Responder]