Upload e ridimensionamento immagini
-
Qualcuno mi può aiutare ha modificare questo codice per l'upload delle immagini.
Attualmente funziona in questo modo:
Viene ritagliato un quadrato con lato uguale alla lunghezza del lato più corto della foto caricata:
Dopodiché il quadrato viene ridotto a 120x120 pixel e poi salvato nella cartella "foto".E qui mi sono bloccato...
Adesso vorrei far si che oltre a salvare la miniatura da 120x120, mi salvasse anche un altra miniatura da 200x200 e poi una copia dell'immagine originale.
ES:
L'utente carica una foto con nome: foto.jpgnella cartella foto ritrovo:
foto.jpg (dimensioni originali)
foto_med.jpg (240x240)
foto_sma.jpg (120x120)Potete aiutarmi... Grazie
Questo è il codice:
"... se volete provarlo il codice è completo, quindi salvandolo in un file .php e avviandolo da php fuziona"<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> <?php // upload the file if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) { // file needs to be jpg,gif,bmp,x-png and 4 MB max if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000)) { // 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/x-png"){ $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]); } $remote_file = "foto/".$_FILES["image_upload_box"]["name"]; imagejpeg($image_source,$remote_file,80); chmod($remote_file,0644); $new_w = 120; $new_h = 120; $orig_w = imagesx($image_source); $orig_h = imagesy($image_source); $w_ratio = ($new_w / $orig_w); $h_ratio = ($new_h / $orig_h); // PAESAGGIO if ($orig_w > $orig_h ) { $crop_w = round($orig_w * $h_ratio); $crop_h = $new_h; $x = ($orig_w - $orig_h) / 2; // RITRATTO } elseif ($orig_w < $orig_h ) { $crop_h = round($orig_h * $w_ratio); $crop_w = $new_w; $y = ($orig_h - $orig_w) /2; // QUADRATO } else { $crop_w = $new_w; $crop_h = $new_h; } $new_image = imagecreatetruecolor($new_w,$new_h); $image_source = imagecreatefromjpeg($remote_file); imagecopyresampled($new_image, $image_source, 0, 0, $x, $y, $crop_w, $crop_h, $orig_w, $orig_h); imagejpeg($new_image,$remote_file,80); imagedestroy($new_image); imagedestroy($image_source); header("Location: submit.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]); exit; } else{ header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error"); exit; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "***************************************"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Image Upload with resize</title> <style type="text/css"> <!-- body,td,th { font-family: Arial, Helvetica, sans-serif; color: #333333; font-size: 12px; } .upload_message_success { padding:4px; background-color:#009900; border:1px solid #006600; color:#FFFFFF; margin-top:10px; margin-bottom:10px; } .upload_message_error { padding:4px; background-color:#CE0000; border:1px solid #990000; color:#FFFFFF; margin-top:10px; margin-bottom:10px; } --> </style></head> <body> <h1 style="margin-bottom: 0px">INSERIMENTO IMMAGINE</h1> <?php if(isset($_REQUEST['upload_message'])){?> <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>"> <?php echo htmlentities($_REQUEST['upload_message']);?> </div> <?php }?> <form action="submit.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;"> <label>- Dimensione massima: 4MB - Estensioni possibili: jpg, gif, png.</label><br /> <input name="image_upload_box" type="file" id="image_upload_box" size="40" /> <input type="submit" name="submit" value="Upload image" /> <br /> <br /> <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" /> </form> <?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?> <p> <img src="foto/<?php echo $_REQUEST['show_image'];?>" /> </p> <?php }?> </body> </html> -
-
-
Ciao
ti allego solo parte php, è una soluzione diversa a quello che hai tu nel codice.
Questo ti salva la foto in originale, ti crea 2 resize, uno da 120 e uno da 240 come hai specificato.P.S. non ho testato il codice che ti allego.
<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> <?php // upload the file if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) { // file needs to be jpg,gif,bmp,x-png and 4 MB max if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000)) { $remote_file = "foto/".$_FILES["image_upload_box"]["name"]; chmod($remote_file,0644); // carico la foto originale move_uploaded_file($_FILES['image_upload_box']['tmp_name'], $remote_file); // procedo a ridimensionare la foto per 240 e per 120 include("include/class.image-resize.php"); $obj = new img_opt(); $obj->max_width(240); $obj->max_height(240); $obj->image_path($remote_file); $obj->image_resize(); $obj2 = new img_opt(); $obj2->max_width(120); $obj2->max_width(120); $obj2->image_path($remote_file); $obj2->image_resize(); header("Location: submit.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]); exit; } else{ header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error"); exit; } } ?>questa è la classe utilizzata per il resize (class.image-resize.php).
<?PHP class img_opt { var $max_width; var $max_height; var $path; var $img; var $new_width; var $new_height; var $mime; var $image; var $width; var $height; function max_width($width) { $this->max_width = $width; } function max_height($height) { $this->max_height = $height; } function image_path($path) { $this->path = $path; } function get_mime() { $img_data = getimagesize($this->path); $this->mime = $img_data['mime']; } function create_image() { switch($this->mime) { case 'image/jpeg': $this->image = imagecreatefromjpeg($this->path); break; case 'image/gif': $this->image = imagecreatefromgif($this->path); break; case 'image/png': $this->image = imagecreatefrompng($this->path); break; } } function image_resize() { set_time_limit(120); $this->get_mime(); $this->create_image(); $this->width = imagesx($this->image); $this->height = imagesy($this->image); $this->set_dimension(); $image_resized = imagecreatetruecolor($this->new_width,$this->new_height); imagecopyresampled($image_resized, $this->image, 0, 0, 0, 0, $this->new_width, $this->new_height,$this->width, $this->height); imagejpeg($image_resized,$this->path); } //######### FUNCTION FOR RESETTING DEMENSIONS OF IMAGE ########### function set_dimension() { if($this->width==$this->height) { $case = 'first'; } elseif($this->width > $this->height) { $case = 'second'; } else { $case = 'third'; } if($this->width>$this->max_width && $this->height>$this->max_height) { $cond = 'first'; } elseif($this->width>$this->max_width && $this->height<=$this->max_height) { $cond = 'first'; } else { $cond = 'third'; } switch($case) { case 'first': $this->new_width = $this->max_width; $this->new_height = $this->max_height; break; case 'second': $ratio = $this->width/$this->height; $amount = $this->width - $this->max_width; $this->new_width = $this->width - $amount; $this->new_height = $this->height - ($amount/$ratio); break; case 'third': $ratio = $this->height/$this->width; $amount = $this->height - $this->max_height; $this->new_height = $this->height - $amount; $this->new_width = $this->width - ($amount/$ratio); break; } } } ?>ciao
-
Grazie per l'aiuto, l'ho provato però mi restituisce questi errori e non capisco il motivo:
```
[/B]
[B]Warning[/B]: chmod() : No such file or directory in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]11[/B][B]Warning[/B]: include(include/class.image-resize.php) [...]: failed to open stream: No such file or directory in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]19[/B]
[B]Warning[/B]: include() [...]: Failed opening 'include/class.image-resize.php' for inclusion (include_path='.;C:\php5\pear') in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]19[/B]
[B]Fatal error[/B]: Class 'img_opt' not found in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]20[/B]
[B]
``` -
Ciao
si hai ragione, macava qualche cosa.
provalo così, io non l'ho provato, attendo alle riche dove vengono definiti i nomi dei 3 file, devi creare le cartelle dove andranno le foto interessate.
<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> <?php // upload the file if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) { // file needs to be jpg,gif,bmp,x-png and 4 MB max if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000)) { $remote_file_full = "foto/full/".$_FILES["image_upload_box"]["name"]; $remote_file_240 = "foto/resize_240/".$_FILES["image_upload_box"]["name"]; $remote_file_120 = "foto/resize_120/".$_FILES["image_upload_box"]["name"]; chmod($remote_file_full,0644); chmod($remote_file_240,0644); chmod($remote_file_120,0644); // carico la foto originale move_uploaded_file($_FILES['image_upload_box']['tmp_name'], $remote_file_full); copy($remote_file_full,$remote_file_240); copy($remote_file_full,$remote_file_120); // procedo a ridimensionare la foto per 240 e per 120 include("include/class.image-resize.php"); $obj = new img_opt(); $obj->max_width(240); $obj->max_height(240); $obj->image_path($remote_file_240); $obj->image_resize(); $obj2 = new img_opt(); $obj2->max_width(120); $obj2->max_width(120); $obj2->image_path($remote_file_120); $obj2->image_resize(); header("Location: submit.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]); exit; } else{ header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error"); exit; } } ?>fammi sapere
ciao
-
Ho creato le cartelle in questo modo, nella cartella "www" di easyphp ho creato le cartelle:
foto/full
foto/resize_240
foto/resize_120però mi da comunque errore:
[B]Warning[/B]: chmod() [---]: No such file or directory in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]15[/B] [B]Warning[/B]: chmod() [---]: No such file or directory in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]16[/B] [B]Warning[/B]: chmod() [---]: No such file or directory in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]17[/B] [B]Warning[/B]: include(include/class.image-resize.php) [---]: failed to open stream: No such file or directory in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]27[/B] [B]Warning[/B]: include() [---]: Failed opening 'include/class.image-resize.php' for inclusion (include_path='.;C:\php5\pear') in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]27[/B] [B]Fatal error[/B]: Class 'img_opt' not found in [B]C:\Program Files (x86)\EasyPHP-5.3.1\www\submit.php[/B] on line [B]28[/B]Andando a controllare le cartelle, al loro interno c'è l'immagine che ho caricato, però non ridimensionata.
-
-
Rieccomi, mi scuso se rispondo solo ora ma avevo accantonato l'upload delle immagini per dare riorità ad altre parti del progetto...Ho ripreso in mano tutte le prove e i problemi persistono, anche con le ultime modifiche non riesco a risolvere il problema; fino al resize della singola immagine tutto ok, mentre i 2 file che mi hai passato non riesco a farli funzionare.
Ti chiedo gentilmente se quando hai tempo puoi fare un test e vedere se capisci dov'è il problema, io non so più dove sbattere la testa.Grazie
-
Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi
