image icon indicating copy to clipboard operation
image copied to clipboard

Text wrapping

Open tpavlek opened this issue 10 years ago • 35 comments

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

tpavlek avatar May 27 '14 20:05 tpavlek

It's not possible at the moment, but would be useful indeed.

olivervogel avatar May 28 '14 10:05 olivervogel

+1

pascalbaljet avatar Dec 17 '15 16:12 pascalbaljet

+1. any updates on this? Or has anyone found any interesting methods to mimic it?

clin407 avatar Mar 31 '16 17:03 clin407

+1 when it will added to the next version?

nurwin avatar Apr 08 '16 10:04 nurwin

+1

primangesta avatar Apr 21 '16 04:04 primangesta

+1

talifhani avatar May 20 '16 13:05 talifhani

+1

vivg avatar Jun 06 '16 00:06 vivg

+1

XaphanBael avatar Jun 16 '16 11:06 XaphanBael

+1

kalenjordan avatar Jan 31 '17 00:01 kalenjordan

+1

trevorgreenleaf avatar Feb 05 '17 11:02 trevorgreenleaf

+1

valkirilov avatar Mar 08 '17 09:03 valkirilov

+1

jayaregalinada avatar Mar 29 '17 18:03 jayaregalinada

+1

bimal125 avatar Jun 01 '17 07:06 bimal125

+1

Anizou avatar Jun 13 '17 13:06 Anizou

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();

bubjavier avatar Aug 20 '17 23:08 bubjavier

http://php.net/manual/en/function.imagettfbbox.php

Dadibom avatar Sep 20 '17 15:09 Dadibom

+1

sngrl avatar Oct 16 '17 17:10 sngrl

+1

garzfaust avatar Oct 31 '17 22:10 garzfaust

+1

tohann avatar Nov 15 '17 04:11 tohann

+1

marysomerville avatar Jan 26 '18 11:01 marysomerville

+1

icgdoug avatar Feb 01 '18 11:02 icgdoug

+1

DouFuJuShi avatar Feb 03 '18 02:02 DouFuJuShi

+1

nnquan avatar Mar 17 '18 00:03 nnquan

+1

hasanAli9t avatar Apr 05 '18 19:04 hasanAli9t

+2

JohnnyWalkerDigital avatar Jul 10 '18 18:07 JohnnyWalkerDigital

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']

tim0991 avatar Aug 11 '18 07:08 tim0991

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');
  });
}

jakeydevs avatar May 15 '19 10:05 jakeydevs

+1

JohnDotOwl avatar Aug 18 '20 01:08 JohnDotOwl

Seriously this issue is opened from 6 years and it hasn't been fixed?!

GioPan04 avatar Dec 03 '20 09:12 GioPan04

+1

fullstackPrincess avatar Dec 18 '20 13:12 fullstackPrincess