Oct 30, 2011

Photos whith MySQL

Small projects for the integration of photos and thumbails in a database, and the creation of an album with these photos and link to enlarged view.


We start by creating the database and table
to store the code for the photo, and thumbail
the picture itself


create database photos;
create table fotos.foto_foto (
idfoto int auto_increment not null primary key,
foto_grande MEDIUMBLOB,
foto_pequena MEDIUMBLOB,
);


/ / / Datos.php (configuration page)


<? Php
$ Server = "";
$ Name = "";
$ Key = "";
?>




Now we take a simple form of our
the thumbail computer and photo (must be pre-
saved with the right size)
elType see the form, this allows us to send
both text files


/ / / Form.php


<?
echo '<font size=1>';
echo '<center>';
echo '<form action="guardararchivo.php" enctype="multipart/form-data" method="post">';
echo '<input type="file" Photo <br> name="foto">';
echo '<br> <br>';
echo 'Thum. <br> <input type="file" name="thumbail"> <br> <br>';
echo '<input type=hidden name=max_file_size value=1000000>';
echo '<input type="submit" value="Enviar archivo">';
echo '</ form>';
echo '<br> <a href=descargar_archivo2.php> View Album </ a>';
?>


This is the page that holds the photos in the database, a message of
error or success and brings us back to the form is very simple


/ / / Guardadararchivo.php


<? Php
require ("datos.php");
echo '<center> <font size=1>';
$ Link = @ mysql_connect ($ host, $ name, $ password);
$ Res = mysql_select_db ("Photo", $ link);
if ($ conn> 0)
{


$ File = $ _FILES ["photo"] ["tmp_name"];
$ Tamani = $ _FILES ["photo"] ["size"];
$ Type = $ _FILES ["photo"] ["type"];
$ Name = $ _FILES ["photo"] ["name"];
$ Archivo_thumb = $ _FILES ["thumbail"] ["tmp_name"];
$ Tamanio_thumb = $ _FILES ["thumbail"] ["size"];
$ Tipo_thumb = $ _FILES ["thumbail"] ["type"];
$ Nombre_thumb = $ _FILES ["thumbail"] ["name"];
if ($ file! = "none" & & $ archivo_thumb! = "none")
{
$ Fp = fopen ($ filename, "rb");
$ Content = fread ($ fp, $ size);
$ Content = addslashes ($ content);
fclose ($ fp);
$ Fp_thumb = fopen ($ archivo_thumb, "rb");
$ Contenido_thumb = fread ($ fp_thumb, $ tamanio_thumb);
$ Contenido_thumb = addslashes ($ contenido_thumb);
fclose ($ fp_thumb);
$ Query = "INSERT INTO fotos.foto_foto VALUES (null ,'".$ ."')"; contenido_thumb ."','".$ content
mysql_query ($ query);
if (mysql_affected_rows ($ conn)> 0) print "the file was saved in the database."
else print "Could not save the file in the database."
}
else print "Could not upload file to server";
Else {}
echo 'Not connected';
}
echo "<HTML> <SCRIPT> document.location = 'form.php', </ SCRIPT> </ HTML>";
?>




Below shows how you can create a photo album with a link to that same photo
larger, the smaller the thumbail we have saved and the smaller size
also large bas we have in the data (I recommend using compressed code with
gzip) here because they do not compress the script a bit complicated.




/ / / Descargar_archivo2.php


<? Php
require ("datos.php");
$ Link = @ mysql_connect ($ host, $ name, $ password);
$ Res = mysql_select_db ("Photo", $ link);


$ Qry = "SELECT idfoto, foto_pequena fotos.foto_foto FROM";
$ Res2 = mysql_query ($ qry);
for ($ j = 0, $ j <mysql_num_rows ($ res2); $ j + +)
for ($ i = 0; $ i <mysql_num_fields ($ res2); $ i + +)
{
$ Photo = basename (tempnam (getcwd (), 'tmp'));
$ Photo .=. "Jpg";
if ($ i == 1)
{
$ Contenido2 = mysql_result ($ res2, $ j, $ i);
$ Ft = fopen ($ photo, "w");
fwrite ($ ft, $ contenido2);
fclose ($ ft);
echo '<a href=fotogrande.php?idfoto='.mysql_result($res2,$j,0).'> <img src='.$foto.'>';
echo'';
}
}
echo '<br> <br> <font size=1> <center> <a href=form.php> Insert new photos </ a>';
?>




and finally create the page where you visualize the big photos
Simple, no?


/ / / Fotogrande.php


<? Php
require ("datos.php");
$ Link = @ mysql_connect ($ host, $ name, $ password);
$ Res = mysql_select_db ("Photo", $ link);
$ Qry = "SELECT FROM fotos.foto_foto foto_grande ='".$_ idfoto GET WHERE [" idfoto "]."'";
$ Res2 = mysql_query ($ qry);
$ Photo = basename (tempnam (getcwd (), 'tmp'));
$ Photo .=. "Jpg";
$ Contenido2 = mysql_result ($ res2, 0.0);
$ Ft = fopen ($ photo, "w");
fwrite ($ ft, $ contenido2);
fclose ($ ft);
echo '<center> <img src='.$foto.'> <br> <br>';
echo '<font size=1> <a href=descargar_archivo2.php> Back to album </ a>';
?>

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes