image
image copied to clipboard
Text wrapping
I have an image and I would like to have a paragraph of text appearing within a certain bounded box on the image. Is there any way with the current implementation to define a bounding box and have the text wrap/resize within it to fit inside?
If not, I think this would be a useful feature to have.
Thanks
It's not possible at the moment, but would be useful indeed.
+1
+1. any updates on this? Or has anyone found any interesting methods to mimic it?
+1 when it will added to the next version?
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
here's what I did for the meantime:
Inside my Laravel controller:
<?php
$width = 600;
$height = 300;
$center_x = $width / 2;
$center_y = $height / 2;
$max_len = 36;
$font_size = 30;
$font_height = 20;
$text = 'The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog?';
$lines = explode("\n", wordwrap($text, $max_len)
$y = $center_y - ((count($lines) - 1) * $font_height);
$img = \Image::canvas($width, $height, '#777');
foreach ($lines as $line)
{
$img->text($line, $center_x, $y, function($font) use ($font_size){
$font->file(base_path('path/to/my/font.ttf'));
$font->size($font_size);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('center');
});
$y += $font_height * 2;
}
return $img->response();
http://php.net/manual/en/function.imagettfbbox.php
+1
+1
+1
+1
+1
+1
+1
+1
+2
function getTextWith($fontSize, $text)
{
$im = new Imagick();
$draw = new ImagickDraw();
$draw->setFont($this->fontPath);
$draw->setFontSize($fontSize);
$info = $im->queryFontMetrics($draw, $text);
return $info['textWidth'];
}
get height by $info['textHeight']
Had this issue this morning, so wrote a function to help with this:
/**
* Generate an array of "lines" our text should
* use
*
* @param String $text
* @param Number $width
* @param Number $size (Font size)
* @param String $font (Font path)
* @return Array [text]
*/
public function generateText($text, $width, $size, $path)
{
//-- Helpers
$line = [];
$lines = [];
//-- Looop through words
foreach(explode(" ", $text) AS $word)
{
//-- Add to line
$line[] = $word;
//-- Create new text query
$im = new \Imagick();
$draw = new \ImagickDraw();
$draw->setFont($path);
$draw->setFontSize($size);
$info = $im->queryFontMetrics($draw, implode(" ", $line));
//-- Check within bounds
if ($info['textWidth'] >= $width)
{
//-- We have gone to far!
array_pop($line);
$lines[] = implode(" ", $line);
//-- Start new line
unset($line);
$line[] = $word;
}
}
//-- We are at the end of the string
$lines[] = implode(" ", $line);
return $lines;
}
I then use this by doing the following on my image:
$textLines = $this->generateText(
'This is a image that has quite a large name so should overlap',
700,
$fontSize,
$fontPath
);
foreach($textLines AS $index => $text)
{
$y = (($index * $fontSize) + $padding);
$image->text($text, $padding, $y, function($font) use ($fontPath, $fontSize) {
$font->file($fontPath);
$font->size($fontSize);
$font->color('#fdf6e3');
$font->align('left');
$font->valign('top');
});
}
+1
Seriously this issue is opened from 6 years and it hasn't been fixed?!
+1