No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Vous devez être membre et vous identifier pour publier un article.
Les visiteurs peuvent toutefois commenter chaque article par une réponse.

Image aléatoire

Astuces de l’Infobrol (Développement)Article publié le 27/08/2006 08:17:53


Comme Lea72 a demandé comment afficher une image aléatoire, j'en profite pour poster ici le code qui me sert à afficher mon avatar.

En effet, quand nous nous inscrivons sur certains forums, nous pouvons définir une image pour nous représenter. Mais si la possibilité existe d'utiliser une image distante, les règles de sécurité n'acceptent qu'un fichier de type image. Comment dès lors soumettre un script distant qui comporte une autre extension?

Nous pouvons utiliser les erreurs 404 (fichier non trouvé) et les fichiers htaccess.
Nous allons créer dans un répertoire un fichier .htaccess qui contiendra les lignes suivantes :
  1. ‹Files "steph.gif"›
  2. ErrorDocument 404 /img/rand/steph.php
  3. ‹/Files›

De cette manière, si nous désirons atteindre le fichier steph.gif, comme ce dernier n'existe pas le script sera exécuté.

Voici donc le script qui permet d'envoyer une image aléatoire :
  1. ‹?php
  2. // CONFIGURATION ============================
  3. $max = 9; // Nombre d'images
  4. $prefix = 'steph'; // Texte situé avant le numéro de l'image
  5. $ext = '.gif'; // Extension des fichiers
  6. // FIN DE CONFIGURATION =====================
  7. $img = rand (0,$max);
  8. // On indique que c'est une image
  9. switch ($ext) {
  10. case ".jpg":
  11. case ".jpeg":
  12. header('Content-type: image/jpeg');
  13. break;
  14. case ".gif":
  15. header('Content-type: image/gif');
  16. break;
  17. case ".png":
  18. header('Content-type: image/png');
  19. break;
  20. }
  21. // On ouvre l'image :
  22. $file = $prefix.$img.$ext;
  23. //si on ne peut pas lire une image aléatoire, on lit celle-ci
  24. if(!$img = readfile($file))$img = readfile("steph0.gif");
  25. // Et on l'affiche
  26. echo $img;
  27. ?›

Le résultat affiche ceci :

Bon amusement ;-)

Avatar :: Steph Un article de Steph

Source : indéterminée


Sélection, tri et recherche d'articles
FILTRER :
TRIER :1er critère : 2e critère :
CHERCHER : Dans les titres Dans le contenu


[Afficher les liens en fonction des critères du formulaire ci-dessus]

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 13/09/2004, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/ast-rf-368.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.