Rinominare immagini in upload
-
Ho creato questo script che permette di fare il ridimensionamento e l'upload di foto, ora vorrei accodare al nome della foto caricata una stringa del tipo "NOMEFOTO**-small**.jpg" però non riesco a capire come fare, mi potete aiutare.
Questo è il codice:
<?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" "..."> <html xmlns="..."> <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>GRAZIE
-
Ciao DARKF3D3,
devi modificare il nome nel primo parametro della funzione imagecopyresampled.
-
Non credo di aver capito bene come fare, prima della funzione imagejpeg ho provato a far assumere alla variabile $new_image una scringa di 4 caratteri però non funziona.
Quello che vorrei riuscire a fare è salvare la stessa immagine 2 vole, una con il nome originale e una con il nome modificato, però ci riesco.
Qualcun'altro ha delle idee su come farlo?
Non so proprio come fare.
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