Re: [pgsql-es-ayuda] Leer imágenes desde la base de datos con php

From: Nahum Castro <nahumcastro(at)gmail(dot)com>
To: Mauricio Baeza <elmau(at)tuta(dot)io>, pgsql-es-ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: [pgsql-es-ayuda] Leer imágenes desde la base de datos con php
Date: 2016-10-20 05:52:40
Message-ID: CABqArLhm3Ty308Jd2TvH8CvfgFvn8tDQZshQ8OfWoLPJNL=znw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Listo, ya encontre la solución para llenar la tabla con las imágenes de la
base de datos.
La solución esta en utilizar decode con base64 y embeberlo con el tag <img
/> también con base64 tal como se define en la variable $consultault
<!DOCTYPE html>
<head>
<title>Orbis</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
li {listt-style: none;}
</style>
</head>
<body>
<h2>Acceso al comedor</h2>
<ul>
<form name="insert" action="index.php" method="POST" >
Tarjeta :</li><li><input type="text" name="tarjeta" autofocus />
<input type="submit" />
</form>
</ul>
<?php
$db = pg_connect("host=localhost port=5432 dbname=orbis user=nahum
password=xxxxx");
$query = "INSERT INTO acceso (tarjeta) VALUES ('$_POST[tarjeta]')";
$result = pg_query($query);

$consultault = "select id, nombre, '<img src=\"data:image/jpeg;base64,' ||
encode(foto, 'base64') || '\"name=\"Imagen1\" align=\"left\" width=\"86\"
height=\"99\" border=\"0\" />' as foto from ultimos";
$resultado = pg_query($consultault);

$i = 0;
$tabla='<table id="ultimos"><tr>';
while ($i < pg_num_fields($resultado))
{
$fieldName = pg_field_name($resultado, $i);
$tabla=$tabla. '<th>' . $fieldName . '</th>';
$i = $i + 1;
}
$tabla=$tabla. '</tr>';
$i = 0;
while ($row = pg_fetch_row($resultado))
{
$tabla=$tabla. '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
$tabla=$tabla. '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
$tabla=$tabla. '</tr>';
$i = $i + 1;
}

$tabla=$tabla.'</table>';

echo $tabla;
pg_free_result($resultado);
?>
</body>
</html>

El 19 de octubre de 2016, 21:42, Mauricio Baeza<elmau(at)tuta(dot)io> escribió:

> 19. Oct 2016 20:46 by nahumcastro(at)gmail(dot)com:
>
>
> Hola a tod(at)s
>
> Tengo un problema al leer imágenes desde la base de datos en php.
> OS: Centos 7
> Postgresql: 9.6
> PHP: 5.4.16
>
> Antes usaba oid y esas cosas en postgresql 7 @ del año 2003, pero ahora
> tengo la necesidad de guardar imágenes en la base de datos en postgresql
> 9.6.
> Esta es mi tabla.
> Tabla «public.personal»
> Columna | Tipo | Modificadores
> ---------------+------------------------+---------------
> id | integer | not null
> nombre | character varying(255) |
> horario | character varying(200) |
> puesto | character varying(255) |
> ingreso | date |
> departamento | character varying(255) |
> clasificacion | character varying(255) |
> tarjeta | character varying(50) |
> foto | bytea |
> Índices:
> "pk_orbis" PRIMARY KEY, btree (id)
> "tarjeta_unica" UNIQUE CONSTRAINT, btree (tarjeta)
> Referenciada por:
> TABLE "acceso" CONSTRAINT "fk_tarjeta_personal" FOREIGN KEY (tarjeta)
> REFERENCES personal(tarjeta) ON UPDATE CASCADE ON DELETE RESTRICT
>
> Las imágenes las guardo en un bytea
> y las puedo obtener como sigue
> \copy (select encode(foto, 'hex') from personal where id = 193) to
> '/home/nahum/Escritorio/imagen.hex';
> y luego
> xxd -p -r imagen.hex > imagen.jpg
> desde bash
> La imágen se ve bien.
>
> Pero el siguiente código no funciona.
> <?php
> // Conectar a la base de datos
> $dbconn = pg_connect("host=localhost port=5432 dbname=personal
> user=nahum password=xxxx");
>
> // Obtener los datos
> $res = pg_query("SELECT foto FROM personal WHERE id=193;");
> $row = pg_fetch_row($res);
>
> // Convertir a binario?
> $image = pg_unescape_bytea($row[0]);
> header("Content-type: image/jpeg");
> echo $image;
> pg_close($dbconn);
> ?>
>
> El problema es que no obtengo ninguna imágen, solo: "No se puede mostrar
> la imágen por que contiene errores"
> ¿Se puede emular xdd -p -r en postgres o en php?
>
> Saludos y gracias.
> Nahum.
>
>
>
> Prueba si esto funciona: http://zetcode.com/db/postgresqlphp/images
>
>
> Saludos
>
>
>
>
>

--
*Nahum Castro González*
Blvd. Perdigón 214, Brisas del Lago.
CP 37207
León, Guanajuato, México
Tel (477)1950304
Cel (477)1274694

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Martín Marqués 2016-10-20 11:49:47 Re: Re: [pgsql-es-ayuda] Leer imágenes desde la base de datos con php
Previous Message Nahum Castro 2016-10-20 01:46:12 Leer imágenes desde la base de datos con php