Visualizzazione dei risultati da 1 a 7 su 7

Discussione: thumbs jpeg, gif, png

  1. #1

    thumbs jpeg, gif, png

    Salve a tutti,

    ho un problema con uno scritto per il resize delle immagini che ha sempre funzionato ma che dopo un mio recente aggiornamento ha cominciato a darmi problemi. il mio aggiornamento consiste nel tentativo con str_replace di riuscire a gestire immagini con errori del tipo: %20, spazi etc...

    con il codice attuale riesco a inserire soltanto immagini png... ( i percorsi su db sono sempre corretti e le immagini originali nella cartella immagini vengono sempre caricati correttamente... il problema si presenta soltanto nel resize...)

    Codice PHP:

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

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

          // load image and get image size
          
    $img = @imagecreatefromjpeg( "{$pathToImages}{$fname}" );
          
    $img = @imagecreatefromgif( "{$pathToImages}{$fname}" );
          
    $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 );
     //
    $img = str_replace(" ", "", $img);
          // copy and resize old image into new image 
          @imagecopyresized( 
    $tmp_img$img, 0, 0, 0, 0, $new_width$new_height$width$height );
     //
    $tmp_img = str_replace(" ", "", $tmp_img);
      //
    $fname = str_replace(" ", "", $fname);
          // save thumbnail into a file
         @imagejpeg( 
    $tmp_img, "{$pathToThumbs}{$fname}" );
          @imagegif( 
    $tmp_img, "{$pathToThumbs}{$fname}" );
             @imagepng( 
    $tmp_img, "{$pathToThumbs}{$fname}" );
        }
      }
     
      // close the directory
      @closedir( 
    $dir );
    }
    createThumbs("
    img/","thumbs/",100);
    ?>

  2. #2
    Utente di HTML.it L'avatar di telegio
    Registrato dal
    Sep 2001
    Messaggi
    2,592
    il codice che hai postato, compresi i commenti, che errori ti da?
    sono le parti commentate che non funzionano?

    da profano, 2 cose.
    1 togli le @ per vedere gli errori.
    2 dici che funziona solo con i PNG.
    sarà forse per questo
    $img = @imagecreatefromjpeg( "{$pathToImages}{$fname}" );
    $img = @imagecreatefromgif( "{$pathToImages}{$fname}" );
    $img = @imagecreatefrompng( "{$pathToImages}{$fname}" );
    che sovrascrivi la variabile $img?

  3. #3
    uhm funzionava anche con jpeg e gif... ma con le jpeg permetteva il resize soltanto di quelle che contengono errori di formattazione e superano :

    // $fname = str_replace(" ", "", $fname);
    //$fname = str_replace("%20", "", $fname);


    che attualmente ho escluso...


  4. #4
    Come ti ha già segnalato telegio, stai sovrascrivendo le variabili e non solo...
    Un codice di questo tipo:
    Codice PHP:
          $img = @imagecreatefromjpeg"{$pathToImages}{$fname});
          
    $img = @imagecreatefromgif"{$pathToImages}{$fname});
          
    $img = @imagecreatefrompng"{$pathToImages}{$fname});
    ...
          
    imagejpeg$tmp_img"{$pathToThumbs}{$fname});
          
    imagegif$tmp_img"{$pathToThumbs}{$fname});
          
    imagepng$tmp_img"{$pathToThumbs}{$fname}); 
    non ha alcun senso...

    Fai un controllo sul mime type del file immagine, ed esegui, di volta in volta, solo le funzioni della gdlibrary corrispondenti al quel tipo di immagine.
    Full Stack Developer presso Advice Lab
    Bonus Bitcoin
    Moon Bitcoin

  5. #5
    Allora riepilogo:

    così riesco a caricare tutte le jpeg anche effettuando i controlli str_replace

    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} 
    ";

          // 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
          @imagejpeg( 
    $tmp_img, "{$pathToThumbs}{$fname}" );

        }
      }
      // close the directory
      closedir( 
    $dir );
    }
    createThumbs("
    img/","thumbs/",100);
    ?>
    Come posso replicare il tutto per poter effettuare thumb di immagini gif e png?

  6. #6
    Utente di HTML.it L'avatar di telegio
    Registrato dal
    Sep 2001
    Messaggi
    2,592
    http://www.asciitable.it/mimetypes.asp

    io farei così
    $estensione1 = "image/pjpeg"; //Jpeg type 1
    $estensione2 = "image/jpeg"; //Jpeg type 2
    $estensione3 = "image/gif"; //Gif type
    $estensione4 = "image/png"; //Png type

    if ($_FILES['img1']['type'] == $estensione1 or $_FILES['img1']['type'] == $estensione2) {
    $img = @imagecreatefromjpeg( "{$pathToImages}{$fname}" );
    } elseif ($_FILES['img1']['type'] == $estensione3) {
    $img = @imagecreatefromgif( "{$pathToImages}{$fname}" );
    } elseif ($_FILES['img1']['type'] == $estensione4) {
    $img = @imagecreatefrompng( "{$pathToImages}{$fname}" );
    } else {
    echo "estensione errata";
    }

  7. #7
    Questa riga e' sbagliata:
    Codice PHP:
    if ( strtolower($info['extension']) == 'jpg' || 'GIF' || 'gif' || 'JPEG'
    cioe', non e' sintatticamente sbagliata ma non fa quello che pensi tu (e non fa nulla di utile in generale). Quello che fa e' verificare se l'estensione e' "jpg", se non lo e' verifica se la stringa costante 'GIF' e' vera in un test booleano (lo e'). Quindi in pratica quell'if e' sempre verificato.

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 © 2024 vBulletin Solutions, Inc. All rights reserved.