1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
<?php
function thumbail($file, $maxWidth, $maxHeight){//Créer une image à partir de $file
$img = ImageCreateFromJpeg("$file");
//Dimensions de l'image
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
//Facteur largeur/hauteur des dimensions max
$whFact = $maxWidth/$maxHeight;
//Facteur largeur/hauteur de l'original
$imgWhFact = $imgWidth/$imgHeight;
//fixe les dimensions du thumb
if($whFact < $imgWhFact){//Si largeur déterminante
$thumbWidth = $maxWidth;
$thumbHeight = $thumbWidth/$imgWhFact;
} else { //Si hauteur déterminante
$thumbHeight = $maxHeight;
$thumbWidth = $thumbHeight*$imgWhFact;
}
//Crée le thumb (image réduite)
$imgThumb = ImageCreateTruecolor($thumbWidth, $thumbHeight);
//Insère l'image de base redimensionnée
ImageCopyResized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
//Nom du fichier thumb
$imgThumbName = "thumb_".$file;
//Crée le fichier thumb
$fp = fopen($imgThumbName, "w");
fclose($fp);
//Renvoie le thumb créé
ImageJpeg($imgThumb, $imgThumbName);
return $imgThumbName;
}
thumbail('scary-movie-4-wallpaper-4-1024.jpg',100,100);
?>
<html>
<head>
<title>Test...</title>
</head>
<body>
<img src='thumb_scary-movie-4-wallpaper-4-1024.jpg'/>
</body>
</html> |
Partager