- Home
- Categorie
- Coding e Sistemistica
- Coding
- [SCRIPT] Resize image mantenendo proporzioni
- 
							
							
							
							
							
[SCRIPT] Resize image mantenendo proporzioniCiao a tutti, mi dite cosa sbaglio in questo script ? [php] 
 list($ihwidth,$ihheight) = explode('x',$grandezza);
 //IMPOSTO UN MASSIMALE PER LE IMMAGINI
 $altezzamassima = "270";
 $larghezzamassima = "240";if($ihwidth > $larghezzamassima && $ihheight <= $altezzamassima) { 
 $ihwidthprovvisorio = $ihwidth - $larghezzamassima;
 $ihwidth1 = $ihwidth - $ihwidthprovvisorio;$prendimolapercentualetolta = ($ihwidthprovvisorio/$ihwidth)*100; 
 $ihheightprovv =($ihheight * $prendimolapercentualetolta)/100;
 $ihheight1 = round($ihheight - $ihheightprovv);
 }if($ihheight > $altezzamassima && $ihwidth <= $larghezzamassima) { 
 $ihheightprovvisorio = $ihheight - $altezzamassima;
 $ihheight1 = $ihheight - $ihheightprovvisorio;$prendimolapercentualetolta2 = ($ihheightprovvisorio/$ihwidth)*100; 
 $ihwidthprovv =($ihwidth * $prendimolapercentualetolta2)/100;
 $ihwidth1 = round($ihwidth - $ihwidthprovv);} if($ihheight > $altezzamassima && $ihwidth > $larghezzamassima) { 
 $ihwidthprovvisorio = $ihwidth - $larghezzamassima;
 $ihheightprovvisorio = $ihheight - $altezzamassima;if($ihwidthprovvisorio >= $ihheightprovvisorio) { 
 $ihwidthprovvisorio = $ihwidth - $larghezzamassima;
 $ihwidth1 = $ihwidth - $ihwidthprovvisorio;
 $prendimolapercentualetolta = ($ihwidthprovvisorio/$ihwidth)*100;
 $ihheightprovv =($ihheight * $prendimolapercentualetolta)/100;
 $ihheight1 = round($ihheight - $ihheightprovv);} else if($ihwidthprovvisorio < $ihheightprovvisorio){ 
 $ihheightprovvisorio = $ihheight - $altezzamassima;
 $ihheight1 = $ihheight - $ihheightprovvisorio;
 $prendimolapercentualetolta2 = ($ihheightprovvisorio/$ihwidth)*100;
 $ihwidthprovv =($ihwidth * $prendimolapercentualetolta2)/100;
 $ihwidth1 = round($ihwidth - $ihwidthprovv);
 }} 
 else if($ihwidth <= $larghezzamassima && $ihheight <= $altezzamassima) {
 $ihwidth1 = $ihwidth;
 $ihheight1 = $ihheight;
 }[/php]Io vorrei solo ricreare l'immagine senza perdere il ratio.. Avete idee? 
 
- 
							
							
							
							
							
Se a qualcuno dovesse servire, ho ottimizzato lo script: [php] 
 list($ihwidth,$ihheight) = explode('x',$grandezza);
 //IMPOSTO UN MASSIMALE PER LE IMMAGINI
 $am = "270";
 $lm = "240";
 if($ihwidth > $lm or $ihheight > $am) {
 $eliminarel = $ihwidth - $lm;
 $eliminarea = $ihheight - $am;
 if (($elimnarel - $eliminarea) > ($eliminarea - $eliminarel)) { $perc = ($eliminarel100)/$ihwidth;
 $ihw = round(($ihwidth/100)$perc);
 $ihwidth1 = $ihwidth-$ihw;
 $ihh = round(($ihheight/100)$perc);
 $ihheight1 = $ihheight - $ihh;
 }
 else if (($elimnarel - $eliminarea) < ($eliminarea - $eliminarel)) { $perc = ($eliminarea100)/$ihheight;
 $ihw = round(($ihwidth/100)$perc);
 $ihwidth1 = $ihwidth-$ihw;
 $ihh = round(($ihheight/100)$perc);
 $ihheight1 = $ihheight - $ihh;
 }
 }
 else {
 $ihwidth1 = $ihwidth;
 $ihheight1 = $ihheight;
 }[/php]Funziona alla perfezione dato che calcola le %. Saluti. 
 
- 
							
							
							
							
							
C'erano 2 errori, questo è perfetto: [php] 
 //IMPOSTO UN MASSIMALE PER LE IMMAGINI
 $am = "150";
 $lm = "150";if($ihwidth > $lm or $ihheight > $am) { 
 $eliminarel = $ihwidth - $lm;
 $eliminarea = $ihheight - $am;
 if ($eliminarel >= $eliminarea) { $perc = round(($eliminarel100)/$ihwidth);
 $ihw = round(($ihwidth/100)$perc);
 $ihwidth1 = $ihwidth-$ihw;
 $ihh = round(($ihheight/100)$perc);
 $ihheight1 = $ihheight - $ihh;
 }
 else if ($eliminarel < $eliminarea) { $perc = round(($eliminarea100)/$ihheight);
 $ihw = round(($ihwidth/100)$perc);
 $ihwidth1 = $ihwidth-$ihw;
 $ihh = round(($ihheight/100)$perc);
 $ihheight1 = $ihheight - $ihh;
 }
 }
 else {
 $ihwidth1 = $ihwidth;
 $ihheight1 = $ihheight;
 }
 [/php]
 
- 
							
							
							
							
							Ciao Sovietiko, 
 ottimo script, complimenti.
 Con il tuo permesso mi piacerebbe inserirlo nella sezione script utili. 
 
- 
							
							
							
							
							
[...] Certo! Spero sarà utile a tutti!  
 
- 
							
							
							
							
							Fatto, grazie a nome della community.  
 
- 
							
							
							
							
							
Buongiorno a tutti....sono un neofita e vorrei sapere all'interno di quale file devo aggiungere il codice. Utilizzo lo script: 
 'giorgiotave.it/forum/scripting-e-risorse-utili/95013-tutorial-come-caricare-un-immagine-su-database-mysql.html'Ringrazio e Buon Anno! 
 Marco
 
- 
							
							
							
							
							
@Sovietiko said: C'erano 2 errori, questo è perfetto: [php] 
 //IMPOSTO UN MASSIMALE PER LE IMMAGINI
 $am = "150";
 $lm = "150";if($ihwidth > $lm or $ihheight > $am) { 
 $eliminarel = $ihwidth - $lm;
 $eliminarea = $ihheight - $am;
 if ($eliminarel >= $eliminarea) { $perc = round(($eliminarel100)/$ihwidth);
 $ihw = round(($ihwidth/100)$perc);
 $ihwidth1 = $ihwidth-$ihw;
 $ihh = round(($ihheight/100)$perc);
 $ihheight1 = $ihheight - $ihh;
 }
 else if ($eliminarel < $eliminarea) { $perc = round(($eliminarea100)/$ihheight);
 $ihw = round(($ihwidth/100)$perc);
 $ihwidth1 = $ihwidth-$ihw;
 $ihh = round(($ihheight/100)$perc);
 $ihheight1 = $ihheight - $ihh;
 }
 }
 else {
 $ihwidth1 = $ihwidth;
 $ihheight1 = $ihheight;
 }
 [/php]e in questo script dove lo dovrei collocare ? /forum/scripting-e-risorse-utili/95013-tutorial-come-caricare-un-immagine-su-database-mysql.html 
 :():
 
- 
							
							
							
							
							
Ciao, quello script è davvero di base per il caricamento delle immagini.. 
 Dovresti prima recuperare le dimensioni magari in questo modo:
 [PHP]
 //recupero altezza e larghezza$imagesize = @getimagesize($nome_file_temporaneo); 
 $ihwidth= $imagesize[0];
 $ihheight= $imagesize[1];//IMPOSTO UN MASSIMALE PER LE IMMAGINI 
 $am = "150";
 $lm = "150";if($ihwidth > $lm or $ihheight > $am) { 
 $eliminarel = $ihwidth - $lm;
 $eliminarea = $ihheight - $am;
 if ($eliminarel >= $eliminarea) { $perc = round(($eliminarel100)/$ihwidth);
 $ihw = round(($ihwidth/100)$perc);
 $ihwidth1 = $ihwidth-$ihw;
 $ihh = round(($ihheight/100)$perc);
 $ihheight1 = $ihheight - $ihh;
 }
 else if ($eliminarel < $eliminarea) { $perc = round(($eliminarea100)/$ihheight);
 $ihw = round(($ihwidth/100)$perc);
 $ihwidth1 = $ihwidth-$ihw;
 $ihh = round(($ihheight/100)$perc);
 $ihheight1 = $ihheight - $ihh;
 }
 }
 else {
 $ihwidth1 = $ihwidth;
 $ihheight1 = $ihheight;
 }//se l'immagine sarà da ridimensionare, verrà ridimensionata grazie allo script che calcola le giuste variazioni $thumb2 = imagecreatetruecolor($ihwidth1, $ihheight1); 
 imagecopyresized($thumb2, $source, 0, 0, 0, 0, $ihwidth1, $ihheight1, $ihwidth, $ihheight);
 $anteprima2 = "nomefoto";
 imagejpeg($thumb2, "percorso della foto".$anteprima2, 75);[/PHP] Ovviamente lascio a te mettere i controlli ecc.. Il codice è vecchio, ma grosso modo è così che funziona. Ciao, 
 M.