ciao a tutti
chiedo aiuto per questo scriptino che creato, mettendo insieme vari altri script.
Premetto che così come è funziona benissimo...ora le mie domande sono queste:
1. come faccio (o meglio come scrivo) di salvarmi le immagini "resizzate" nella cartella img
2. utilizzando la stessa function (function.php) come potrei fare a salvare per esempio non 1 ma 2 immagini.Ovviamente aggiungendo un campo nel db (es. image2)
So che per gli esperti saranno domande banali, scusatemi.
form immissione:
codice:
<?
include ("top_foot.inc.php");
top();
?>
<html>
<head>
<title>Documento senza titolo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form enctype="multipart/form-data" method="post" action="res.php">
<font face="Tahoma">Resize Image</font></p>
<font face="Tahoma">Titolo:</font>
<input type="text" name="titolo">
</p>
<input type="file" name="image" size="50">
<input type="submit" value="Invia">
<input type="submit" name="reset" value="Reset">
</p>
</form>
<?
foot();
?>
</body>
</html>
pagina resize:
codice:
<?
include ("top_foot.inc.php");
top();
// Filename to store image as (no extention)
$FILENAME=$_FILES['image']['name'];
// Width to reszie image to (in pixels)
$RESIZEWIDTH=400;
// Width to reszie image to (in pixels)
$RESIZEHEIGHT=400;
// DO NOT EDIT BELOW HERE -----------------------------------------
include("function.php");
$uploaddir = 'D:\Internet\Script_php\res\img';
if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME.jpg")){
unlink("$FILENAME.jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
ImageDestroy ($im);
}
}
include ("config.inc.php");
$db = mysql_connect($db_host, $db_user, $db_password);
if ($db == FALSE)
die ("<div align=\"center\"><FONT COLOR=\"#FF0000\">Errore nella connessione.</FONT></div>");
mysql_select_db($db_name, $db)
or die ("<div align=\"center\"><FONT COLOR=\"#FF0000\">Errore nella connessione.</FONT></div>");
$now = mktime(date("H"), date("i, s, m, d, Y")); $data = date('d.m.Y',$now); $ora = date('H.i.s',$now);
$sql = "INSERT INTO res (titolo, image, data, ora) VALUES ('$titolo', '$FILENAME', '$data', '$ora')";
$aggiornamento = mysql_query($sql,$db)
or die("Impossibile aggiungere il record");
print "<font face=\"Arial\"><font size=\"4\"><center><font color=\"#FF0000\">Upload avvenuto correttamente!</center></font>";
?>
E questa la function per il resize:
codice:
<?
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name);
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name);
}
}
?>