sgaiotti
mercredi 1 juin 2005 à 07:35
bonjour
Voici un code sympathique qui pourait t'aider
CODE
1. <?
2. // -----------------------------------------------------------------------
3. // Fonction de redimensionnement d'image
4. // -----------------------------------------------------------------------
5. function Resize($source, $destination, $width, $height, $stretch, $quality) {
6. // 1 - Gestion des erreurs
7. if (!file_exists($source)) return "Error : File not exist !";
8. if (!function_exists("ImageCreateFromJpeg")) return "Error : Librairie GD non instalée !";
9. // 2 - Lecture de l'image
10. $src_img=imagecreatefromjpeg($source);
11. if (!$src_img) return "Erreur : Lecture impossible de l'image ".$source." !";
12. $w = imagesx($src_img);
13. $h = imagesy($src_img);
14. // 3 - Redimensionnement en largeur
15. if (!$stretch) {
16. if ((($h * $width) / $w) > $height) {
17. $im_w = ($w * $height) / $h;
18. $im_h = $height;
19. } else {
20. $im_w = $width;
21. $im_h = ($h * $width) / $w;
22. }
23. } else {
24. $im_w = $width;
25. $im_h = $height;
26. }
27. $x = ($width-$im_w)/2;
28. $y = ($height-$im_h)/2;
29. // 4 - Création d'une image buffer
30. $dst_img = imagecreate($width, $height);
31. if (!$dst_img) return "Erreur : Buffer non créé : ".$dst_img;
32. $bgc = imagecolorallocate($dst_img, 255, 255, 255);
33. imagefilledrectangle($dst_img, 0, 0, $width, $height, $bgc);
34. imagecopyresized($dst_img,$src_img,$x,$y,0,0,$im_w,$im_h,$w,$h);
35. // 5 - Enregistrement du fichier
36. imagejpeg($dst_img,$destination,$quality);
37. }
38. ?>
Explication de la fonction Resize($source, $destination, $width, $height, $stretch, $quality)
- source : l'image que tu veux redimensionner
-destination : l'image résultante
- width : la largeur de l'image résultante
- height : la hauteur de l'image résultante
- stretch : true/false : Si tu veux étirer ton image ou bien la redimentionner proportionnellement
- quality : la qualitée de l'image résultante ( un chiffre de 0 à 100)
Plus le chiffre est important, meilleur sera la qualité mais attention, grande qualité = grosse image
Apres, tu peux utilisé un tableau de façon a renseigner les tailles d'écran
Exemple, pour un telephone A, taille = 128 * 128
Pour un téléphone B, Taille = 64*64
Tu crées un tableau ( Tel,Width,Height,Stretch)
CODE
TAB[0] = (A,128,128,False)
TAB[1] = (B,128,64,Yes) (le yes précise que les proportion ne sont par respetées
Ensuite, pour un téléphone B, tu utilise la fonction comme suit
CODE
Resize($source, $destination, TAB[1,1], TAB[1,3], TAB[1,3], 50)
Bon courage