Codice PHP:
<? set_time_limit(0); $link = mysql_connect(localhost, PROVA, PROVA) or die("Could not connect to host."); mysql_select_db(PROVA) or die("Could not find database."); define ("MAX_SIZE","500"); define ("WIDTH","100"); define ("HEIGHT","100"); define ("WIDTH2","300"); define ("HEIGHT2","300"); function make_thumb($img_name,$filename,$new_w,$new_h){ $ext=getExtension($img_name); if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) $src_img=imagecreatefromjpeg($img_name); if(!strcmp("png",$ext)) $src_img=imagecreatefrompng($img_name); if(!strcmp("gif",$ext)) $src_img=imagecreatefromgif($img_name); $old_x=imageSX($src_img); $old_y=imageSY($src_img); $ratio1=$old_x/$new_w; $ratio2=$old_y/$new_h; if($ratio1>$ratio2) { $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; } else { $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if(!strcmp("png",$ext)) imagepng($dst_img,$filename); else imagejpeg($dst_img,$filename); if (!strcmp("gif",$ext)) imagegif($dst_img,$filename); imagedestroy($dst_img); imagedestroy($src_img); } function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if(isset($_POST['Submit'])){ $image=$_FILES['cons_image']['name']; if ($image) { $filename = stripslashes($_FILES['cons_image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension! Please use .gif, .jpg or .png files only.</h1>'; $errors=1; }else{ $size=getimagesize($_FILES['cons_image']['tmp_name']); $sizekb=filesize($_FILES['cons_image']['tmp_name']); if ($sizekb > MAX_SIZE*1024) { echo '<h1>You have exceeded the 1MB size limit!</h1>'; $errors=1; } $rand= rand(0, 1000); $image_name=$rand.'.'.$extension; $consname="image/".$image_name; $consname2="image/thumb/".$image_name; $copied = copy($_FILES['cons_image']['tmp_name'], $consname); $copied = copy($_FILES['cons_image']['tmp_name'], $consname2); $sql="INSERT INTO PROVA (image, image2) VALUES ('$consname', '$consname2')" or die(mysql_error()); $query = mysql_query($sql)or die(mysql_error()); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; }else{ $thumb_name=$consname2 ; $thumb=make_thumb($consname,$thumb_name,WIDTH,HEIGHT); $thumb=make_thumb($consname,$consname,WIDTH2,HEIGHT2); } } } }