[ 987 553 ]
[ 38.107.191.81 ]
Créer des titres en image PHP avec la librairie GDVoici comment créer des titres en image, avec la librairie GD2. Mise en application- Le code pour créer notre image texte, enregistrez le sous "titre_image.php" :
<?php
// on créer une image vide
$image = imagecreatetruecolor($_GET['x'], $_GET['y']);
// couleur de la transparence
$fond_R = hexdec(substr($_GET['color_fond'], 0, 2));
$fond_V = hexdec(substr($_GET['color_fond'], 2, 2));
$fond_B = hexdec(substr($_GET['color_fond'], 4, 2));
$color_fond = imagecolorallocate($image, $fond_R, $fond_V, $fond_B);
// on remplit l'image
imagefill($image, 0, 0, $color_fond);
// transparence du fond de l'image
imagecolortransparent($image, $color_fond);
// couleur du texte
$text_R = hexdec(substr($_GET['color_text'], 0, 2));
$text_V = hexdec(substr($_GET['color_text'], 2, 2));
$text_B = hexdec(substr($_GET['color_text'], 4, 2));
$color_text = imagecolorallocate($image, $text_R, $text_V, $text_B);
// /fonts/ est le répertoire ou vous stockerez la police utilisée.
// $_SERVER["DOCUMENT_ROOT"] indique le chemin absolu ou votre base de site se situe.
putenv('GDFONTPATH='.$_SERVER["DOCUMENT_ROOT"].'/fonts/');
// centrage du texte en hauteur
$pos_y = ($_GET['y'] + $_GET['size']) /2;
// on écrit le premier texte
imagettftext($image, $_GET['size'], 0, 2, $pos_y, $color_text, $_GET['font'], stripslashes($_GET['text']));
// on affiche l'image
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
- Le code pour afficher notre image texte : <img src="titre_image.php?x=260&y=30&color_fond=ffffff&color_text=cc6699&font=ft66&size=17&text=Mon titre en image !" alt="" /> Variables utilisées pour l'affichage :
[0] commentaire - Voir/Editer |