$max_width && !$first_word) { $lines[] = $current_line; $line_widths[] = $last_width ?: $line_width; $current_line = $item; } else { $current_line .= ($first_word ? '' : ' ') . $item; } if ($i == count($words) - 1) { $lines[] = $current_line; $line_widths[] = $line_width; } $last_width = $line_width; $first_word = false; } if ($current_line) { $current_line = $item; } } // vertical align $top_offset = 0; if ($valign == self::ALIGN_CENTER) { $top_offset = ($max_height - $largest_line_height * count($lines)) / 2; } elseif ($valign == self::ALIGN_BOTTOM) { $top_offset = $max_height - $largest_line_height * count($lines); } $top += $largest_line_height + $top_offset; $i = 0; foreach ($lines as $line) { // horizontal align $left_offset = 0; if ($align == self::ALIGN_CENTER) { $left_offset = ($max_width - $line_widths[$i]) / 2; } elseif ($align == self::ALIGN_RIGHT) { $left_offset = ($max_width - $line_widths[$i]); } imagettftext($image, $size, $angle, $left + $left_offset, $top + ($largest_line_height * $i), $color, $font, $line); $i++; } return $largest_line_height * count($lines); } function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0) { if ($spacing == 0) { imagettftext($image, $size, $angle, $x, $y, $color, $font, $text); } else { $temp_x = $x; for ($i = 0; $i < mb_strlen($text); $i++) { $bbox = imagettftext($image, $size, $angle, $temp_x, $y, $color, $font, $text[$i]); $temp_x += $spacing + ($bbox[2] - $bbox[0]); } } } }