Générer une image aléatoire

  Information

Ce code source va vous permettre de créer une image aléatoire.

L'image est créée à partir de fonctions de la librairie GD.

  code source classé dans  Images - Graphisme

 
 01    
 02    
 03    
 04    
 05    
 06    
 07    
 08    
 09    
 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    
 45    
 46    
 47    
 48    
 49    
 50    
 51    
 52    
 53    
 54    
 55    
 56    
 57    
 58    
 59    
 60    
 61    
 62    
 63    
 64    
 65    
 66    
 67    
 68    
 69    
 70    
 71    
 72    
 73    
 74    
 75    
 76    
 77    
 78    
 79    
 80    
 81    
 82    
 83    
 84    
 85    
 86    
 87    
 88    
 89    
 90    
 91    
 92    
 93    
 94    
 95    
 96    
 97    
 98    
 99    
 100    
 101    
 102    
 103    
 104    
 105    
 106    
 107    
 108    
 109    
 110    
 111    
 112    
 113    
 114    
 115    
 116    
 117    
 118    
 119    
 120    
 121    
 122    
 123    
 124    
 125    
 126    
 127    
 128    
 129    
 130    
 131    
 132    
 133    
 134    
 135    
 136    
 137    
 138    
 139    
 140    
 141    
 142    
 143    
 144    
 145    
 146    
 147    
                               
<?php
/*---------------------------------------------------------------*/
/*
    Titre : Générer une image aléatoire                                                                                
                                                                                                                          
    URL   : https://phpsources.net/code_s.php?id=255
    Auteur           : Cam                                                                                                
    Date édition     : 04 Mars 2007                                                                                       
    Date mise à jour : 12 Aout 2019                                                                                      
    Rapport de la maj:                                                                                                    
    - fonctionnement du code vérifié                                                                                    
    - maintenance du code                                                                                                 
    - modification de la description                                                                                      
*/
/*---------------------------------------------------------------*/

    Header('Content-type: image/png');
    $img ImageCreate200200);
    $colorbackgr ImageColorAllocate$img000);
    //nombre de cercles
    $u1 Rand1020);
    for( $i=1$i $u1$i++)
    {
      //couleurs
      $col1 Rand71255);
      $col2 Rand71255);
      $col3 Rand71255);
      //position des centres x y
      $a1 Rand( -5105);
      $a2 Rand( -5105);
      //largeur et hauteur x y
      $a3 Rand5150);
      $a4 Rand5150);
      //début du cercle et fin
      $deg1 Rand0360);
      $deg2 Rand0360);
      //type de cercle
      $a5 Rand01);
  
  if($a5 == 0)
  {
    ImageArc$img$a1$a2$a3$a4$deg1$deg2ImageColorAllocate$img
$col1$col2$col3));
    ImageArc$img$a1+1$a2+1$a3+1$a4+1$deg1+1$deg2-1
ImageColorAllocate$img$col1-10$col2-10$col3-10));
    ImageArc$img$a1-1$a2-2$a3-1$a4-1$deg1+1$deg2-1
ImageColorAllocate$img$col1-10$col2-10$col3-10));
    ImageArc$img$a1+2$a2+2$a3+2$a4+2$deg1+2$deg2-2
ImageColorAllocate$img$col1-20$col2-20$col3-20));
    ImageArc$img$a1-2$a2-2$a3-2$a4-2$deg1+2$deg2-2
ImageColorAllocate$img$col1-20$col2-20$col3-20));
    ImageArc$img$a1+3$a2+3$a3+3$a4+3$deg1+3$deg2-3
ImageColorAllocate$img$col1-30$col2-30$col3-30));
    ImageArc$img$a1-3$a2-3$a3-3$a4-3$deg1+3$deg2-3
ImageColorAllocate$img$col1-30$col2-30$col3-30));
    ImageArc$img$a1+4$a2+4$a3+4$a4+4$deg1+4$deg2-4
ImageColorAllocate$img$col1-40$col2-40$col3-40));
    ImageArc$img$a1-4$a2-4$a3-4$a4-4$deg1+4$deg2-4
ImageColorAllocate$img$col1-40$col2-40$col3-40));
    ImageArc$img$a1+5$a2+5$a3+5$a4+5$deg1+5$deg2-5
ImageColorAllocate$img$col1-50$col2-50$col3-50));
    ImageArc$img$a1-5$a2-5$a3-5$a4-5$deg1+5$deg2-5
ImageColorAllocate$img$col1-50$col2-50$col3-50));
    ImageArc$img$a1+6$a2+6$a3+6$a4+6$deg1+6$deg2-6
ImageColorAllocate$img$col1-60$col2-60$col3-60));
    ImageArc$img$a1-6$a2-6$a3-6$a4-6$deg1+6$deg2-6
ImageColorAllocate$img$col1-60$col2-60$col3-60));
    ImageArc$img$a1+7$a2+7$a3+7$a4+7$deg1+7$deg2-7
ImageColorAllocate$img$col1-70$col2-70$col3-70));
    ImageArc$img$a1-7$a2-7$a3-7$a4-7$deg1+7$deg2-7
ImageColorAllocate$img$col1-70$col2-70$col3-70));
  }
  else
  {
    ImageArc$img$a1$a2$a3$a4$deg1$deg2ImageColorAllocate$img
$col1$col2$col3));
    ImageArc$img$a1+1*Rand(-1,1), $a2+1*Rand(-1,1), $a3+1*Rand(-1,1), $a4+1*
Rand(-1,1), $deg1+1$deg2-1ImageColorAllocate$img$col1-10$col2-10
$col3-10));
    ImageArc$img$a1-1*Rand(-1,1), $a2-2*Rand(-1,1), $a3-1*Rand(-1,1), $a4-1*
Rand(-1,1), $deg1+1$deg2-1ImageColorAllocate$img$col1-10$col2-10
$col3-10));
    ImageArc$img$a1+2*Rand(-1,1), $a2+2*Rand(-1,1), $a3+2*Rand(-1,1), $a4+2*
Rand(-1,1), $deg1+2$deg2-2ImageColorAllocate$img$col1-20$col2-20
$col3-20));
    ImageArc$img$a1-2*Rand(-1,1), $a2-2*Rand(-1,1), $a3-2*Rand(-1,1), $a4-2*
Rand(-1,1), $deg1+2$deg2-2ImageColorAllocate$img$col1-20$col2-20
$col3-20));
    ImageArc$img$a1+3*Rand(-1,1), $a2+3*Rand(-1,1), $a3+3*Rand(-1,1), $a4+3*
Rand(-1,1), $deg1+3$deg2-3ImageColorAllocate$img$col1-30$col2-30
$col3-30));
    ImageArc$img$a1-3*Rand(-1,1), $a2-3*Rand(-1,1), $a3-3*Rand(-1,1), $a4-3*
Rand(-1,1), $deg1+3$deg2-3ImageColorAllocate$img$col1-30$col2-30
$col3-30));
    ImageArc$img$a1+4*Rand(-1,1), $a2+4*Rand(-1,1), $a3+4*Rand(-1,1), $a4+4*
Rand(-1,1), $deg1+4$deg2-4ImageColorAllocate$img$col1-40$col2-40
$col3-40));
    ImageArc$img$a1-4*Rand(-1,1), $a2-4*Rand(-1,1), $a3-4*Rand(-1,1), $a4-4*
Rand(-1,1), $deg1+4$deg2-4ImageColorAllocate$img$col1-40$col2-40
$col3-40));
    ImageArc$img$a1+5*Rand(-1,1), $a2+5*Rand(-1,1), $a3+5*Rand(-1,1), $a4+5*
Rand(-1,1), $deg1+5$deg2-5ImageColorAllocate$img$col1-50$col2-50
$col3-50));
    ImageArc$img$a1-5*Rand(-1,1), $a2-5*Rand(-1,1), $a3-5*Rand(-1,1), $a4-5*
Rand(-1,1), $deg1+5$deg2-5ImageColorAllocate$img$col1-50$col2-50
$col3-50));
    ImageArc$img$a1+6*Rand(-1,1), $a2+6*Rand(-1,1), $a3+6*Rand(-1,1), $a4+6*
Rand(-1,1), $deg1+6$deg2-6ImageColorAllocate$img$col1-60$col2-60
$col3-60));
    ImageArc$img$a1-6*Rand(-1,1), $a2-6*Rand(-1,1), $a3-6*Rand(-1,1), $a4-6*
Rand(-1,1), $deg1+6$deg2-6ImageColorAllocate$img$col1-60$col2-60
$col3-60));
    ImageArc$img$a1+7*Rand(-1,1), $a2+7*Rand(-1,1), $a3+7*Rand(-1,1), $a4+7*
Rand(-1,1), $deg1+7$deg2-7ImageColorAllocate$img$col1-70$col2-70
$col3-70));
    ImageArc$img$a1-7*Rand(-1,1), $a2-7*Rand(-1,1), $a3-7*Rand(-1,1), $a4-7*
Rand(-1,1), $deg1+7$deg2-7ImageColorAllocate$img$col1-70$col2-70
$col3-70));
  }
}
    //image 100x100 a été crée
    //symétrie
    $x 0;    $y 0;
    while( $x <= 100)
    {
      for( $y=0$y<=100$y++)
      {
        //image de droite
        ImageSetPixel$img200-($x), $yImageColorAt$img$x ,$y));
        //image dessous gauche
        ImageSetPixel$img$x200-$yImageColorAt$img$x$y));
        //image dessous droite
        ImageSetPixel$img200-$x200-$yImageColorAt$img$x$y));
      }
      $x++;
    }
    //transparent ?

     ImageColorTransparent$imgImageColorClosest$img000));

    // On crée l'image
    ImagePng($img);
    // On libère l'espace mémoire alloué à l'image
    ImageDestroy($img);
?>

          Fonctions du code - Doc officielle PHP

   php.net   Description Versions PHP OUTIL
   header Envoie un en-tête HTTP PHP 4, PHP 5, PHP 7, PHP 8
   imagearc Dessine une ellipse partielle PHP 4, PHP 5, PHP 7, PHP 8
   imagecolorallocate Alloue une couleur pour une image PHP 4, PHP 5, PHP 7, PHP 8
   imagecolorat Retourne l'index de la couleur d'un pixel donné PHP 4, PHP 5, PHP 7, PHP 8
   imagecolorclosest Retourne l'index de la couleur la plus proche d'une couleur donnée PHP 4, PHP 5, PHP 7, PHP 8
   imagecolortransparent Définit la couleur transparente PHP 4, PHP 5, PHP 7, PHP 8
   imagecreate Crée une nouvelle image à palette PHP 4, PHP 5, PHP 7, PHP 8
   imagedestroy Détruit une image PHP 4, PHP 5, PHP 7, PHP 8
   imagepng Envoie une image PNG vers un navigateur ou un fichier PHP 4, PHP 5, PHP 7, PHP 8
   imagesetpixel Dessine un pixel PHP 4, PHP 5, PHP 7, PHP 8
   rand Génère une valeur aléatoire PHP 4, PHP 5, PHP 7, PHP 8

   Dites merci aux auteurs pour leurs travail, ça ne coûte rien et ça fait toujours plaisir wink

 Autres snippets qui pourraient vous intéressez

Génération basique de password aléatoire

Compatibilité : PHP 5, PHP 7

2 Fonctions permettant la génération d'un mot de passe aléatoire. Le nombre de caractère a générer est passé en argument dans la fonction.

Génère 1 chaine de caractère unique et aléatoire

Compatibilité : PHP 5, PHP 7

Générer une chaine de caractère unique et aléatoire. 3 fonctions différentes qui génère de manière différentes ces chaines.

Mot de passe aléatoire lettres et chiffres

Compatibilité : PHP 5, PHP 7

Obtenir un mot de passe aléatoire de la taille que l'on veut et composé de chiffres et de lettres.

Sélection aléatoire dans une table MySQL

Compatibilité : PHP 4, PHP 5

Sélectionne le nombre d'enregistrements souhaités de manière aléatoire dans une table MySQL. Code compatible php 4 et php 5

Générer 1 nombre au hasard compris entre 2 valeurs

Compatibilité : PHP 5, PHP 7

Générer un nombre au hasard soit avec la fonction mt_rand () ou avec la fonction rand ()

Présentation de PHP

PHP débutant et initié 50 Tutoriel

Présentation de MySQL

avatar

Cam

  04 Mars 2007

  SOURCE   Télécharger

Information sur les mises à jour

Dernière mise à jour :

    12 Aout 2019
    fonctionnement du code vérifié
    maintenance du code
    modification de la description

11 252 Vues
Compatibilité
PHP 5, 7 et 8+
54 codes

Catégorie :  Images - Graphisme

Ajoute à votre image un cadre noir 3D
Ajouter un logo en filigrane (watermark) sur ses images
Composantes d'une image
concaténation d'images pour faire des sprites CSS
Connaitre la taille d'une image et son format
Création d'images miniatures
Création d'images miniatures carrés
Créer des images fractales
Crée des titres sympa
Creer un camembert 2D facilement
Créer une vignette avec GD en se basant sur le type d'image
Crop d'une image JPEG
Dessine un polygone régulier
Dessine un polygone regulier plein
Donne la couleur opposée d'une couleur
Ecrire des nombres dans une images, sans police
Ecrire un texte dans une image avec retour à la ligne automatique
Ecrire un texte dans une image PNG
Ecrire un texte sur une image - Code de base
Effet miroir sur des images
Effet solarisation
Egalisation des couleurs d'une image
Explorer et visionner des images dans un répertoire
Extrait un canal de couleur d'une image
Fabrication d'une vignette et sauvegarde de la vignette
Fonction hexadécimal vers RVB (RGB)
Fonctions imagecreatefrombmp et imagebmp
Générer une image aléatoire
GD/Graphiques
Homothétie d'une image
Image aléatoire
Image en noir et blanc
Image transparente
Images Miniatures
Ligne diagonale
Modifications des composantes
Photo du jour choisi de manière aléatoire à partir d'un répertoire
Redimensionne des images JPG et PNG
Redimensionner image en gardant les proportions
Redimensionner une image
Redimensionner une image sans distorsion
Regarde si l'image est présente sur le serveur
Représentation graphique
Rotation de triangle
Rotation image
Sélectionne et affiche la dernière image mise à jour
Sépia aspect ancien
Symétrie horizontale
Taille image maximum en pixels
Texte multi-lignes sur une image avec une police PostScript Type1
Texte ombré en police TrueType
Transparence simple sur une image
Un masque sur une image
Une galerie de photos automatique depuis un répertoire