IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[ImageMagick] Création image transparente


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre habitué Avatar de alexmorel
    Profil pro
    Inscrit en
    Septembre 2003
    Messages
    196
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2003
    Messages : 196
    Points : 157
    Points
    157
    Par défaut [ImageMagick] Création image transparente
    Bonjour,

    je voudrai cree une image transparent avec un texte par dessus qui utilise une police TTF


    Pour crée le tout avec une couleur pas de problème mais le rendre transparent soucis

    J'utilise se bout de code ...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
     
    session_start();
    ### Declare this script will be displayed as a PNG image.
    header("Content-type: image/png");
     
    ####################### BEGIN USER EDITS #######################
    $imagewidth = 500;
    $imageheight = 90;
    $fontsize = "30";
    $fontangle = "0";
    $font = "police.ttf";
    $text = 'Mon texte a afficher';
    $backgroundcolor = "E1ECF7";
    $textcolor = "000033";
    ######################## END USER EDITS ########################
     
    ### Convert HTML backgound color to RGB
    if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $backgroundcolor, $bgrgb ) )
    {$bgred = hexdec( $bgrgb[1] );  $bggreen = hexdec( $bgrgb[2] );  $bgblue = hexdec( $bgrgb[3] );}
     
    ### Convert HTML text color to RGB
    if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $textcolor, $textrgb ) )
    {$textred = hexdec( $textrgb[1] );  $textgreen = hexdec( $textrgb[2] );  $textblue = hexdec( $textrgb[3] );}
     
    ### Create image
    $im = imagecreate( $imagewidth, $imageheight );
     
    ### Declare image's background color
    $bgcolor = imagecolorallocate($im, $bgred,$bggreen,$bgblue);
     
    ### Declare image's text color
    $fontcolor = imagecolorallocate($im, $textred, $textgreen, $textblue);
     
    ### Get exact dimensions of text string
    $box = @imageTTFBbox($fontsize, $fontangle, $font, $text);
     
    ### Get width of text from dimensions
    $textwidth = abs($box[4] - $box[0]);
     
    ### Get height of text from dimensions
    $textheight = abs($box[5] - $box[1]);
     
    ### Get x-coordinate of centered text horizontally using length of the image and length of the text
    $xcord = ($imagewidth/2)-($textwidth/2)-2;
     
    ### Get y-coordinate of centered text vertically using height of the image and height of the text
    $ycord = ($imageheight/2-10)+($textheight/2);
     
    ### Declare completed image with colors, font, text, and text location
    imagettftext ($im, $fontsize, $fontangle, $xcord, $ycord, $fontcolor, $font, $text );
     
    ### Display completed image as PNG
    imagepng($im);
     
     
    ### Close the image
    imagedestroy($im);
    Voilà je trouve pas comment faire l'image de fond transparent et le texte de dessus de couleur..

    Merci

  2. #2
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    Je te suggère de lire les commentaire de la doc :
    http://php.net/imagecreatetruecolor#66448

  3. #3
    Membre habitué Avatar de alexmorel
    Profil pro
    Inscrit en
    Septembre 2003
    Messages
    196
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2003
    Messages : 196
    Points : 157
    Points
    157
    Par défaut
    alors j'ai trouvé dans ta doc comment faire l'image de font transparent.

    Mais la qualité du texte diminue !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    /* En début de code */
    function imageCreateTransparent($x, $y, $red, $green, $blue) { 
       $imageOut = imagecreate($x, $y);
       $colourBlack = imagecolorallocate($imageOut, $red, $green, $blue);
       imagecolortransparent($imageOut, $colourBlack);
       return $imageOut;
    }
     
    $im = imageCreateTransparent($imagewidth, $imageheight, $textred, $textgreen, $textblue);
     
     
    ### Declare completed image with colors, font, text, and text location
    imagettftext ($im, $fontsize, $fontangle, $xcord, $ycord, $fontcolor, $font, $text );
    Mais là le texte donne moche et impressit.


    Merci d'avance

  4. #4
    Membre éclairé
    Avatar de kankrelune
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    763
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 763
    Points : 858
    Points
    858
    Par défaut
    Il faut utiliser imagecreatetruecolor et non pas imagecreate et il faut utiliser une couleur que tu est sûr de ne pas retrouver dans ton texte (dans l'exemple le rose)... .. .

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    function imageCreateTransparent($x, $y) 
    { 
       $imageOut = imagecreatetruecolor($x, $y);
       $bg = imagecolorallocate($imageOut, 255,0,255)
       imagefill($imageout,$bg);
       imagecolortransparent($imageOut, 0, 0, $bg);
     
       return $imageOut;
    }
    @ tchaOo°

  5. #5
    Membre habitué Avatar de alexmorel
    Profil pro
    Inscrit en
    Septembre 2003
    Messages
    196
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2003
    Messages : 196
    Points : 157
    Points
    157
    Par défaut
    si je reprend ton code l'image est pas crée !

  6. #6
    Membre éclairé
    Avatar de kankrelune
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    763
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 763
    Points : 858
    Points
    858
    Par défaut
    Fautes d'inattention...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function imageCreateTransparent($x, $y) 
    { 
       $imageOut = imagecreatetruecolor($x, $y);
       $bg = imagecolorallocate($imageOut, 255,0,255);
       imagefill($imageOut, 0, 0, $bg);
       imagecolortransparent($imageOut, $bg);
       
       return $imageOut;
    }
    @ tchaOo°

  7. #7
    Membre habitué Avatar de alexmorel
    Profil pro
    Inscrit en
    Septembre 2003
    Messages
    196
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2003
    Messages : 196
    Points : 157
    Points
    157
    Par défaut
    ouai j'ai vu les 0, 0 mais pas le $imageOut.


    Ben maitenant j'ai l'image mais avec un fond rose !

  8. #8
    Membre éclairé
    Avatar de kankrelune
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    763
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 763
    Points : 858
    Points
    858
    Par défaut
    Bizar c'est comme ça que je fais et j'ais pas de problème... de chez moi ça marche... .. .

    Tu l'affiche dans quel format ton image, tu utiliserais pas IE pour la voir... .. ?

    @ tchaOo°

  9. #9
    Membre habitué Avatar de alexmorel
    Profil pro
    Inscrit en
    Septembre 2003
    Messages
    196
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2003
    Messages : 196
    Points : 157
    Points
    157
    Par défaut
    Voilà j'ai trouvé comment faire :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    45
     
    // on déclare nos constantes
    $text = 'centre de jeunesse régional';
    $fontfile = "police.ttf";
    $size = 30;
    $fontangle = 0;
    $textcolor = "000033";
    $imagewidth = 500;
    $imageheight = 90;
     
    // on determine la taille occupee par le texte
    $box = @imagettfbbox($size, $fontangle, $fontfile, $text);
    $textwidth = abs($box[4] - $box[0]);
    $textheight = abs($box[5] - $box[1]);
     
    // La position du texte dans images
    $xcord = ($imagewidth/2)-($textwidth/2)-2;
    $ycord = ($imageheight/2-10)+($textheight/2);
     
    // on cree l'image a la bonne taille, avec un peu de marge
    $img = imagecreatetruecolor($imagewidth, $imageheight);
     
    //forcer une palette de 256 couleurs & on génère un png 8 bits 
    imagetruecolortopalette($img, true, 256);
     
    // on alloue les couleurs de fond et de texte
    $bg = imagecolorallocate($img, 127, 127, 127);
    $fg = imagecolorallocate($img, 0, 0, 0);
     
    // on remplit la couleur de fond, et on la declare transparente
    imagefilledrectangle($img, 0, 0, $imagewidth, $imageheight, $bg);
    imagecolortransparent($img, $bg);
     
    // on determine la couleur du texte
    if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $textcolor, $textrgb ) )
    {$textred = hexdec( $textrgb[1] );  $textgreen = hexdec( $textrgb[2] );  $textblue = hexdec( $textrgb[3] );}
    $fontcolor = imagecolorallocate($img, $textred, $textgreen, $textblue);
     
    // on ecrit le texte
    imagettftext($img, $size, $fontangle, $xcord, $ycord, $fontcolor, $fontfile, $text);
     
    // on renvoie l'image
    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
    Apres avoir cherché un moment, j'ai donc trouvé qu'il fallait forcer une palette de 256 couleurs, grâce à l'instruction

    imagetruecolortopalette($img, true, 256);

    Avec ça on génère un png 8 bits et tous les navigateurs le gère bien.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [ImageMagick] Création d'image avec des apostrophes
    Par Adaviada dans le forum Bibliothèques et frameworks
    Réponses: 9
    Dernier message: 05/07/2007, 12h35
  2. [GD] Création partie d'image transparente
    Par mussara dans le forum Bibliothèques et frameworks
    Réponses: 7
    Dernier message: 07/12/2006, 20h06
  3. [ImageMagick] Création d'une image miniature
    Par popeye82 dans le forum Bibliothèques et frameworks
    Réponses: 5
    Dernier message: 01/02/2006, 20h10
  4. [ImageMagick] Création d'image à la volée
    Par gdawirs dans le forum Bibliothèques et frameworks
    Réponses: 4
    Dernier message: 21/11/2005, 15h53
  5. [ImageMagick] Création de vignettes (images réduites)
    Par tom06440 dans le forum Bibliothèques et frameworks
    Réponses: 3
    Dernier message: 22/10/2005, 15h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo