[Php-avanzado] Cargar imagen BLOB

Leonardo Tadei - Pegasus Tech Supply leonardot en pegasusnet.com.ar
Vie Oct 26 09:59:50 ART 2012


Hola Jésica,

	para generar la imagen almacenada, tal y como hacés tener que emitir
los header() adecuados al tipo mime del archivo, pero header no funciona
si el script ya tiene salida, y tu código de img.php tiene tags que
harán que ya no puedas modificar la cabecera.

	También tenés que decidirte cómo manejar la imagen:
a) si está en un BLOB hay que reconstruirla desde el campo de la tabla.
b) si usás imagemagik tenés que tener el archivo ya guardado en el disco
(y entonces no tiene sentido el BLOB)
c) si usás la bilbioteca GD de PHP, entonces tenés que tener el archivo
en el disco y no tiene sentido usar imagemagik.

	Estás mezclando 3 maneras de manejar una imagen!



El mié, 24-10-2012 a las 20:47 +0000, Jesica Billia escribió:
> 
> Buenas a todos!!!
> Consulta, tengo que cargar una imagen blob, la cargo pero no la puedo
> visualizar.... está en formato BLOB
> pego el código para ver si alguien me puede ayudar!! 
> Hay dos archivos, uno es img.php donde tengo la imagen blob y en
> catalogo2.php es el que debería mostrar la imagen.....
> Gracias!!!
> 
> 
> 
> 
> 
> 
> img.php
> ---------
> <HTML>
> <HEAD>
>  <TITLE>New Document</TITLE>
> </HEAD>
> 
> 
> <?php
> if(isset($_GET['id']))
> {
> 
> 
>     include 'config.php';
> $connection=mysql_connect("$bdservidor","$bdunombre","$bdpass") or
> die("Error conectando a la base de datos");
> 
> 
> 
> 
>  
>  
> $db=mysql_select_db("$bdnombre",$connection) or die ("Error
> seleccionando la base de datos");
> 
> 
>     $id      = $_GET['id'];
>     
> 
> 
>     $query   = "SELECT name, type, size, content FROM upload WHERE id
> = '$id'";
>     $result  = mysql_query($query) or die('Error, query failed');
> 
> 
>     list($name, $type, $size, $foto) = mysql_fetch_array($result);
>   echo 'lista imagen<br/>';echo $name;
> 
> 
>     $ruta= "images/accesorios/".$name ;
>     $im = imagecreatetruecolor(120, 20);
> 
> 
>     
>     // $image = new Imagick();
>     // $image->setResolution(300, 300);
>     // $image->readImageBlob($content);
>     
>         // convert the output to JPEG
>     //    $image->setImageFormat('jpeg');
>     //    $image->setImageCompressionQuality(60)
> 
> 
>     //header("Content-Disposition: attachment; filename=$name");
>     header("Content-length: $size");
>     header("Content-type: $type");
>     
> 
> 
>     
>     print $foto; //imagen blob
>     exit;
> }
> ?>
> 
> 
> 
> 
> <BODY>
> 
> 
> </BODY>
> </HTML>
> 
> 
> catalogo2.php
> 
> 
> <?php require_once('Connections/con_imag.php'); ?>
> <?php
> if (!function_exists("GetSQLValueString")) {
> function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
> $theNotDefinedValue = "") 
> {
>   if (PHP_VERSION < 6) {
>     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) :
> $theValue;
>   }
> 
> 
>   $theValue = function_exists("mysql_real_escape_string") ?
> mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
> 
> 
>   switch ($theType) {
>     case "text":
>       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
>       break;    
>     case "long":
>     case "int":
>       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
>       break;
>     case "double":
>       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
>       break;
>     case "date":
>       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
>       break;
>     case "defined":
>       $theValue = ($theValue != "") ? $theDefinedValue :
> $theNotDefinedValue;
>       break;
>   }
>   return $theValue;
> }
> }
> 
> 
> $maxRows_catalogo_imag = 10; //maxima paginas
> $pageNum_catalogo_imag = 0;
> if (isset($_GET['pageNum_catalogo_imag']))
>  {
>   $pageNum_catalogo_imag = $_GET['pageNum_catalogo_imag'];
>  }
> $startRow_catalogo_imag = $pageNum_catalogo_imag *
> $maxRows_catalogo_imag;
> 
> 
>  mysql_select_db($database_con_imag, $con_imag);
>  $query_catalogo_imag = "SELECT * FROM linkimagen";
>  $query_limit_catalogo_imag = sprintf("%s LIMIT %d, %d",
> $query_catalogo_imag, $startRow_catalogo_imag,
> $maxRows_catalogo_imag);
>  
>  $catalogo_imag = mysql_query($query_limit_catalogo_imag, $con_imag)
> or die(mysql_error());
>  $row_catalogo_imag = mysql_fetch_assoc($catalogo_imag);
> 
> 
> if (isset($_GET['totalRows_catalogo_imag'])) {
>   $totalRows_catalogo_imag = $_GET['totalRows_catalogo_imag'];
> } else {
>   $all_catalogo_imag = mysql_query($query_catalogo_imag);
>   $totalRows_catalogo_imag = mysql_num_rows($all_catalogo_imag);
> }
> $totalPages_catalogo_imag =
> ceil($totalRows_catalogo_imag/$maxRows_catalogo_imag)-1;
> ?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
> <title>Documento sin título</title>
> </head>
> 
> 
> <body>
> <table border="1">
>   <tr>
>     <td>Referencia</td>
>     <td>Imagen</td>
>     <td>Tipo</td>
>     <td>Precio</td>
>     <td>Nombre</td>
>     <td>Descripcion</td>
>   </tr>
>   <?php do
> 
> 
>   {
>       $flsize = $row_catalogo_imag['tama�o'];
>       $fltype = $row_catalogo_imag['tpimagen'];
>       $name   = $row_catalogo_imag['Nombre'];
>       $foto  = $row_catalogo_imag['imagenblob'];
>       
>       
>         ?>
>     <tr>
>       <td>          <?php echo
> $row_catalogo_imag['Referencia']; ?></td>
>                     <?//el id es fijo solo para prueba   ?>
>       <td>          <IMG src="img.php? id=3"></td>
>       <td>          <?php echo $row_catalogo_imag['Tipo']; ?></td>
>       <td><?php echo $row_catalogo_imag['Precio']; ?></td>
>       <td><?php echo $row_catalogo_imag['Nombre']; ?></td>
>       <td><?php echo $row_catalogo_imag['Descripcion']; ?></td>
>     </tr>
>     <?php } while ($row_catalogo_imag =
> mysql_fetch_assoc($catalogo_imag)); ?>
> </table>
> </body>
> </html>
> <?php
> mysql_free_result($catalogo_imag);
> ?>
> 
> 
> 
> 
> _______________________________________________
> Php-avanzado mailing list
> Php-avanzado en pato2.fi.mdp.edu.ar
> http://www3.fi.mdp.edu.ar/cgi-bin/mailman/listinfo/php-avanzado

-- 
Leonardo Tadei
leonardot en pegasusnet.com.ar
Web: http://leonardo.tadei.com.ar
Firma pública: http://www.pegasusnet.com.ar/LeonardoTadei-public.key



Más información sobre la lista de distribución Php-avanzado