note 71308 added to function.imagettfbbox

From: Date: Tue, 21 Nov 2006 22:11:37 +0000
Subject: note 71308 added to function.imagettfbbox
Groups: php.notes 
Request: Send a blank email to php-notes+get-119962@lists.php.net to get a copy of this message
<?php /* |------------------------------------------------------------ | This function fixes an issue with imagettftext when | printing some fonts at small point sizes(Like Myriad Pro) | | Author: Luke Scott |------------------------------------------------------------ */ /** * The text size used before it's resized. */ define( 'DRAW_TTF_BASE', 72); /** * Draws TTF/OTF text on the destination image with best quality. * The built in function imagettftext freaks out with small point * size on some fonts, commonly OTF. Also fixes a position bug * with imagettftext using imagettfbbox. If you just want the text * pass a null value to 'Destination Image Resource' instead. * * @param resource Destination Image Resource * @param int Point Size (GD2), Pixel Size (GD1) * @param int X Position (Destination) * @param int Y Position (Destination) * @param int Font Color - Red (0-255) * @param int Font Color - Green (0-255) * @param int Font Color - Blue (0-255) * @param string TTF/OTF Path * @param string Text to Print * @return null */ function drawttftext( &$des_img, $size, $posX=0, $posY=0, $colorR, $colorG, $colorB, $font='', $text='' ) { //----------------------------------------- // Establish a base size to create text //----------------------------------------- if( ! is_int( DRAW_TTF_BASE ) ) { define( 'DRAW_TTF_BASE', 72); } if( $size >= DRAW_TTF_BASE ) { define( 'DRAW_TTF_BASE', $size * 2 ); } //----------------------------------------- // Simulate text and get data. // Get absolute X, Y, Width, and Height //----------------------------------------- $text_data = imagettfbbox( DRAW_TTF_BASE, 0, $font, $text ); $posX_font = min($text_data[0], $text_data[6]) * -1; $posY_font = min($text_data[5], $text_data[7]) * -1; $height = max($text_data[1], $text_data[3]) - min($text_data[5], $text_data[7]); $width = max($text_data[2], $text_data[4]) - min($text_data[0], $text_data[6]); //----------------------------------------- // Create blank translucent image //----------------------------------------- $im = imagecreatetruecolor( $width, $height ); imagealphablending( $im, false ); $trans = imagecolorallocatealpha( $im, 0, 0, 0, 127 ); imagefilledrectangle( $im, 0, 0, $width, $height, $trans ); imagealphablending( $im, true ); //----------------------------------------- // Draw text onto the blank image //----------------------------------------- $m_color = imagecolorallocate( $im, $colorR, $colorG, $colorB ); imagettftext( $im, DRAW_TTF_BASE, 0, $posX_font, $posY_font, $m_color, $font, $text ); imagealphablending( $im, false ); //----------------------------------------- // Calculate ratio and size of sized text //----------------------------------------- $size_ratio = $size / DRAW_TTF_BASE; $new_width = round($width * $size_ratio); $new_height = round($height * $size_ratio); //----------------------------------------- // Resize text. Can't use resampled direct //----------------------------------------- $rimg = imagecreatetruecolor( $new_width, $new_height ); $bkg = imagecolorallocate($rimg, 0, 0, 0); imagecolortransparent($rimg, $bkg); imagealphablending($rimg, false); imagecopyresampled($rimg, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height); if( $des_img != NULL ) { //----------------------------------------- // Copy resized text to origoinal image //----------------------------------------- imagealphablending($des_img, true); imagecopy( $des_img, $rimg, $posX, $posY, 0, 0, $new_width, $new_height ); imagealphablending($des_img, false); imagedestroy( $im ); imagedestroy( $rimg ); } else { //----------------------------------------- // Just return the resized image //----------------------------------------- $des_img = $rimg; imagedestroy( $im ); } } header('Content-type: image/png'); $img = imagecreatefrompng('myimage.png'); drawttftext( $img, 73, 100, 100, 255,255,255, "MyriadPro-Regular.otf", "Example"); imagesavealpha( $img, true ); imagepng( $img ); ?> ---- Server IP: 64.71.164.2 Probable Submitter: 24.10.41.165 ---- Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php Edit -- https://master.php.net/note/edit/71308 Del: integrated -- https://master.php.net/note/delete/71308/integrated Del: useless -- https://master.php.net/note/delete/71308/useless Del: bad code -- https://master.php.net/note/delete/71308/bad+code Del: spam -- https://master.php.net/note/delete/71308/spam Del: non-english -- https://master.php.net/note/delete/71308/non-english Del: in docs -- https://master.php.net/note/delete/71308/in+docs Del: other reasons-- https://master.php.net/note/delete/71308 Reject -- https://master.php.net/note/reject/71308 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119962) next »