Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    visualizzare immagini da DB rimpicciolite?

    ciao,
    ho delle immagini in una cartella ed il nome del file salvato in un database assieme a dei dati.

    Siccome l'immagine di suo ha dimensioni 400x400px circa volevo visualizzare nell'elenco dei prodotti un'immagine di 100x100px circa.

    Volevo sapere se usando le GD la cosa è fattibile...
    cioè riesco a:
    1 - prendere le immagini grandi
    2 - ridimensionarle a seconda dell'altezza o larghezza
    3 - visualizzarle rimpicciolite

    risparmio il tempo di caricamento che sarebbe voluto x visualizzare quelle grandi???

  2. #2
    Utente di HTML.it L'avatar di _kime_
    Registrato dal
    Sep 2003
    Messaggi
    311
    io avevo trovato questo...
    Codice PHP:
        /**
        * Thumbnail Class
        *
        * Creates resized (preview-) images
        *
        * Usage Example:
        * $img = new Thumbnail();
        * echo $img->create_tag("../images/photos/sunflower.jpg");
        *
        * @author   Philipp v. Criegern <criegep@criegern.com>
        * @version  1.0 20.02.2002
        */
        
    class Thumbnail
        
    {
            
    /**
            * Directory where the created thumbnails are stored in (for PHP access)
            * e.g. '/usr/local/apache/htdocs/images/thumbnails'
            * Can be overwritten by global configuration array $_CONFIG['thumbnail_dir_internal']
            *
            * @access public
            */
            
    var $thumbnail_dir_internal  =  '';

            
    /**
            * Path to thumbnail directory for browser access
            * e.g. '/images/thumbnails'
            * Can be overwritten by global configuration array $_CONFIG['thumbnail_dir_external']
            *
            * @access public
            */
            
    var $thumbnail_dir_external  =  '';

            
    /**
            * Maximum pixel width of created thumbnail
            *
            * @access public
            */
            
    var $max_width               =  120;

            
    /**
            * Maximum pixel height of created thumbnail
            *
            * @access public
            */
            
    var $max_height              =  120;

            
    /**
            * File name of created thumbnail
            * e.g. 'sunflower.png'
            *
            * @access public
            */
            
    var $image_name;

            
    /**
            * Complete image tag for created thumbnail
            * e.g. '[img]/images/thumbnails/sunflower.png[/img]'
            *
            * @access public
            */
            
    var $image_tag;

            
    /**
            * Error message if creation fails
            *
            * @access public
            */
            
    var $error;

            
    /**
            * Pixel width of created thumbnail
            *
            * @access public
            */
            
    var $width;

            
    /**
            * Pixel height of created thumbnail
            *
            * @access public
            */
            
    var $height;


            
    /**
            * Thumbnail Constructor
            *
            * @access public
            */
            
    function Thumbnail ()
            {
                global 
    $_CONFIG;

                if (!empty(
    $_CONFIG['thumbnail_dir_internal']))
                {
                    
    $this->thumbnail_dir_internal  =  $_CONFIG['thumbnail_dir_internal'];
                }
                if (!empty(
    $_CONFIG['thumbnail_dir_external']))
                {
                    
    $this->thumbnail_dir_external  =  $_CONFIG['thumbnail_dir_external'];
                }
            }

            
    /**
            * Create Thumbnail and return Image Name
            *
            * @access public
            * @param string $parameter Filename of source image
            * @return string Filename of created thumbnail
            */
            
    function create_name $parameter '' )
            {
                
    $this->create$parameter );
                return 
    $this->image_name;
            }

            
    /**
            * Create Thumbnail and return Image Tag
            *
            * @access public
            * @param string $parameter Filename of source image
            * @return string Complete HTML Image-Tag of created thumbnail
            */
            
    function create_tag $parameter '' )
            {
                
    $this->create$parameter );
                return 
    $this->image_tag;
            }

            
    /**
            * Create Thumbnail
            *
            * @access private
            * @param string $parameter Filename of source image
            * @return void
            */
            
    function create $imagefile )
            {
                if (
    strlen($this->thumbnail_dir_internal)  &&  (substr($this->thumbnail_dir_internal, -1) != '/'))
                {
                    
    $this->thumbnail_dir_internal  .=  '/';
                }
                if (
    strlen($this->thumbnail_dir_external)  &&  (substr($this->thumbnail_dir_external, -1) != '/'))
                {
                    
    $this->thumbnail_dir_external  .=  '/';
                }
                
    $tmp_name  =  basename($imagefile);
                if (
    strtolower(substr($tmp_name, -4)) == '.jpg')
                {
                    
    $this->image_name  =  substr($tmp_name0, -4) . '.png';
                    if (!
    is_file($this->thumbnail_dir_internal $this->image_name))
                    {
                        
    $old    =  ImageCreateFromJPEG($imagefile);
                        
    $old_w  =  ImageSX($old);
                        
    $old_h  =  ImageSY($old);
                        
    $this->width   =  $old_w;
                        
    $this->height  =  $old_h;
                        if (
    $this->max_width  &&  ($this->width $this->max_width))
                        {
                            
    $this->height  =  round($this->height $this->max_width $this->width);
                            
    $this->width   =  $this->max_width;
                        }
                        if (
    $this->max_height  &&  ($this->height $this->max_height))
                        {
                            
    $this->width   =  round($this->width $this->max_height $this->height);
                            
    $this->height  =  $this->max_height;
                        }
                        
    $new  =  imagecreate($this->width$this->height);
                        
    imagecopyresized($new$old0,00,0$this->width$this->height$old_w$old_h);
                        
    ImageJPEG($new$this->thumbnail_dir_internal $this->image_name);
                        
    ImageDestroy($new);
                        
    ImageDestroy($old);
                    }
                    else
                    {
                        
    $arr  =  getimagesize($this->thumbnail_dir_internal $this->image_name);
                        
    $this->width   =  $arr[0];
                        
    $this->height  =  $arr[1];
                    }
                    
    $this->image_tag  =  '<IMG SRC="' $this->thumbnail_dir_external $this->image_name 
                                       
    '" WIDTH="'  $this->width
                                       
    '" HEIGHT="' $this->height
                                       
    '" BORDER="0">';
                }
                else
                {
                    
    $this->error  =  "Filetype not JPG";
                }
            }
        } 
    + / Powered by Ubuntu 4.10 "The Warty Warthog"

    + / Manuale PHP.net

    + / Think Free

  3. #3
    l'ho provata, ma mi dai dei problemi... nn capisco il xchè mi visualizza l'immagine grande invece che piccola.


    P.S: ho modificato le maiuscole delle funzioni GD xchè ho una vers superiore...

  4. #4
    Ti posto tutti i file che uso io:

    codice:
    [img]thumb.php?blobId=1[/img]
    thumb.php
    codice:
    <?
    global $blobId;
    if(!is_numeric($blobId))
    	die("blobId non valido");
    
    include 'connect.inc.php';
    
    $dbQuery = "SELECT blob_type, blob_data FROM file WHERE id_file = $blobId";
    
    $result = mysql_query($dbQuery) or die("Couldn't get file list");
    
    $fileType = @mysql_result($result, 0, "blob_type");
    $fileContent = @mysql_result($result, 0, "blob_data");
    header("Content-type: $fileType");
    
    	
    include 'resize.php';
    	
    $img_res = imagecreatefromstring($fileContent);
    $new_img_res = resize_image($img_res, 60, 80);
    unset($img_res);
    print_image($new_img_res, 100);
    unset($new_img_res);
    ?>
    connect.inc.php -> File di connessione al db
    codice:
    <?
    // parametri del database
    $db_host = "localhost";
    $db_user = "db_user";
    $db_password = "db_password";
    $db_name = "db_name";
    
    mysql_connect($db_host, $db_user, $db_password)or die("Errore nella connessione: ".mysql_error());
    mysql_select_db($db_name) or die("Errore nella selezione del database: ".mysql_error());
    ?>
    resize.php -> Fa il resize dell'immagine
    codice:
    <?
    function resize_image($img_res, $maxX, $maxY) {
      $actualX=imagesx($img_res);
      $actualY=imagesy($img_res);
      $newX=$maxX;
      $newY=$maxY;
      $tmp_img_res = imagecreatetruecolor($newX, $newY);
      $res = imagecopyresampled($tmp_img_res, $img_res, 0, 0, 0, 0, $newX, $newY, $actualX, $actualY);
      return $tmp_img_res;
    }
    
    function save_image($img_res, $filename, $quality=80) {
      imagejpeg($img_res, $filename, $quality);
    }
    
    function print_image($img_res, $quality=100) {
      header('Content-Type: image/jpeg');
      imagejpeg($img_res,NULL, $quality);
    }
    ?>
    Devi naturalmente modificare la query, i nomi delle variabili, e naturalmente le dimensioni che vuoi in resize.php a seconda dei nomi dei campi che hai nel tuo db, per il resto è tutto a posto.

    Talvolta anche una persona apparentemente inutile si rivela un abile samurai dalla forza di mille uomini, dimostrando di poter rinunciare alla vita e che il suo cuore si è completamente identificato con quello del suo padrone

  5. #5
    puoi farlo tranquillamente.
    Io avevo postato qualche cosa qualche tempo fa:
    http://forum.html.it/forum/showthrea...49#post6009349

    Devi comunque assicurarti di avere il supporto per le gd2 sul php.

  6. #6
    in alternativa puoi usare questo resize.php che ti mantiene costanti le proporzioni senza sfigurarti l'immagine:

    codice:
    <?
    function resize_image($img_res, $maxX, $maxY) {
      $actualX=imagesx($img_res);
      $actualY=imagesy($img_res);
    		
      if ($actualY>$maxY){
        $newY = $maxY;
        $newX = ($actualX/$actualY)*$newY;
        if ($newX>$maxX)
          $newX = $maxX; $newY = ($actualY/$actualX)*$newX;
      }elseif ($actualX>$maxX){
        $newX = $maxX;
        $newY = ($actualY/$actualX)*$newX;
        if ($newY>$maxY)
          $newY = $maxY; $newX=($actualX/$actualY)*$newY;
      }else{
        $newX = $actualX;
        $newY = $actualY;
      }
      $tmp_img_res = imagecreatetruecolor($newX, $newY);
    		
      $res = imagecopyresampled($tmp_img_res, $img_res, 0, 0, 0, 0, $newX, $newY, $actualX, $actualY);
    
      return $tmp_img_res;
    }
    
    function save_image($img_res, $filename, $quality=100) {
      imagejpeg($img_res, $filename, $quality);
    }
    
    function print_image($img_res, $quality=100) {
      header('Content-Type: image/jpeg');
      imagejpeg($img_res,NULL, $quality);
    }
    ?>
    Talvolta anche una persona apparentemente inutile si rivela un abile samurai dalla forza di mille uomini, dimostrando di poter rinunciare alla vita e che il suo cuore si è completamente identificato con quello del suo padrone

  7. #7
    p.s. non vorrei prendermi il merito di quelle funzioni che non ho fatto io, per correttezza ti posto il link della discussione a cui fanno riferimento: http://forum.html.it/forum/showthrea...hreadid=592358

    Thank's to daniele_dll
    Talvolta anche una persona apparentemente inutile si rivela un abile samurai dalla forza di mille uomini, dimostrando di poter rinunciare alla vita e che il suo cuore si è completamente identificato con quello del suo padrone

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.