Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Problema .PNG resizing

  1. #1

    Problema .PNG resizing

    Salve ragazzi!
    Sto cercando di ridimensionare una png/gif 640*100 a 320*50. Ho il seguente codice:

    Codice PHP:
    $allowed_type = array("image/jpeg","image/pjpeg""image/gif""image/x-png""image/png");    $type $_FILES["image_upload_box"]["type"];        
    $type $_FILES["image_upload_box"]["type"];                
    list(
    $image_width$image_height) = getimagesize($_FILES["image_upload_box"]["tmp_name"]);

    if (
    in_array($type$allowed_type) && ($_FILES["image_upload_box"]["size"] < 4000000) && ($image_width == 640) && ($image_height == 100)) {
      
    if(
    $_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){                $image_source imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);        }                

    if(
    $_FILES["image_upload_box"]["type"] == "image/gif"){                
    $image_source imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);        }
    if(
    $_FILES["image_upload_box"]["type"] == "image/bmp"){
                    
    $image_source imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);        }                    
    if(
    $_FILES["image_upload_box"]["type"] == "image/png" || $_FILES["image_upload_box"]["type"] == "image/x-png"){
                
    $image_source imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);        }        
     
    $remote_file "image_files/".rand(0,10000).$_FILES["image_upload_box"]["name"];        

    imagejpeg($image_source,$remote_file,100);        
    chmod($remote_file,0644);        
                                        
    $new_width 320;            
    $new_height 50;                                    
    $new_image imagecreatetruecolor($new_width $new_height);            
    $image_source imagecreatefromjpeg($remote_file);                        

    imagecopyresampled($new_image$image_source0000$new_width$new_height$image_width$image_height);            
    imagejpeg($new_image,$remote_file,100);                        
    imagedestroy($new_image);                        
    imagedestroy($image_source);                
            
    $resized_name basename($remote_file); 
    Purtroppo però, sebbene funzioni perfettamente con immagini jpg, quando cerco di ridimensionare gif o png, mi restituisce un'immagine nera.

    Ho già provato ad usare diverse soluzioni trovate nel forum, ma non riesco ad implementarle nel codice sopra. Avete qualche idea?

    Grazie mille a chi mi aiuterà!!!

  2. #2
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,166
    L'immagine png ha delle parti trasparenti? Di solito dipende da questo.
    Prova a cercare qualcosa riguardo il mantenere la trasparenza dei file png elaborandoli con PHP.

    P.S. Prima di confermare l'invio del post, visualizza l'anteprima e se necessario, adesso lo sarebbe stato, riiformatta il codice correttamente, così è un po' un casino leggerlo.
    Ultima modifica di Alhazred; 25-01-2015 a 11:29
    Earth2: 3QCNSPFQUH

  3. #3
    Ciao Alhazred! Scusa, hai ragione, ora dovrebbe essere meglio:

    Codice PHP:
    $allowed_type = array("image/jpeg","image/pjpeg""image/gif""image/x-png""image/png");
        
    $type $_FILES["image_upload_box"]["type"];
        
        
                
    // get width and height of original image
        
    list($image_width$image_height) = getimagesize($_FILES["image_upload_box"]["tmp_name"]);
     
    $OS $_POST['OS'];
    $pub_name $_POST['pub_name'];
    $lang $_POST['lang'];








        
    // file needs to be jpg,gif,bmp,x-png, 4 MB max and width 320,640.
        
    if (in_array($type$allowed_type) && ($_FILES["image_upload_box"]["size"] < 4000000) && ($image_width == 640) && ($image_height == 100)) {


      


            
            
    // if uploaded image was JPG/JPEG
            
    if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){    
                
    $image_source imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
            }        
            
    // if uploaded image was GIF
            
    if($_FILES["image_upload_box"]["type"] == "image/gif"){    
                
    $image_source imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
            }    
            
    // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)    
            // if uploaded image was BMP
            
    if($_FILES["image_upload_box"]["type"] == "image/bmp"){    
                
    $image_source imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
            }            
            
    // if uploaded image was PNG
            
    if($_FILES["image_upload_box"]["type"] == "image/png" || $_FILES["image_upload_box"]["type"] == "image/x-png"){
                
    $image_source imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
            }
            


        
            
            
    $remote_file "image_files/".rand(0,10000).$_FILES["image_upload_box"]["name"];
            
    imagejpeg($image_source,$remote_file,100);
            
    chmod($remote_file,0644);
        
        


        
            
                
                
    $new_width 320;
                
    $new_height 50;
                
                
                
    $new_image imagecreatetruecolor($new_width $new_height);
                
    $image_source imagecreatefromjpeg($remote_file);
                


                
                
    imagecopyresampled($new_image$image_source0000$new_width$new_height$image_width$image_height);
                
    imagejpeg($new_image,$remote_file,100);
                
                
    imagedestroy($new_image);
                    
            
    imagedestroy($image_source);
            
            
    //final file name
            
    $resized_name basename($remote_file); 
    Ho trovato alcune soluzioni, tipo imagesavealpha(), ma non so in che parte del codice dovrei inserirla.
    Il problema persiste solo con le immagini trasparenti, ma la maggiore utilità dello script dovrebbe essere proprio con queste ultime. Qualche idea?

  4. #4
    Raga, ho trovato una funzione che fa al caso mio. L'ho quindi adattata alle mie esigenze, ma ho un piccolo problema, non so come dare un nome dinamico al nuovo file creato. Sto ancora imparando ad utilizzare le funzioni, so che è sicuramente qualcosa di semplicissimo, ma non riesco:

    Codice PHP:
    function resizeImage$file$width 0$height 0$proportional false$output 'file') {
        
    $info getimagesize($file);    $image '';    
    $final_width 0;    $final_height 0;    
    list(
    $width_old$height_old) = $info;    
    if (
    $proportional) {        
    if (
    $width == 0$factor $height/$height_old;        
    elseif (
    $height == 0$factor $width/$width_old;        
    else 
    $factor min $width $width_old$height $height_old);          
    $final_width round ($width_old $factor);        
    $final_height round ($height_old $factor);    }    
    else {    if ((
    $width_old == 320) && ($height_old == 50)){        
    $final_width 320;        
    $final_height 50;    }
    elseif((
    $width_old == 640) && ($height_old == 100)){        
    $final_width 320;        
    $final_height 50;    }
               }    
    switch ( 
    $info[2] ) {        
    case 
    IMAGETYPE_GIF:            $image imagecreatefromgif($file);        break;        
    case 
    IMAGETYPE_JPEG:            $image imagecreatefromjpeg($file);        break;        case IMAGETYPE_PNG:         $image imagecreatefrompng($file);        break;        
    default:            return 
    false;    }            
    $image_resized imagecreatetruecolor$final_width$final_height );                
    if ( (
    $info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {        
    $trnprt_indx imagecolortransparent($image);
            if (
    $trnprt_indx >= 0) {            
    $trnprt_color    imagecolorsforindex($image$trnprt_indx);            
    $trnprt_indx    imagecolorallocate($image_resized$trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);            
    imagefill($image_resized00$trnprt_indx);            
    imagecolortransparent($image_resized$trnprt_indx);        }         
    elseif (
    $info[2] == IMAGETYPE_PNG) {            
    imagealphablending($image_resizedfalse);            
    $color imagecolorallocatealpha($image_resized000127);            
    imagefill($image_resized00$color);            
    imagesavealpha($image_resizedtrue);        
    }    
    }    
    imagecopyresampled($image_resized$image0000$final_width$final_height$width_old$height_old);                
    switch ( 
    strtolower($output) ) {        
    case 
    'browser':            $mime image_type_to_mime_type($info[2]);            
    header("Content-type: $mime");            
    $output NULL;        break;        
    case 
    'file':            
    $output $file;        
    break;        
    case 
    'return':            
    return 
    $image_resized;        break;        
    default:        break;    }    
    switch ( 
    $info[2] ) {        
    case 
    IMAGETYPE_GIF:            imagegif($image_resized$output);        break;        
    case 
    IMAGETYPE_JPEG:            imagejpeg($image_resized$output);        break;        
    case 
    IMAGETYPE_PNG:            imagepng($image_resized$output);        break;        
    default:            return 
    false;    }    
    return 
    true;}

    $test $_FILES["image_upload_box"]["tmp_name"];
    resizeImage($test"320""50"false"image_files/prova.gif");
    echo 
    '<img src="image_files/prova.gif">'
    La parte che mi interessa è questa:
    resizeImage($test, "320", "50", false, "image_files/prova.gif");
    come faccio a chiamare la nuova immagine con qualcosa tipo: "images/".rand(0,100).$_FILES["image_upload_box"]["name"];

    Spero possiate aiutarmi!

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