Problema con funzione "imagecreatefrompng"

saverio_web

Utente Attivo
Buona sera ragazzi, sto lavorando ad un gestionale in PHP e ho riscontrato un problema in una funzione che permette la creazione di miniature di foto che vengono caricate.

Mi spiego meglio: attraverso un form procedo alla creazione di un album, i dati vengono controllati e caricati sul server e le foto vengono caricate; fino a qui tutto bene. Siccome però le immagini risultano troppo pesanti da caricare, ho inserito una funzione che mi permette di creare delle miniature delle foto caricate; ed ora arriva il problema: se effettuo questa operazione su immagini JPG il problema non sussiste, le miniature vengono correttamente create, se utilizzo la stessa funzione su immagini in formato PNG si formano "immagini" di 0 byte e non visualizzabili. Accade raramente che si creino immagini tutte nere.

I codici sono i seguenti:
Questo fa riferimento alla funzione per le immagini PNG
PHP:
            $extimg=substr($_FILES["photo$p"]["type"], 6, 3);
            if($extimg=='png') {
                $img2 = imagecreatefrompng($img);
                $thumb=ImageCreateTrueColor($nw,$nh);
	
                $wm = $w/$nw;
                $hm = $h/$nh;
	
                $h_height = $nh/2;
                $w_height = $nw/2;
	
                if($w > $h){
	
	                $adjusted_width = $w / $hm;
	                $half_width = $adjusted_width / 2;
	                $int_width = $half_width - $w_height;
	
	                ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
                    imagePNG($thumb,$thname,95); 
	
                }elseif(($w < $h) || ($w == $h)){
	
	                $adjusted_height = $h / $wm;
	                $half_height = $adjusted_height / 2;
	                $int_height = $half_height - $h_height;
	
	                ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
	                ImagePNG($thumb,$thname,95); 
	
                }else{
	                ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h); 	
	                ImagePNG($thumb,$thname,95); 
                }
                
            }

Quest, che funziona, a quelle in JPG
PHP:
            else {
                $img2 = ImageCreateFromJpeg($img);
                $thumb=ImageCreateTrueColor($nw,$nh);
	
                $wm = $w/$nw;
                $hm = $h/$nh;
	
                $h_height = $nh/2;
                $w_height = $nw/2;
	
                if($w > $h){
	
	                $adjusted_width = $w / $hm;
	                $half_width = $adjusted_width / 2;
	                $int_width = $half_width - $w_height;
	
	                ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
	                ImageJPEG($thumb,$thname,95); 
	
                }elseif(($w < $h) || ($w == $h)){
	
	                $adjusted_height = $h / $wm;
	                $half_height = $adjusted_height / 2;
	                $int_height = $half_height - $h_height;
	
	                ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
	                ImageJPEG($thumb,$thname,95); 
	
                }else{
	                ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h); 	
	                ImageJPEG($thumb,$thname,95); 
                }
            }

Ovviamente le immagini temporanee vengono poi distrutte tramite la funzione "imagedestroy".

Vi ringrazi in anticipo :fonzie:
 
magari se postassi la valorizzazione di $w, $h, $nw, $nh e $wm aiuterebbe a riprodurre il caso
altrimenti si devono dare i numeri ...
ciao
 
magari se postassi la valorizzazione di $w, $h, $nw, $nh e $wm aiuterebbe a riprodurre il caso
altrimenti si devono dare i numeri ...
ciao

Questa è la parte subito prima i codici prima postati. La variabile $dir rappresenta la directory di destinazione dell'immagine $ipath la directory dove è stata caricata la immagine (uguale a $dir) e $tpath rappresenta la directory dove verrà caricata la miniatura.
PHP:
            $nw=350;
            $nh=262;

            $ipath = $dir;
            $tpath = $dir."thumb";

            if (is_dir("$tpath")) {}
            else {
                mkdir("$tpath");
            }


            $img="$ipath" . $_FILES["photo$p"]["name"];

            $dimensions = GetImageSize($img);

            $thname = "$tpath/" . $_FILES["photo$p"]["name"];

            $w=$dimensions[0];
            $h=$dimensions[1];
 
Questo è la parte di codice completo funzionante, che verifica l'estensione dell'immagine caricata e ne crea una miniatura. Spero possa essere utilile. :D

PHP:
            $nw=350;
            $nh=262;

            $ipath = $dir;
            $tpath = $dir."thumb";

            if (is_dir("$tpath")) {}
            else {
                mkdir("$tpath");
            }


            $img="$ipath" . $_FILES["photo$p"]["name"];

            $dimensions = GetImageSize($img);

            $thname = "$tpath/" . $_FILES["photo$p"]["name"];
            //echo "<br>$img<br> $ipath<br> $tpath<br> $thname";die ();

            $w=$dimensions[0];
            $h=$dimensions[1];

            
            $extimg=substr($_FILES["photo$p"]["type"], 6, 3);
            if($extimg=='png') {
                $img2 = ImageCreateFromPNG($img);
                $thumb=ImageCreateTrueColor($nw,$nh);
	
                $wm = $w/$nw;
                $hm = $h/$nh;
	
                $h_height = $nh/2;
                $w_height = $nw/2;
	
                if($w > $h){
	
	                $adjusted_width = $w / $hm;
	                $half_width = $adjusted_width / 2;
	                $int_width = $half_width - $w_height;
	
	                ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
	                ImagePNG($thumb,$thname,9); 
	
                }elseif(($w < $h) || ($w == $h)){
	
	                $adjusted_height = $h / $wm;
	                $half_height = $adjusted_height / 2;
	                $int_height = $half_height - $h_height;
	
	                ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
	                ImagePNG($thumb,$thname,95); 
	
                }else{
	                ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h); 	
	                ImagePNG($thumb,$thname,95); 
                }
            }
            else {
                $img2 = ImageCreateFromJpeg($img);
                $thumb=ImageCreateTrueColor($nw,$nh);
	
                $wm = $w/$nw;
                $hm = $h/$nh;
	
                $h_height = $nh/2;
                $w_height = $nw/2;
	
                if($w > $h){
	
	                $adjusted_width = $w / $hm;
	                $half_width = $adjusted_width / 2;
	                $int_width = $half_width - $w_height;
	
	                ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
	                ImageJPEG($thumb,$thname,95); 
	
                }elseif(($w < $h) || ($w == $h)){
	
	                $adjusted_height = $h / $wm;
	                $half_height = $adjusted_height / 2;
	                $int_height = $half_height - $h_height;
	
	                ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
	                ImageJPEG($thumb,$thname,95); 
	
                }else{
	                ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h); 	
	                ImageJPEG($thumb,$thname,95); 
                }
            }

            imagedestroy($img2);
 

Discussioni simili