par
jacque99 » 27 oct. 2010, 15:07
Bonjour Jojolapine,
Au fait, je cherche aussi à prendre une partie de l'image, par exemple le centre de chaque image.
J'ai pris ta fonction class Hoathis_Images, et j'ai essayé mélanger avec mon code, mais apparemment il existe des erreurs
Je suis à l'écoute de tes conseils
<!-----------------------------------------page index.php---------------------------------------->
<html>
<head></head>
<body>
<!--On affiche le formulaire d'envoi d'une image-->
<center>
<br /><hr />
<form method="post" enctype="multipart/form-data" action="upload.php">
<p>
Le nom de l'image redimensionner avec 95 ,95
<input type="text" name="video"><br><br>
<input type="file" name="fichier" size="30">
<input type="submit" name="upload" value="Uploader">
</p>
</form>
</center>
</body>
</html>
<!----------------------------------------------------------------------------------------------->
<!-----------------------------------------page upload.php---------------------------------------->
<?php
$nomImage=$_POST['video'];
if( isset($_POST['upload']) ) // si formulaire soumis
{
$content_dir = 'upload/'; // dossier où sera déplacé le fichier
$tmp_file = $_FILES['fichier']['tmp_name'];
if( !is_uploaded_file($tmp_file) )
{
exit("Le fichier est introuvable");
}
// on vérifie maintenant l'extension
$type_file = $_FILES['fichier']['type'];
if( !strstr($type_file, 'jpg') && !strstr($type_file, 'jpeg') && !strstr($type_file, 'png') && !strstr($type_file, 'bmp') &&
!strstr($type_file, 'gif') )
{
exit("Le fichier n'est pas une image");
}
// on copie le fichier dans le dossier de destination
$ext = substr($_FILES['fichier']['name'], strrpos($_FILES['fichier']['name'], '.'));
$filename_orig = $nomImage.$ext;
echo $filename_orig;
class Hoathis_Images {
public static function resize(
$filename_orig,
$filename_dest,
$max_width,
$max_height
){
// Calcul des nouvelles dimensions
list($width_orig, $height_orig) = getimagesize($filename_orig);
if($width_orig > $max_width || $height_orig > $max_height){
$fileinfos = pathinfo($filename_orig);
$ext = strtolower($fileinfos['extension']);
switch($ext){
case 'jpg':
case 'jpeg':
$old_img = imagecreatefromjpeg($filename_orig);
break;
case 'png':
$old_img = imagecreatefrompng($filename_orig);
break;
case 'gif':
$old_img = imagecreatefromgif($filename_orig);
break;
}
$ratio_orig = $width_orig/$height_orig;
if ($max_width/$max_height > $ratio_orig) {
$new_width = $max_height*$ratio_orig;
$new_height = $max_height;
} else {
$new_height = $max_width/$ratio_orig;
$new_width = $max_width;
}
// Redimensionnement
$new_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_img, $old_img, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
switch($ext){
case 'jpg':
case 'jpeg':
imagejpeg($new_img,$filename_dest,95);
break;
case 'png':
imagepng($new_img,$filename_dest,95);
break;
case 'gif':
imagegif($new_img,$filename_dest,95);
break;
}
}
else {
copy($filename_orig,$filename_dest);
}
}
public static function crop(
$filename_orig,
$filename_dest,
$x_orig,
$y_orig,
$width,
$height
)
{
$fileinfos = pathinfo($filename_orig);
$ext = strtolower($fileinfos['extension']);
switch($ext){
case 'jpg':
case 'jpeg':
$old_img = imagecreatefromjpeg($filename_orig);
break;
case 'png':
$old_img = imagecreatefrompng($filename_orig);
break;
case 'gif':
$old_img = imagecreatefromgif($filename_orig);
break;
}
// Redimensionnement
$new_img = imagecreatetruecolor($width, $height);
imagecopyresampled($new_img, $old_img, 0, 0, $x_orig, $y_orig, $width, $height, $width, $height);
switch($ext){
case 'jpg':
case 'jpeg':
imagejpeg($new_img,$filename_dest,100);
break;
case 'png':
imagepng($new_img,$filename_dest);
break;
case 'gif':
imagegif($new_img,$filename_dest);
break;
}
}
}
if( preg_match('#[\x00-\x1F\x7F-\x9F/\\\\]#', $name_file) )
{
exit("Nom de fichier non valide");
}
else if( !move_uploaded_file($tmp_file, $content_dir . $name_file) )
{
exit("Impossible de copier le fichier dans $content_dir");
}
Hoathis_Images($content_dir.$filename_dest,171, 107);
}
?>
<!----------------------------------------------------------------------------------------------->
Le principe que je voulais faire est proche de bout de code qui contient des erreurs aussi :
<?php
// Crop dimensions.
$width = 200;
$height = 200;
// Set the path to the image to resize
$input_image = "monImage.png";
// Get the size of the original image into an array
$size = getimagesize( $input_image );
// Prepare canvas
$canvas = imagecreatetruecolor( $width, $height );
// Create a new image in the memory from the file
$cropped = imagecreatefrompng( $input_image );
// Prepare image crop - center the crop on the image
$newwidth = $size[0] / 2;
$newheight = $size[1] / 2;
$cropLeft = ( $newwidth/2 ) - ( $width/2 );
$cropHeight = ( $newheight/2 ) - ( $height/2 );
// Generate the cropped image
imagecopyresized( $canvas, $cropped, 0,0, $cropLeft, $cropHeight, $size[0], $size[1], $newwidth, $newheight );
header('Content-type: image/jpeg');
imagejpeg($canvas);
// Clear the memory of the tempory images
imagedestroy( $canvas );
imagedestroy( $cropped );
?>
Bonjour Jojolapine,
Au fait, je cherche aussi à prendre une partie de l'image, par exemple le centre de chaque image.
J'ai pris ta fonction class Hoathis_Images, et j'ai essayé mélanger avec mon code, mais apparemment il existe des erreurs
Je suis à l'écoute de tes conseils
[PHP]
<!-----------------------------------------page index.php---------------------------------------->
<html>
<head></head>
<body>
<!--On affiche le formulaire d'envoi d'une image-->
<center>
<br /><hr />
<form method="post" enctype="multipart/form-data" action="upload.php">
<p>
Le nom de l'image redimensionner avec 95 ,95
<input type="text" name="video"><br><br>
<input type="file" name="fichier" size="30">
<input type="submit" name="upload" value="Uploader">
</p>
</form>
</center>
</body>
</html>
<!----------------------------------------------------------------------------------------------->
<!-----------------------------------------page upload.php---------------------------------------->
<?php
$nomImage=$_POST['video'];
if( isset($_POST['upload']) ) // si formulaire soumis
{
$content_dir = 'upload/'; // dossier où sera déplacé le fichier
$tmp_file = $_FILES['fichier']['tmp_name'];
if( !is_uploaded_file($tmp_file) )
{
exit("Le fichier est introuvable");
}
// on vérifie maintenant l'extension
$type_file = $_FILES['fichier']['type'];
if( !strstr($type_file, 'jpg') && !strstr($type_file, 'jpeg') && !strstr($type_file, 'png') && !strstr($type_file, 'bmp') &&
!strstr($type_file, 'gif') )
{
exit("Le fichier n'est pas une image");
}
// on copie le fichier dans le dossier de destination
$ext = substr($_FILES['fichier']['name'], strrpos($_FILES['fichier']['name'], '.'));
$filename_orig = $nomImage.$ext;
echo $filename_orig;
class Hoathis_Images {
public static function resize(
$filename_orig,
$filename_dest,
$max_width,
$max_height
){
// Calcul des nouvelles dimensions
list($width_orig, $height_orig) = getimagesize($filename_orig);
if($width_orig > $max_width || $height_orig > $max_height){
$fileinfos = pathinfo($filename_orig);
$ext = strtolower($fileinfos['extension']);
switch($ext){
case 'jpg':
case 'jpeg':
$old_img = imagecreatefromjpeg($filename_orig);
break;
case 'png':
$old_img = imagecreatefrompng($filename_orig);
break;
case 'gif':
$old_img = imagecreatefromgif($filename_orig);
break;
}
$ratio_orig = $width_orig/$height_orig;
if ($max_width/$max_height > $ratio_orig) {
$new_width = $max_height*$ratio_orig;
$new_height = $max_height;
} else {
$new_height = $max_width/$ratio_orig;
$new_width = $max_width;
}
// Redimensionnement
$new_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_img, $old_img, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
switch($ext){
case 'jpg':
case 'jpeg':
imagejpeg($new_img,$filename_dest,95);
break;
case 'png':
imagepng($new_img,$filename_dest,95);
break;
case 'gif':
imagegif($new_img,$filename_dest,95);
break;
}
}
else {
copy($filename_orig,$filename_dest);
}
}
public static function crop(
$filename_orig,
$filename_dest,
$x_orig,
$y_orig,
$width,
$height
)
{
$fileinfos = pathinfo($filename_orig);
$ext = strtolower($fileinfos['extension']);
switch($ext){
case 'jpg':
case 'jpeg':
$old_img = imagecreatefromjpeg($filename_orig);
break;
case 'png':
$old_img = imagecreatefrompng($filename_orig);
break;
case 'gif':
$old_img = imagecreatefromgif($filename_orig);
break;
}
// Redimensionnement
$new_img = imagecreatetruecolor($width, $height);
imagecopyresampled($new_img, $old_img, 0, 0, $x_orig, $y_orig, $width, $height, $width, $height);
switch($ext){
case 'jpg':
case 'jpeg':
imagejpeg($new_img,$filename_dest,100);
break;
case 'png':
imagepng($new_img,$filename_dest);
break;
case 'gif':
imagegif($new_img,$filename_dest);
break;
}
}
}
if( preg_match('#[\x00-\x1F\x7F-\x9F/\\\\]#', $name_file) )
{
exit("Nom de fichier non valide");
}
else if( !move_uploaded_file($tmp_file, $content_dir . $name_file) )
{
exit("Impossible de copier le fichier dans $content_dir");
}
Hoathis_Images($content_dir.$filename_dest,171, 107);
}
?>
<!----------------------------------------------------------------------------------------------->
[/PHP]
Le principe que je voulais faire est proche de bout de code qui contient des erreurs aussi :
[PHP]
<?php
// Crop dimensions.
$width = 200;
$height = 200;
// Set the path to the image to resize
$input_image = "monImage.png";
// Get the size of the original image into an array
$size = getimagesize( $input_image );
// Prepare canvas
$canvas = imagecreatetruecolor( $width, $height );
// Create a new image in the memory from the file
$cropped = imagecreatefrompng( $input_image );
// Prepare image crop - center the crop on the image
$newwidth = $size[0] / 2;
$newheight = $size[1] / 2;
$cropLeft = ( $newwidth/2 ) - ( $width/2 );
$cropHeight = ( $newheight/2 ) - ( $height/2 );
// Generate the cropped image
imagecopyresized( $canvas, $cropped, 0,0, $cropLeft, $cropHeight, $size[0], $size[1], $newwidth, $newheight );
header('Content-type: image/jpeg');
imagejpeg($canvas);
// Clear the memory of the tempory images
imagedestroy( $canvas );
imagedestroy( $cropped );
?>
[/PHP]