Visualizzazione dei risultati da 1 a 3 su 3

Discussione: immagini jpg, gif png

  1. #1

    immagini jpg, gif png

    Salve a tutti,

    ho questa situazione dalla quale non riesco ad uscire...

    Il seguente script funziona perfettamente per il resize delle immagini jpg ma, volendo gestire anche immagini con estensione gif e png, ho provato a gestire la problematica con switch... senza raggiungere alcun risultato... qualcuno ha qualche suggerimento ? Posto qui perché sono ormai diversi giorni che non riesco a venirne a capo...

    Codice PHP:
    <?php
    function createThumbs$pathToImages$pathToThumbs$thumbWidth 
    {
    //$estensioni = array('jpg','jpeg','gif','GIF','JPG','JPEG');
      // open the directory
      
    $dir = @opendir$pathToImages );

      
    // loop through it, looking for any/all JPG files:
      
    while (false !== ($fname = @readdir$dir ))) {
        
    // parse path for the extension
        
    $info pathinfo($pathToImages $fname);
        
    $fname str_replace(" """$fname);
        
    $fname str_replace("%20"""$fname);
        
        
    // continue only if this is a JPEG image
      //  if ( strtolower($info['extension']) == 'jpg' || 'GIF' || 'gif' || 'JPEG') 
       //{
         // echo "Ho creato miniatura per immagine: {$fname} 
    ";

    switch(
    $type)
        {
            case 1: 
    $img = @imagecreatefromjpeg( "{$pathToImages}{$fname}" ); break;
            case 2: 
    $img = @imagecreatefrompng( "{$pathToImages}{$fname}" ); break;
            case 3: 
    $img = @imagecreatefromgif( "{$pathToImages}{$fname}" ); break;
        }


          // load image and get image size
         // 
    $img = @imagecreatefromjpeg( "{$pathToImages}{$fname}" );
          
    $width = @imagesx( $img );
          
    $height = @imagesy( $img );

          // calculate thumbnail size
          
    $new_width = $thumbWidth;
          
    $new_height = @floor( $height * ( $thumbWidth / $width ) );

          // create a new temporary image
          
    $tmp_img = @imagecreatetruecolor( $new_width$new_height );

          // copy and resize old image into new image 
          @imagecopyresized( 
    $tmp_img$img, 0, 0, 0, 0, $new_width$new_height$width$height );

          // save thumbnail into a file
          switch(
    $type2)
        {
            case 1:  @imagejpeg( 
    $tmp_img, "{$pathToThumbs}{$fname}" ); break;
            case 2:  @imagepng( 
    $tmp_img, "{$pathToThumbs}{$fname}" ); break;
            case 3:  @imagegif( 
    $tmp_img, "{$pathToThumbs}{$fname}" ); break;
        }
          
         //@imagejpeg( 
    $tmp_img, "{$pathToThumbs}{$fname}" );

    //    }
      }
      // close the directory
      @closedir( 
    $dir );
    }
    // call createThumb function and pass to it as parameters the path 
    // to the directory that contains images, the path to the directory
    // in which thumbnails will be placed and the thumbnail's width. 
    // We are assuming that the path will be a relative path working 
    // both in the filesystem, and through the web for links
    createThumbs("
    img/","thumbs/",100);
    ?>
    ah... dimenticavo... grazie

  2. #2
    Ho modificato lo script così:

    Codice PHP:

    <?php
    function createThumbs$pathToImages$pathToThumbs$thumbWidth 
    {
    //$estensioni = array('jpg','jpeg','gif','GIF','JPG','JPEG');
      // open the directory
      
    $dir = @opendir$pathToImages );

      
    // loop through it, looking for any/all JPG files:
      
    while (false !== ($fname = @readdir$dir ))) {
        
    // parse path for the extension
        
    $info pathinfo($pathToImages $fname);
        
    $fname str_replace(" """$fname);
        
    $fname str_replace("%20"""$fname);
        
        
    // continue only if this is a JPEG image
      //  if ( strtolower($info['extension']) == 'jpg' || 'GIF' || 'gif' || 'JPEG') 
       //{
         // echo "Ho creato miniatura per immagine: {$fname} 
    ";



    $nome_file = $fname;
     

    $ext = strtolower(substr($nome_file, strrpos($nome_file, "."), strlen($nome_file)-strrpos($nome_file, ".")));
    $ext = str_replace(".","",$ext);

    return 
    $ext;


    switch(
    $ext)
        {
            case 'jpeg': 
    $img = @imagecreatefromjpeg( "{$pathToImages}{$fname}" ); break;
            case 'png': 
    $img = @imagecreatefrompng( "{$pathToImages}{$fname}" ); break;
            case 'gif': 
    $img = @imagecreatefromgif( "{$pathToImages}{$fname}" ); break;
        }


          // load image and get image size
         // 
    $img = @imagecreatefromjpeg( "{$pathToImages}{$fname}" );
          
    $width = @imagesx( $img );
          
    $height = @imagesy( $img );

          // calculate thumbnail size
          
    $new_width = $thumbWidth;
          
    $new_height = @floor( $height * ( $thumbWidth / $width ) );

          // create a new temporary image
          
    $tmp_img = @imagecreatetruecolor( $new_width$new_height );

          // copy and resize old image into new image 
          @imagecopyresized( 
    $tmp_img$img, 0, 0, 0, 0, $new_width$new_height$width$height );

          // save thumbnail into a file
          switch(
    $tmp_img)
        {
            case 'jpeg':  @imagejpeg( 
    $tmp_img, "{$pathToThumbs}{$fname}" ); break;
            case 'png':  @imagepng( 
    $tmp_img, "{$pathToThumbs}{$fname}" ); break;
            case 'gif':  @imagegif( 
    $tmp_img, "{$pathToThumbs}{$fname}" ); break;
        }
          
         //@imagejpeg( 
    $tmp_img, "{$pathToThumbs}{$fname}" );

    //    }
      }
      // close the directory
      @closedir( 
    $dir );
    }
    // call createThumb function and pass to it as parameters the path 
    // to the directory that contains images, the path to the directory
    // in which thumbnails will be placed and the thumbnail's width. 
    // We are assuming that the path will be a relative path working 
    // both in the filesystem, and through the web for links
    createThumbs("
    img/","thumbs/",100);
    ?>
    ma non vengono caricate nemmeno le jpg !

  3. #3

    RISOLTO

    Salve a tutti,

    ho poi risolto il problema presentato. Ecco la soluzione: (ho utilizzato degli IF) cercherò poi di utilizzare uno SWITCH più elegante.

    Codice PHP:

    <?php
    function createThumbs$pathToImages$pathToThumbs$thumbWidth 
    {
    //$estensioni = array('jpg','jpeg','gif','GIF','JPG','JPEG');
      // open the directory
      
    $dir = @opendir$pathToImages );

      
    // loop through it, looking for any/all JPG files:
      
    while (false !== ($fname = @readdir$dir ))) {
        
    // parse path for the extension
        
    $info pathinfo($pathToImages $fname);
        
    $fname str_replace(" """$fname);
        
    $fname str_replace("%20"""$fname);
        
        

    $ext strtolower(substr($fnamestrrpos($fname"."), strlen($fname)-strrpos($fname".")));
    $ext str_replace(".","",$ext);



    /*
    switch($ext)
        {
            case 'jpeg': case'jpg': $img = @imagecreatefromjpeg( "{$pathToImages}{$fname}" ); break;
            case 'png': $img = @imagecreatefrompng( "{$pathToImages}{$fname}" ); break;
            case 'gif': $img = @imagecreatefromgif( "{$pathToImages}{$fname}" ); break;
        }


          // load image and get image size gestisce jpeg gif e png
             */  
            
    if (($ext == 'jpg') || ($ext == 'jpeg') || ($ext == 'JPG') || ($ext == 'JPEG'))  {
              
    $img = @imagecreatefromjpeg"{$pathToImages}{$fname});
          
    $width = @imagesx$img );
          
    $height = @imagesy$img );
    }


            if ((
    $ext == 'gif') || ($ext == 'GIF'))  {
              
    $img = @imagecreatefromgif"{$pathToImages}{$fname});
          
    $width = @imagesx$img );
          
    $height = @imagesy$img );
    }

            if ((
    $ext == 'x-png') || ($ext == 'png') || ($ext == 'PNG'))  {
              
    $img = @imagecreatefrompng"{$pathToImages}{$fname});
          
    $width = @imagesx$img );
          
    $height = @imagesy$img );
    }




          
    // calculate thumbnail size
          
    $new_width $thumbWidth;
          
    $new_height = @floor$height * ( $thumbWidth $width ) );

          
    // create a new temporary image
          
    $tmp_img = @imagecreatetruecolor$new_width$new_height );

          
    // copy and resize old image into new image 
          
    @imagecopyresized$tmp_img$img0000$new_width$new_height$width$height );

           
       
        if ((
    $ext == 'jpg') || ($ext == 'jpeg') || ($ext == 'JPG') || ($ext == 'JPEG'))  {
       @
    imagejpeg$tmp_img"{$pathToThumbs}{$fname});
    }

    if ((
    $ext == 'gif') || ($ext == 'GIF'))  {
    @
    imagegif$tmp_img"{$pathToThumbs}{$fname});

    }

    if ((
    $ext == 'x-png') || ($ext == 'png') || ($ext == 'PNG'))  {
    @
    imagepng$tmp_img"{$pathToThumbs}{$fname});

    }

    //    }
      
    }
      
    // close the directory
      
    @closedir$dir );
    }
    // call createThumb function and pass to it as parameters the path 
    // to the directory that contains images, the path to the directory
    // in which thumbnails will be placed and the thumbnail's width. 
    // We are assuming that the path will be a relative path working 
    // both in the filesystem, and through the web for links
    createThumbs("img/","thumbs/",100);
    ?>

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.