Salve a tutti e buonasera.
Ho un problema su uno script che mi permette di effettuare l'upload e il ridimensionamento delle immagini.
Lo script in origini portava alla larghezza di 400px qualunque immagine io caricavo.
Invece a me serviva la situazione in cui nel caso l'immagine è meno larga di 400 px lo script deve solo caricare ma non fare il resize.
Questo è lo script e in rosso vi mostro la parte da me aggiunta per adattarlo alla mia situazione:
Se io carico un'imamgine meno larga di 400 pixel lui me la carica ma mi crea un quadrato tutto nero delle dimensioni dell'immagine.codice:<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title> .: Maaking.Com :. Upload and Resize Images</title> <?php ######################################## # Upload and Resize Images # # with Aspect Ratio # # By: Mohammed Ahmed # # Gaza, Palestine # # Email: m@maaking.com # # Skype: maaking # # Date: 16-May-2006 # ######################################## //load the config file include("config.php"); $site ="http://www........."; //site name with the directory that the uploading file is on ex. http://your_site/phpimages/ $directory = "images/thumbs/"; //directory name, where all the images upload $apertura = """; $chiusura = """; //if the for has submittedd if (isset($_POST['upForm'])){ $file_type = $_FILES['imgfile']['type']; $file_name = $_FILES['imgfile']['name']; $file_size = $_FILES['imgfile']['size']; $file_tmp = $_FILES['imgfile']['tmp_name']; //check if you have selected a file. if(!is_uploaded_file($file_tmp)){ echo "Error: Please select a file to upload!. --<a href=\"$_SERVER[PHP_SELF]\">back</a>"; exit(); //exit the script and don't do anything else. } //check file extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { echo "Wrong file extension. --<a href=\"$_SERVER[PHP_SELF]\">back</a>"; exit(); } //get the file extension. $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); //get the new width variable. $ThumbWidth = $img_thumb_width; //keep image type if($file_size){ if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ $new_img = imagecreatefromjpeg($file_tmp); }elseif($file_type == "image/x-png" || $file_type == "image/png"){ $new_img = imagecreatefrompng($file_tmp); }elseif($file_type == "image/gif"){ $new_img = imagecreatefromgif($file_tmp); } //list width and height and keep height ratio. list($width, $height) = getimagesize($file_tmp); $imgratio=$width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio; }else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio; } //function for resize image. if (function_exists(imagecreatetruecolor)){ if ($width <= 400) { echo " Immagine caricata: "; $imgNoResized = imagecreatetruecolor($width,$height); //$imgNoResized = "565521240.jpg"; ImageJpeg ($imgNoResized,"$path_thumbs/$rand_name.$file_ext"); echo '[img]' . $path_thumbs . '/' . $rand_name . '.' . $file_ext . '[/img]'; echo " <font class=testo>Copia e incolla nel tuo post la stringa sottostante:</font> <font size=2 color=gray><textarea rows=3 cols=60>[img] . $apertura . $site . $directory . $rand_name . $file_ext . $chiusura . [/img]</textarea></font>"; die("L'immagine non può essere ridimenssionata perchè più piccola di 400px"); } $resized_img = imagecreatetruecolor($newwidth,$newheight); }else{ die("Error: Please make sure you have GD library ver 2+"); } imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //save image ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext"); ImageDestroy ($resized_img); ImageDestroy ($new_img); //print message echo " Immagine ridimensionata: "; echo '[img]' . $path_thumbs . '/' . $rand_name . '.' . $file_ext . '[/img]'; echo " <font class=testo>Copia e incolla nel tuo post la stringa sottostante:</font> <font size=2 color=gray><textarea rows=3 cols=60>[img] . $apertura . $site . $directory . $rand_name . $file_ext . $chiusura . [/img]</textarea></font>"; } //upload the big image move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext"); echo " Image Big: <a href=\"$path_big/$rand_name.$file_ext\">$path_big/$rand_name.$file_ext</a>"; echo " --<a href=\"$_SERVER[PHP_SELF]\">back</a>"; }else{ //if the form hasn't been submitted. //print the form echo "<script> function view_img(img_name){ document[img_name].src = upForm.imgfile.value; document[img_name].width = 400; } </script>\n\n <h3>Carica immagine:</h3>\n <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[PHP_SELF]\">\n <input type=\"file\" name=\"imgfile\" > [img][/img] \n Image width will resize to $img_thumb_width with height ratio. <input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n </form> "; } //print copyright ;) echo" </body> </html>"; ?>
Se carico un'immagine più grande invece funziona tutto bene.
Potete cortesemente darmi qualche suggerimento?
Grazie e tutti in anticipo.
![]()

Rispondi quotando