Précédent   Forum des professionnels en informatique > PHP > Bibliothèques et frameworks > Images > Imagick
Imagick Forum d'entraide pour l'extension Imagick permettant de manipuler des images en PHP (port de l'API ImageMagick).
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 05/10/2006, 11h45   #1
Membre habitué
 
Avatar de alexmorel
 
Inscription : septembre 2003
Messages : 196
Détails du profil
Informations personnelles :
Âge : 29

Informations forums :
Inscription : septembre 2003
Messages : 196
Points : 120
Points : 120
Envoyer un message via MSN à alexmorel
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 :
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
__________________
A.Morel
alexmorel est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2006, 12h00   #2
Expert Confirmé Sénior
 
Avatar de Mr N.
 
Inscription : septembre 2004
Messages : 5 421
Détails du profil
Informations forums :
Inscription : septembre 2004
Messages : 5 421
Points : 5 835
Points : 5 835
Je te suggère de lire les commentaire de la doc :
http://php.net/imagecreatetruecolor#66448
Mr N. est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2006, 13h08   #3
Membre habitué
 
Avatar de alexmorel
 
Inscription : septembre 2003
Messages : 196
Détails du profil
Informations personnelles :
Âge : 29

Informations forums :
Inscription : septembre 2003
Messages : 196
Points : 120
Points : 120
Envoyer un message via MSN à alexmorel
alors j'ai trouvé dans ta doc comment faire l'image de font transparent.

Mais la qualité du texte diminue !

Code :
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
__________________
A.Morel
alexmorel est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2006, 14h28   #4
Membre chevronné
 
Avatar de kankrelune
 
Inscription : décembre 2005
Messages : 766
Détails du profil
Informations forums :
Inscription : décembre 2005
Messages : 766
Points : 745
Points : 745
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 :
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°
kankrelune est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2006, 14h41   #5
Membre habitué
 
Avatar de alexmorel
 
Inscription : septembre 2003
Messages : 196
Détails du profil
Informations personnelles :
Âge : 29

Informations forums :
Inscription : septembre 2003
Messages : 196
Points : 120
Points : 120
Envoyer un message via MSN à alexmorel
si je reprend ton code l'image est pas crée !
__________________
A.Morel
alexmorel est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2006, 14h44   #6
Membre chevronné
 
Avatar de kankrelune
 
Inscription : décembre 2005
Messages : 766
Détails du profil
Informations forums :
Inscription : décembre 2005
Messages : 766
Points : 745
Points : 745
Fautes d'inattention...

Code :
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°
kankrelune est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2006, 14h51   #7
Membre habitué
 
Avatar de alexmorel
 
Inscription : septembre 2003
Messages : 196
Détails du profil
Informations personnelles :
Âge : 29

Informations forums :
Inscription : septembre 2003
Messages : 196
Points : 120
Points : 120
Envoyer un message via MSN à alexmorel
ouai j'ai vu les 0, 0 mais pas le $imageOut.


Ben maitenant j'ai l'image mais avec un fond rose !
__________________
A.Morel
alexmorel est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2006, 15h07   #8
Membre chevronné
 
Avatar de kankrelune
 
Inscription : décembre 2005
Messages : 766
Détails du profil
Informations forums :
Inscription : décembre 2005
Messages : 766
Points : 745
Points : 745
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°
kankrelune est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2006, 15h26   #9
Membre habitué
 
Avatar de alexmorel
 
Inscription : septembre 2003
Messages : 196
Détails du profil
Informations personnelles :
Âge : 29

Informations forums :
Inscription : septembre 2003
Messages : 196
Points : 120
Points : 120
Envoyer un message via MSN à alexmorel
Voilà j'ai trouvé comment faire :


Code :
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.
__________________
A.Morel
alexmorel est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 22h50.


 
 
 
 
Partenaires

Hébergement Web