jijiz | Bonjour,
Sur mon site, j'ai deux scripts, un qui permet de redimmensionner des images à la volée, et un autre d'upload:
Redimmensionner :
<?
$filename = "../../".$HTTP_GET_VARS[src_pic]; // we store here the dimensions maximum a picture can take
$size_max = 150;
// $percent = 0.17;
// Content type
$ext = substr($HTTP_GET_VARS[src_pic],-3);
$ext = strtolower($ext); if ($ext == "jpg" )
{
header('Content-type: image/jpeg');
}
if ($ext == "gif" )
{
header('Content-type: image/gif');
}
if ($ext == "png" )
{
header('Content-type: image/png');
}
// Calcul des nouvelles dimensions
list($width, $height) = getimagesize($filename);
// we test if the image is larger than $size_max
if (($width > $size_max) || ($height > $size_max))
{
// The longer between $width and $height will be our limit
if ($width > $height)
{
$percent = $size_max / $width;
}else
{
$percent = $size_max / $height;
}
}
else
{
$percent = 1;
}
$newwidth = $width * $percent;
$newheight = $height * $percent;
// chargement
$thumb =imagecreatetruecolor($newwidth, $newheight);
if ($ext == "jpg" )
{
$source = imagecreatefromjpeg($filename);
}
if ($ext == "gif" )
{
$source = imagecreatefromgif($filename);
}
if ($ext == "png" )
{
$source = imagecreatefrompng($filename);
}
// Redimensionnement
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Affichage
if ($ext == "jpg" )
{
imagejpeg($thumb);
}
if ($ext == "gif" )
{
imagegif($thumb);
}
if ($ext == "png" )
{
imagepng($thumb);
}
?> |
Upload :
<html><head>
<SCRIPT LANGUAGE='javascript'>
function control()
{
var test_name = document.add_pic.nm_picture.value
var test_src = document.add_pic.myFic.value
if (test_name== "" )
{
alert ("Type a name for the picture." );
return false;
}
if (test_src == "" )
{
alert ("Choose an image file to upload." );
return false;
}
return true;
} </SCRIPT>
</head>
<? if ( count ($HTTP_POST_VARS) == 0 )
{
?>
<? // We get all the picture category
// We display all the image category:
$request = "select * from gallery_cat";
$answer = mysql_query($request)
or die (mysql_error($anwser));
?>
<center><h1>Add a picture in the Gallery</h1></center>
<table border = 0 align = center>
<tr><td>
<FORM NAME = "add_pic" ACTION = "content/undermenu/gallery/add.php" METHOD = "POST" ENCTYPE = "multipart/form-data" OnSubmit = "return control()">
Name : </td><td><INPUT TYPE = text NAME = "nm_picture" id = "nm_picture" size = 20 maxlength= 20></td></tr>
<tr><td>Source : </td><td><input type = "file" name = "myFic" id = "myFic" size = 23 </td></tr>
<? echo "<tr><td>Category :</td><td><select name = cat>";
while ($row = mysql_fetch_array($answer))
{
echo "<option value = $row[id_cat]>$row[name_cat]";
} echo "</select>";
?>
</table><br>
<center><INPUT TYPE = submit VALUE = Ok><INPUT TYPE = reset VALUE = Clear > </center>
<?
}else
{?>
<html>
<head>
<META HTTP-EQUIV = "refresh" "content = "3;URL = ../../../index.php?page=gallery">
</head>
<?
include "../../../../conn.php";
// errors
$err = "";
// folder of destination
$folderDest = "../../../../content/gallery/";
$nameDest = $HTTP_POST_FILES["myFic"]["name"];
$ext = substr($HTTP_POST_FILES["myFic"]["name"],-3);
if (!(file_exists($folderDest.$nameDest)))
{
$ext = strtolower($ext);
// We check the extention of the file
if (($ext == "jpg" ) || ($ext == "gif" ) || ($ext == "png" ))
{
if (is_uploaded_file($HTTP_POST_FILES["myFic"]["tmp_name"]))
{
if (rename($HTTP_POST_FILES["myFic"]["tmp_name"],$folderDest.$nameDest))
{
$err = "";
} else
{
$err = "folder";
}
} else
{
$err = "size";
}
}else
{
echo "<center>Bad type of file</center>";
$err = "ext";
}
}else
{
$err = "";
}
// The file is now uploaded, we can add informations in the database
if ($err == "" )
{
$racine = "content/gallery/";
$picture= $racine.$HTTP_POST_FILES["myFic"]["name"];
$requete = "INSERT INTO gallery VALUES (LAST_INSERT_ID() ,'".$HTTP_POST_VARS['nm_picture']."','$picture',\"$_POST[cat]\" );";
$reponse = mysql_query($requete)
OR DIE (mysql_error());
echo "<center>Picture added :<br><br><img src = ../../../../$picture></center>";
echo "<center><a href=\"javascript:history.back()\">Back to Administration</a>" ;
}else
{
echo "<center>Error : $err";
}
} ?>
|
Je ne vois pas comment faire pour redimmensionner l'image puis de l'uploader
imagecopyresampled($thumb, $source, 0,... quel est le type de "$thumb? puis le donner a manger à mon script d'upload? Comment récupérer le nom du fichier contenu par $thumb.
Merci |