note 60678 deleted from function.imagettfbbox by cmb

From: Date: Sun, 17 Nov 2019 08:53:05 +0000
Subject: note 60678 deleted from function.imagettfbbox by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213089@lists.php.net to get a copy of this message
Note Submitter: Nimja ---- My previous function had 2 bugs which are now fixed: * Align is now pixel perfect, no longer dependant on the first character. Solved by putting a space " " in front of every rendered line, making the basepoint at the exact same place every time. * X Y Coordinates are no longer the base-point, but the perfect top/left coordinates. This made my life designing a LOT easier. Features: * Newline support! (both Windows and Linux) * Paragraph support, no newlines are ignored, not even empty ones. So empty lines are properly supported. * True top/left x/y coordinates instead of TTF basepoint.(at least as close as possible). * Align function for Left, Center and Right * Support for words that are longer then the supported width. <?php //A function for pixel precise text Wrapping function imageTextWrapped(&$img, $x, $y, $width, $font, $color, $text, $textSize, $align="l") { //Recalculate X and Y to have the proper top/left coordinates instead of TTF base-point $y += $textSize; $dimensions = imagettfbbox($textSize, 0, $font, " "); //use a custom string to get a fixed height. $x -= $dimensions[4]-$dimensions[0]; $text = str_replace ("\r", '', $text); //Remove windows line-breaks $srcLines = split ("\n", $text); //Split text into "lines" $dstLines = Array(); // The destination lines array. foreach ($srcLines as $currentL) { $line = ''; $words = split (" ", $currentL); //Split line into words. foreach ($words as $word) { $dimensions = imagettfbbox($textSize, 0, $font, $line.$word); $lineWidth = $dimensions[4] - $dimensions[0]; // get the length of this line, if the word is to be included if ($lineWidth > $width && !empty($line) ) { // check if it is too big if the word was added, if so, then move on. $dstLines[] = ' '.trim($line); //Add the line like it was without spaces. $line = ''; } $line .= $word.' '; } $dstLines[] = ' '.trim($line); //Add the line when the line ends. } //Calculate lineheight by common characters. $dimensions = imagettfbbox($textSize, 0, $font, "MXQJPmxqjp123"); //use a custom string to get a fixed height. $lineHeight = $dimensions[1] - $dimensions[5]; // get the heightof this line $align = strtolower(substr($align,0,1)); //Takes the first letter and converts to lower string. Support for Left, left and l etc. foreach ($dstLines as $nr => $line) { if ($align != "l") { $dimensions = imagettfbbox($textSize, 0, $font, $line); $lineWidth = $dimensions[4] - $dimensions[0]; // get the length of this line if ($align == "r") { //If the align is Right $locX = $x + $width - $lineWidth; } else { //If the align is Center $locX = $x + ($width/2) - ($lineWidth/2); } } else { //if the align is Left $locX = $x; } $locY = $y + ($nr * $lineHeight); //Print the line. imagettftext($img, $textSize, 0, $locX, $locY, $color, $font, $line); } } ?>

« previous php.notes (#213089) next »