rjm_wd
rjm_wd

Reputation: 25

Wrap Text on Image PHP

I hope someone can help me, i need to position my TEXT/STRING to the added border at the bottom part of the image and wrap it or format it.

Here is my code:

header("Content-type: image/jpeg");

//$add=$img_link;
$add="https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg";

//$string = $verse_ref;
$string = "There are glimpses of heaven to us in every act, or thought, or word, that raises us above ourselves.";
$fontSize = 20;

$border=10; // Change the value to adjust width
$im=ImageCreateFromJpeg($add);

$width=ImageSx($im);
$height=ImageSy($im);

$img_adj_width=$width+(2*$border);
$img_adj_height=$height+(15*$border);

$newimage=imagecreatetruecolor($img_adj_width,$img_adj_height);

$border_color = imagecolorallocate($newimage, 0, 0, 0);

imagefilledrectangle($newimage,0,0,$img_adj_width,$img_adj_height,$border_color);

imageCopyResized($newimage,$im,$border,$border,0,0,$width,$height,$width,$height);


$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);

$text_width = $font_width * strlen($string);
$position_center = ceil(($width - $text_width) / 2);

$text_height = $font_height;
$position_middle = ceil(($height - $text_height) / 2);

$color = imagecolorallocate($newimage, 255, 255, 255);

imagestring($newimage, $fontSize, $position_center, $position_middle, $string, $color);
ImageJpeg($newimage);

I get this output on my current code: Screenshot

Upvotes: 0

Views: 2212

Answers (2)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

You have already drawn a black 10x10x10x140px border. In the next steps you will have to implement an algorithm similar to the following (pseudo-code):

// Calculate the maximum number of characters per line
max_line_chars := floor(width / character_width)

// Calculate the maximum number of lines that will fit the bottom area
max_lines := floor(bottom_black_area_height / character_height)

// Build an array of wrapped lines
lines = get_array_of_word_wrapped_lines(string, max_line_chars)

// Shrink `lines` to the size of maximum number of lines, if necessary
if length_of lines > max_lines then
  lines := shrink(lines, max_lines)
  height_offset := 0
else
  height_offset := floor(((max_lines - length_of lines)) * character_height) / 2)
endif

for line in lines
  line_width := character_width * length_of line

  x := floor((image_width_with_borders - line_width) / 2)
  y := floor(height_of_image + 2 * border_width + height_offset)

  draw_line(x, y, line)

  height_offset := height_offset + character_height
endfor

Example

$add = 'test.jpg';
$string = 'My people will live in peaceful dwelling places, in secure homes, in undisturbed places of rest. Isaiah 32:17-18';
$string_len = strlen($string);
$split_words = false;

$im = imagecreatefromjpeg($add);
$width  = imagesx($im);
$height = imagesy($im);

$border         = 10;
$width_delta    = 2 * $border;
$height_delta   = 15 * $border;
$img_adj_width  = $width + $width_delta;
$img_adj_height = $height + $height_delta;

$newimage = imagecreatetruecolor($img_adj_width, $img_adj_height);
$border_color = imagecolorallocate($newimage, 0, 0, 0);
imagefilledrectangle($newimage, 0, 0,
  $img_adj_width, $img_adj_height, $border_color);
imagecopyresized($newimage, $im, $border, $border, 0, 0,
  $width, $height, $width, $height);

$color = imagecolorallocate($newimage, 255, 255, 255);

$font = 4;
$font_width  = imagefontwidth($font);
$font_height = imagefontheight($font);

$max_line_chars = floor($width / $font_width);
$max_lines      = floor(($height_delta - 2 * $border) / $font_height);

$lines = explode(PHP_EOL, wordwrap($string, $max_line_chars, PHP_EOL, false));
if (count($lines) > $max_lines) {
  $lines = array_slice($lines, 0, $max_lines);
  $height_offset = 0;
} else {
  $height_offset = floor((($max_lines - count($lines)) * $font_height) / 2);
}

foreach ($lines as $line) {
  $line_width = $font_width * strlen($line);

  $x = floor(($img_adj_width - $line_width) / 2);
  $y = floor($height + $border * 2 + $height_offset);

  imagestring($newimage, $font, $x, $y, $line, $color);

  $height_offset += $font_height;
}

imagejpeg($newimage);

Note, the wordwrap function as well as the standard string functions such as substr do not support Unicode! For Unicode, you will need to implement your own version of wordwrap, and use the multi-byte string functions instead.

Sample output

Sample output

Upvotes: 3

junkfoodjunkie
junkfoodjunkie

Reputation: 3178

Change the $position_middle-line to $position_middle = ceil(($height + ($text_height * 4.5))); and see if that solves the problem.

Upvotes: 0

Related Questions