Visualizzazione risultati 1 fino 4 di 4

Discussione: Thumbnail casuali in PHP

  1. #1
    Guest

    Predefinito Thumbnail casuali in PHP

    Salve, ho scovato questo script su internet per prelevare un'immagine a caso da una cartella (in questo caso /images/)

    Codice PHP:
    <?php
    /*******************************************************************************
    * Title: Random image script (RandIm)
    * Version: 1.0 @ January 3, 2009
    * Author: Klemen Stirn
    * Website: http://www.phpjunkyard.com
    ********************************************************************************
    * COPYRIGHT NOTICE
    * Copyright 2009 Klemen Stirn. All Rights Reserved.
    *
    * This script may be used and modified free of charge by anyone
    * AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT.
    * By using this code you agree to indemnify Klemen Stirn from any
    * liability that might arise from it's use.
    *
    * If you are using this script you are required to place a link
    * to PHPJunkyard on your website. You will find some link suggestions here:
    * http://www.phpjunkyard.com/link2us.php
    *
    * Selling the code for this program, in part or full, without prior
    * written consent is expressly forbidden.
    *
    * Obtain permission before redistributing this software over the Internet
    * or in any other medium. In all cases copyright and header must remain
    * intact. This Copyright is in full effect in any country that has
    * International Trade Agreements with the United States of America or
    * with the European Union.
    *******************************************************************************/

    /*******************************************************************************
    * SETTINGS
    *
    * See readme.htm file for further instructions!
    *******************************************************************************/

    /* The default folder with images */
    $settings['img_folder'] = 'images/';

    /* File types (extensions) to display */
    $settings['img_ext'] = array('.jpg','.gif','.png');

    /*
    How to display the images?
    0 = print just the image path (for includes), like: images/test.jpg
    1 = redirect to the image, when using: <img src="randim.php" />
    */
    $settings['display_type'] = 1;

    /* Allow on-the-fly settings override? 0 = NO, 1 = YES */
    $settings['allow_otf'] = 1;


    /*******************************************************************************
    * DO NOT EDIT BELOW...
    *
    * ...or at least make a backup before you do!
    *******************************************************************************/

    /* Override type? */
    if ($settings['allow_otf'] && isset($_GET['type']))
    {
    $type = intval($_GET['type']);
    }
    else
    {
    $type = $settings['display_type'];
    }

    /* Override images folder? */
    if ($settings['allow_otf'] && isset($_GET['folder']))
    {
    $folder = htmlspecialchars(trim($_GET['folder']));
    if (!
    is_dir($folder))
    {
    $folder = $settings['img_folder'];
    }
    }
    else
    {
    $folder = $settings['img_folder'];
    }

    /* Make sure images fodler ends with an '/' */
    if (substr($folder,-1) != '/')
    {
    $folder.='/';
    }

    /* Get a list of all the image files */
    $flist = array();
    foreach(
    $settings['img_ext'] as $ext)
    {
    $tmp = glob($folder.'*'.$ext);
    if (
    is_array($tmp))
    {
    $flist = array_merge($flist,$tmp);
    }
    }

    /* If we have any images choose a random one, otherwise select the "noimg.gif" image */
    if (count($flist))
    {
    $src = $flist[array_rand($flist)];
    }
    else
    {
    $src = 'noimg.gif';
    }

    /* Output the image according to the selected type */
    if ($type)
    {
    header('Location:'.$src);
    exit();
    }
    else
    {
    echo
    $src;
    }
    ?>
    Fin qui niente di errato... Il problema è che come output, quando vado a fare il thumbnail io uso questo:
    Codice:
    <a href="randim.php"><img src="randim.php" width="200" height="150"></a>
    E quindi, anche se genera il thumbnail, quando clicco sull'immagine viene prelevata un'altra immagine a caso, diversa da quella mostrata in miniatura! Come rimedio?

  2. #2
    Guest

    Predefinito

    Purtroppo così facendo stampa proprio testualmente il percorso dell'immagine, tipo /images/img1.jpg...... Anche sapendo questo, non c'è modo di usarlo come dicevo io?

  3. #3
    L'avatar di javascripter
    javascripter non è connesso Moderatore
    Data registrazione
    14-02-2010
    Messaggi
    1,114

    Predefinito

    Modifica lo script così:
    Codice PHP:
    <?php
    session_start
    ();

    function
    filter($v) {
    global
    $ext;
    $e = end(explode('.', $v));

    if(
    in_array($e, $ext))
    return
    true;
    return
    false;
    }

    /* configurazioni */
    $path = 'cartella/'; // con lo slash finale.
    $ext = array('jpg', 'gif', 'png');
    /* fine configurazioni */

    $files = array_filter(glob($path . '*.*'), 'filter');

    if(isset(
    $_GET['c'], $_SESSION['img']))
    $img = $_SESSION['img'];
    else
    $img = $files[rand(0, count($files) - 1)];

    $_SESSION['img'] = $img;

    header('Location: ' . $img);
    ?>
    E per fare il link usi:
    Codice HTML:
    <a href="randim.php?c"><img src="randim.php" width="200" height="150" alt="casuale"></a>

  4. #4
    Guest

    Predefinito

    E' semplicemente perfetto! Grazie mille!

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •