Foros del Web » Programando para Internet » PHP »

Poner orden a las imagenes de mi galería.

Estas en el tema de Poner orden a las imagenes de mi galería. en el foro de PHP en Foros del Web. Hola. Tengo hecha una galeria de fotos en php con partes de una galeria prefabricada que me descargué. Consta de dos partes, una es para ...
  #1 (permalink)  
Antiguo 21/04/2009, 14:43
Avatar de Joystickoso  
Fecha de Ingreso: enero-2009
Mensajes: 127
Antigüedad: 15 años, 3 meses
Puntos: 3
Poner orden a las imagenes de mi galería.

Hola. Tengo hecha una galeria de fotos en php con partes de una galeria prefabricada que me descargué. Consta de dos partes, una es para subir las imagenes y crear los thumbs en una carpeta para la galeria y la otra abre la carpeta donde están y la muestra en una tabla.
Mi problema es que muestra las imagenes en dicha tabla pero sin ningun orden aparente, lo he mirado y no es ni por tamaño ni fecha ni nombre.
Son dos archivos index.php y gallerybuilder.php. (pongo solo lo importante)
Index.php
Código PHP:
<?php
// el archivo empieza por

 
$default "";
 include(
"galleryBuilder.php");
?>
// y el codigo de la tabla donde aparecen las imagenes
<table align="center">
                <?php createGallery("Upload/pics/" $a"Upload/pics/" $a "thumbs/"120805FALSE); ?>
              </table>
Y el Gallerybuilder.php
Código PHP:
<?php
if($_GET["a"] != "")
{
 
$a $_GET["a"] . "/";
}
else
{
 
$a $default "/";
}
// This is a very basic way of determining whether to insert
// an extra trailing slash or not


function createGallery($gF$tF$tW$tH$nR$nW)
{
 
// $gF is the path to the folder that contains the images
 //     example: "gallery/"

 // $tF is the path to the folder that contains the thumbnails of the images
 //     example: "gallery/thumbs/"

 // $tW is the desired width of the thumbnails
 //     example: 120

 // $tH is the maximum desired height of the thumbnails
 //     example: 80

 // $nR is the desired number of images in each row of the table
 //     example: 8

 // $nW is a boolean value. If set to TRUE, opens full sizes in new window
 //     example: FALSE


 
echo " <tr>\n";

 
buildThumbs($gF$tF$tW$tH);
 
// call to another function to generate the thumbs

 
$open opendir($gF);
 
// open the directory to the gallery folder

 
$i 1;
 
$j 1;
 
// we use $i and $j to loop alternating table cells and the number of cells
 // per row
 
while(FALSE !== ($fileName readdir($open)))
 {
  
$info pathinfo($gF $fileName);
  
// get info for the filename
  
if(strtolower($info["extension"]) == "jpe" || strtolower($info["extension"]) == "jpeg" || strtolower($info["extension"]) == "jpg" || strtolower($info["extension"]) == "gif" || strtolower($info["extension"]) == "png")
  {
  
// this case-insensitively checks for all possible JPEG, GIF, and PNG extensions and only
  // returns files that meet these conditions
   
echo "  <td width=\"" $tW "\" height=\"" $tH "\" class=\"galleryCell";
   if(
$i == 1)
   {
    
// if $i is 1
    
echo "1";
    
$i 2;
   }
   else
   {
    echo 
"2";
    
$i 1;
   }
   echo 
"\"><a";
   if(
$nW == TRUE)
   {
    echo 
" target=\"_blank\"";
   }
   echo 
" href=\"" $gF $fileName "\" title=\"" $fileName "\"><img class=\"galleryImage\" src=\"" $tF $fileName .  "\" border=\"0\"></a></td>\n";
   if(
$j == $nR)
   {
    
// if $j is the number or cells per row...
    
echo " </tr>\n <tr>\n";
    
$j 1;
   }
   else
   {
    
$j++;
   }
  }
 }
 
closedir($open);
 
// close the directory
 
echo " </tr>\n";
}






function 
buildThumbs($gF$tF$tW$tH)
{
 
// $gF is the path to the folder that contains the images
 //     example: "gallery/"

 // $tF is the path to the folder that contains the thumbnails of the images
 //     example: "gallery/thumbs/"

 // $tW is the desired width of the thumbnails
 //     example: 120

 // $tH is the maximum desired height of the thumbnails
 //     example: 80

 
$open opendir($gF);
 
// open the directory to the gallery folder
 
while(FALSE !== ($fileName readdir($open)))
 {
  
// loop throught the entire directory
  
if(!file_exists($tF $fileName))
  {
   
// there is NOT already a thumbnail with the same name, so let's create one!
   
$info pathinfo($gF $fileName);
   
// get file info for each file
   
if(strtolower($info["extension"]) == "jpe" || strtolower($info["extension"]) == "jpeg" || strtolower($info["extension"]) == "jpg" || strtolower($info["extension"]) == "gif" || strtolower($info["extension"]) == "png")
   {
    
// this really long line asks if the lowercase version of the extension ends in each JPEG format (jpe, jpeg, jpg), a GIF format (gif), or a PNG format (png), all of our valid image types
    
if(strtolower($info["extension"]) == "jpe" || strtolower($info["extension"]) == "jpeg" || strtolower($info["extension"]) == "jpg")
    {
     
// this narrows it down--the image must be a JPEG
     
$thumbnail imagecreatefromjpeg($gF $fileName);
    }
    if(
strtolower($info["extension"]) == "gif")
    {
     
// it must be a GIF
     
$thumbnail imagecreatefromgif($gF $fileName);
    }
    if(
strtolower($info["extension"]) == "png")
    {
     
// create a PNG
     
$thumbnail imagecreatefrompng($gF $fileName);
    }
    
$oWidth    imagesx($thumbnail);
    
// original width
    
$oHeight   imagesy($thumbnail);
    
// original height
    
$tWidth    $tW;
    
// the new thumbnail will have the specified width
    
$tHeight   floor($oHeight * ($tWidth $oWidth));
    
// calculate the thumbnail height by multiplying the original height by the width ratio
    
if($tHeight $tH)
    {
     
// if the image is still too tall...
     
$tHeight  $tH;
     
// set the height to the max height
     
$tWidth   floor($oWidth * ($tHeight $oHeight));
     
// calculate the thumnail width by multiplying the original width by the height ratio
    
}
    
$tempImage imagecreatetruecolor($tWidth$tHeight);
    
// create a temporary image (it's blank right now)

    
imagecopyresized($tempImage$thumbnail0000$tWidth$tHeight$oWidth$oHeight);
    
// copy and resize the full-size image into a thumbnail - see PHP.net's imagecopyresized() function for details

    
if(strtolower($info["extension"]) == "jpe" || strtolower($info["extension"]) == "jpeg" || strtolower($info["extension"]) == "jpg")
    {
     
// save a JPG thumbnail!
     
$thumbnail imagejpeg($tempImage$tF $fileName);
    }
    if(
strtolower($info["extension"]) == "gif")
    {
     
// save a GIF thumbnail!
     
$thumbnail imagegif($tempImage$tF $fileName);
    }
    if(
strtolower($info["extension"]) == "png")
    {
     
// save a PNG thumbnail!
     
$thumbnail imagepng($tempImage$tF $fileName);
    }
   }
  }
 }
 
closedir($open);
 
// close the directory
}






function 
listAlbums($gF$aS$dF)
{
 
// $gF is the path to the folder that contains the subfolders
 //     example: "gallery/"

 // $aS is the separator between each album title
 //     example: " &bull; "

 // $dF is the default folder that we wish to exclude from the list
 //     example: "default_gallery"

 
$open opendir($gF);
 
// open the directory to the gallery folder

 
$i 1;
 
$toReturn "";
 
// we use $i to loop through the count and $toReturn to come
 // up with a full album list to echo later
 
while(FALSE !== ($fileName readdir($open)))
 {
  if(
is_dir($gF $fileName) && $fileName != "." && $fileName != ".." && $fileName != $dF)
  {
   
// if the file is actually a directory... ("." and ".." are not directories) and NOT the default
   
$displayName str_replace("_"" "$fileName);
   
// change all underscores to spaces in the display name
   
$displayName ucwords($displayName);
   
// capitalize the first letter of each word in the display name
   
$toReturn $toReturn "<a href=\"?a=" $fileName "\">" $displayName "</a>" $aS;
   
// add the album name, and add a separator
  
}
 }
 
$aS_length strlen($aS);
 
// here we figure out how many characters $aS is
 
$aS_length $aS_length * -1;
 
// get the negative number
 
echo substr_replace($toReturn""$aS_length);
 
// this is a messy but understandable way to remove the last
 // album separator from the list

}
?>
Lo que quiero es que "opendir" ordene las imagenes por la fecha en la que han subido las imagenes. Como lo hago?
Gracias de antemano.
  #2 (permalink)  
Antiguo 21/04/2009, 14:51
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Poner orden a las imagenes de mi galería.

Tendrias que modificar el script para que cargues las imagenes en una matriz (array), depues las ordenas por nombre: sort(), despues haces un bucle foreach() donde recorras la matriz y muestres las imagenes.
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 21/04/2009, 14:57
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Poner orden a las imagenes de mi galería.

pues no es tan sencillo como decir: quiero que las ordene!

y tampoco un simple sort() basta para ordenar por otras características...


al ser una función nativa de PHP desconozco sus reglas de ordenación...

además, no se puede ir ordenando mientras se lee con readdir()


lo mejor, es re-implementar las funciones para obtener este nuevo comportamiento

específicamente, me refiero a createGallery()


primero, debes abrir con opendir() e ir agregando archivo por archivo a un array() asociativo, con el nombre como índice y su tamaño (o fecha de modificación, acceso, creación, etc...) como valor....

ahora debes ordenarlo con alguna funcion de ordenación de arrays.... y ya tienes listo tu array con los archivos necesarios...

este array debes usarlo en lugar de las llamadas a opendir(), readdir(), closedir()....

un simple foreach() y deberías comenzar a imprimir el HTML necesario...

esto solo es necesario en la funcion de impresión de las imágenes al documento, no mas...

(si desconoces algún termino o función, por favor dirigete a consultar el manual de PHP)


suerte!
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:23.