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:
 

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
magari se postassi la valorizzazione di $w, $h, $nw, $nh e $wm aiuterebbe a riprodurre il caso
altrimenti si devono dare i numeri ...
ciao
 

saverio_web

Utente Attivo
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];
 

saverio_web

Utente Attivo
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
Autore Titolo Forum Risposte Data
M Problema con connessione MySqli e funzione PHP 2
A [PHP] Problema invio mail con funzione mail() PHP 3
F [Javascript] Problema funzione jquery con elementi css esterni Javascript 1
R Problema funzione caricate con il body Javascript 1
G Problema con la funzione array_combine PHP 2
L problema con la funzione header!!! AIUTO! PHP 24
M problema con elementi della funzione Javascript 3
L problema nella implementare una funzione con ritorno PHP 2
F Problema con funzione cambia password PHP 5
S Problema return con funzione PHP 2
L problema email con funzione mail PHP 1
B Problema con funzione JS Javascript 4
S problema con funzione php PHP 3
D Problema apostrofo con funzione unlink PHP 7
C Problema nel Caricare file e spostarlo con la funzione move_uploaded_file PHP 14
D Problema con funzione preg_match() per controllo email PHP 2
Neptune7650 Piccolo problema con la funzione include PHP 1
asevenx problema con funzione per far apparire e scomparire un form di commenti PHP 1
asevenx problema con la funzione AVG() PHP 1
P Problema con la funzione mail PHP 13
G ajax php myqsl - problema con funzione javascript Ajax 1
F Problema con funzione di RainTpl PHP 3
dk-wamp problema con la funzione sleep PHP 4
jan267 Problema con funzione getElementsByClassName Javascript 2
O Principiante: problema con funzione CURL ###URGENTE### PHP 1
O problema con dvr dahua xvr5116 IP Cam e Videosorveglianza 0
G Problema con Xampp Web Server 1
andrea barletta Problema con miniature comandi Photoshop 0
I problema con alice Posta Elettronica 0
N Problema con position absolute e overflow HTML e CSS 4
L Problema con inner join PHP 11
K [php] Problema con inner join PHP 4
K [PHP] Problema con variabili concatenate. PHP 1
O problema con query PHP 4
I problema con 2 account Posta Elettronica 1
L problema collegamento file css con html HTML e CSS 1
E Problema accesso a file con app sviluppata con MIT APP INVENTOR 2 Sviluppo app per Android 0
M Problema con Try Catch PHP 0
Sergio Unia Problema con gli eventi del mouse su una data table: Javascript 2
T PROBLEMA CON SESSIONI PHP 3
T ALTRO PROBLEMA CON ARRAY PHP PHP 1
R problema con else PHP 0
T PROBLEMA CON ARRAY PHP 8
L problema con query select PHP 2
R Problema query con ricerca id numerico PHP 2
F Problema con risposta PHP 0
S problema con recupero dati tabella mysql PHP 2
Z Problema con il mio tp-l i nk Reti LAN e Wireless 1
L Problema RAM con Tomcat 8 Apache 0
napuleone problema con sort e asort PHP 4

Discussioni simili