Ver Mensaje Individual
  #3 (permalink)  
Antiguo 25/11/2003, 09:43
Avatar de touza
touza
 
Fecha de Ingreso: febrero-2003
Ubicación: barcelona
Mensajes: 65
Antigüedad: 21 años, 3 meses
Puntos: 0
Una alternativa...

Curioseando por hotscripts encontré un .php que, incluido dentro de la plantilla portal_body.tpl, me muestra dentro del portal una foto al azar de entre las que han subido los usuarios a los foros.Lo he probado y funciona ( podéis ver como funciona aquí: http://www.jrey.net/foros/portal.php ), simplemente tienes que incluir ese archivo php dentro de la carpeta que tienes asignada para los "attachments" ( "files" por defecto ), y su código es este:

<?
/*
|-------------------------------------------------------------------------------------------------------|
| JARD : Just Another Random Displayer |
| Copyright © 2003 - Alessandro Pellegrini ([email protected]) |
| You are allowed to use, edit and copy this script. Just leave this copyright notice intact. |
|-------------------------------------------------------------------------------------------------------|
| Syntax: <img src="rid.php"> ===> Displays a random image |
| <img src="rid.php?tnsize=XXX"> ===> Displays a random image of a given |
| size. |
|-------------------------------------------------------------------------------------------------------|
| How to use: |
| Put this file in a directory containing all the images you want to display |
| randomly. If there are no-image files, the script won't consider them. |
| Then connect to this script directly from the addres bar or trought an <img> tag in a |
| html page. |
|-------------------------------------------------------------------------------------------------------|
| What this script supports: |
| This script can handle any gif, jpeg, and png image file. There is no bmp support as long |
| as I think bmp is not a great format for the web. Maybe in a further version I'll add it, or |
| you may add it yourself... |
|-------------------------------------------------------------------------------------------------------|
| Requirements: |
| This script requires GD Libraries. If you want JPG support, you must have installed |
| version 1.6.2 or lower, as long as the JPEG group asked to remove this support due to |
| Copyright motivations... |
| To use any other support, you must have at least PHP 4.3.0 on your server... |
|-------------------------------------------------------------------------------------------------------|
| FOR ANY QUESTION OR BUG REPORT, CONTACT ME AT [email protected] |
|-------------------------------------------------------------------------------------------------------|
*/


// Opens the current folder
$folder=opendir(".");

// Stores all the files in the directory and counts their number
while ($file = readdir($folder))
$names[count($names)] = $file;

// Closes the directory
closedir($folder);

// Sorts the names alphabetically
sort($names);

// Removes all non-image files
$tempvar=0;
for ($i=0;$names[$i];$i++){
$ext=strtolower(substr($names[$i],-4));
if ($ext==".jpg"||$ext==".gif"||$ext=="jpeg"||$ext==" .png"){$names1[$tempvar]=$names[$i];$tempvar++;}
}

// Choses one random image
srand ((double) microtime() * 10000000);
$rand_keys = array_rand ($names1, 2);
$slika=$names1[$rand_keys[0]];

// Checks if you've choosen to resize the image
if (!isset($tnsize)) {

// If not, it displays the choosen image
header ("Location: $slika");

} else {

// Checks if the given size is valid
$tnsize = (integer) $tnsize;
if (($tnsize<20) or ($tnsize>300)){

// If not, it creates an error image and displays it
$img = ImageCreate(100, 100);
$red = ImageColorAllocate($img, 255, 0, 0);
$yellow = ImageColorAllocate($img, 255,255, 0);
ImageString($img, 4, 20, 20, "Dimensione", $yellow);
ImageString($img, 4, 20, 40, "fornita", $yellow);
ImageString($img, 4, 20, 60, "errata!", $yellow);
ImagePNG($img);
ImageDestroy($img);
exit();
}

// We're now sure the image is valid. Now we have to resize it, we're about to use GD libraries.

// It now checks the image format and loads it into a variable
if ($ext==".jpg" || $ext=="jpeg"){
$bigimage = @ImageCreateFromJPEG($slika);
}
if ($ext==".gif"){
$bigimage = @ImageCreateFromGIF($slika);
}
if ($ext==".png" || $ext=="jpeg"){
$bigimage = @ImageCreateFromPNG($slika);
}

// It now creates an empty image of the given size

$tnimage = ImageCreate($tnsize, $tnsize);
$darkblue = ImageColorAllocate($tnimage, 0,0, 127);
ImageColorTransparent($tnimage,$darkblue);

// It now calculates the resizing image factor
$sz = GetImageSize($slika);
$x = $sz[0];
$y = $sz[1];
if ($x>$y) {
$dx = 0;
$w = $tnsize;
$h = ($y / $x) * $tnsize;
$dy = ($tnsize - $h) / 2;
}else{
$dy = 0;
$h = $tnsize;
$w = ($x / $y) * $tnsize;
$dx = ($tnsize - $w) / 2;
}

// Resizes the image
ImageCopyResized($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h,$x, $y);

// Displays the image
ImagePNG($tnimage);

// Clears the variables
ImageDestroy($tnimage);
ImageDestroy($bigimage);
}

?>


Como véis es muy completo, ya que se supone que utiliza las librerías GD del servidor y todo. Esto significa que no me aparecen las fotos a lo bruto,sino en el tamaño que yo le indique. Así pues, lo que yo hice fue incluir dentro de portal_body.tpl esta tabla:

<table width="100%" cellpadding="2" cellspacing="1" border="0" class="forumline">
<tr>
<td class="catHead" height="25"><span class="genmed"><b>Ultima foto subida</b></span></td>
</tr>
<tr>
<td class="row1" align="center"><a href="_self" target="_blank"><img src="http://www.midominio.com/carpetadelforo/files/elscript.php" width="130" height="130" alt="Ultima foto subida" border="0" vspace="3"></a></td>
</tr>
</table>

De este modo he logrado que en el portal me aparezca la susodicha "tabla" con una foto de la carpeta "files" escogida al azar y con las dimensiones que le he marcado en este caso ( 130 por 130 ). Pero claro, me falta por definir lo siguiente:

-Primero, me gustaría que la foto apareciese redimensionada proporcionalmente, ya que poniendo un tamaño fijo de ancho y alto como 130 por 130 ó 100 por 100 pues, a no ser que la foto original sea cuadrada, me va a salir siempre desproporcionada de un lado o de otro.

-Lo segundo que me gustaría es que en vez de escoger una foto al azar escogiese la última de las que se han subido a la carpeta files ( osea a los foros ). Yo creo que esto no debe ser muy difícil, sería cuestión de indicarle que escoja la imagen con la fecha más reciente ( aunque yo eso no sé hacerlo ).

-Y tercero, y quizás esto sea lo más difícil por no decir imposible: que al pinchar en la foto me lleve directamente al mensaje en el que está incluida. Como habréis visto yo le he puesto "_blank"en el enlace , pero es como no poner nada.

En fin, puede que todo esto no sean más que cábalas y no tenga nada que ver, pero en un principio me pareció una buena alternativa para no tener que meterse con rutas a la base de datos que no hay quien entienda.


Saludos.
__________________
Mi web es http:www.jrey.net