note 26508 deleted from function.imagestring by aidan

From: Date: Thu, 26 Aug 2004 06:27:06 +0000
Subject: note 26508 deleted from function.imagestring by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75284@lists.php.net to get a copy of this message
Note Submitter: alexnow at msn dot com ---- Just my little addition to the ImageStringAlignAndWrap() function. This "version" lets you align vertically too: "bottom" , "center" and the default: "top". It can also display a shadow of the text. If you don't want a shadow simply set $drop to '0'. The last parameter $padding is optional, it sets a margin between the edge of the image and the text. I hope it helps! - Alex http://alexnow.com ------------------------------------------ if(!function_exists('ImageStringAlignAndWrap')){ // prevent function redeclare function ImageStringAlignAndWrap($image, $font, $text, $color, $shadow, $drop, $maxwidth, $alignment, $valign, $padding="5") { $fontwidth = ImageFontWidth($font); $fontheight = ImageFontHeight($font); $margin = floor($padding + $drop)/2; // So that shadow is not off image on right align & bottom valign if ($maxwidth != NULL) { $maxcharsperline = floor( ($maxwidth - ($margin * 2)) / $fontwidth); $text = wordwrap($text, $maxcharsperline, "\n", 1); } $lines = explode("\n", $text); switch($valign){ case "center": $y = (imageSY($image) - ($fontheight * sizeof($lines)))/2; break; case "bottom": $y = imageSY($image) - (($fontheight * sizeof($lines)) + $margin); break; default: $y = $margin; break; } switch($alignment){ case "right": while (list($numl, $line) = each($lines)) { ImageString($image, $font, (imagesx($image) - $fontwidth*strlen($line))-$margin+$drop, ($y+$drop), $line, $shadow); ImageString($image, $font, (imagesx($image) - $fontwidth*strlen($line))-$margin, $y, $line, $color); $y += $fontheight; } break; case "center": while (list($numl, $line) = each($lines)) { ImageString($image, $font, floor((imagesx($image) - $fontwidth*strlen($line))/2)+$drop, ($y+$drop), $line, $shadow); ImageString($image, $font, floor((imagesx($image) - $fontwidth*strlen($line))/2), $y, $line, $color); $y += $fontheight; } break; default: while (list($numl, $line) = each($lines)) { ImageString($image, $font, $margin+$drop, ($y+$drop), $line, $shadow); ImageString($image, $font, $margin, $y, $line, $color); $y += $fontheight; } break; } } // end function } // end function_exists ------------------------------------------ Execute with something like: ImageStringAlignAndWrap($im, $font, $string, $white, $black, 3, $imageWidth, "left", "top","7"); or ImageStringAlignAndWrap($im, $font, $string, $white, $black, 3, $imageWidth, "center", "center");

« previous php.notes (#75284) next »