note 68920 added to function.imagettftext

From: Date: Wed, 16 Aug 2006 05:16:11 +0000
Subject: note 68920 added to function.imagettftext
Groups: php.notes 
Request: Send a blank email to php-notes+get-115960@lists.php.net to get a copy of this message
I spent days looking for this, couldn't find it, so just made it myself. This is an ultra simple text banner that keeps the text pretty much centered (not perfect when text is angled) vertically and horizontally. Size, font, colors are easy to edit and in HTML version for the colors. Any additions (maybe for multi-line functionality) can be added if you desire. <?php ### Declare this script will be displayed as a PNG image. header("Content-type: image/png"); ####################### BEGIN USER EDITS ####################### $imagewidth = 500; $imageheight = 100; $fontsize = "20"; $fontangle = "0"; $font = "arial.ttf"; $text = "123456789"; $backgroundcolor = "003366"; $textcolor = "FFCC66"; ######################## 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)+($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); ?> ---- Server IP: 64.71.164.2 Probable Submitter: 65.102.253.144 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.imagettftext.php Edit -- https://master.php.net/note/edit/68920 Del: integrated -- https://master.php.net/note/delete/68920/integrated Del: useless -- https://master.php.net/note/delete/68920/useless Del: bad code -- https://master.php.net/note/delete/68920/bad+code Del: spam -- https://master.php.net/note/delete/68920/spam Del: non-english -- https://master.php.net/note/delete/68920/non-english Del: in docs -- https://master.php.net/note/delete/68920/in+docs Del: other reasons-- https://master.php.net/note/delete/68920 Reject -- https://master.php.net/note/reject/68920 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#115960) next »