How can I wrap text in an Image using magickwand?

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Locked
vinodkalpaka

How can I wrap text in an Image using magickwand?

Post by vinodkalpaka »

Hi,

I am using a textarea for inputing text and need to embed the text to an image. Here is my code in a simlified form.

$magick_wand=NewMagickWand();
MagickReadImage($magick_wand,'customland.png');


$drawing_wand=NewDrawingWand();
DrawSetFont($drawing_wand,"Fonts/arial.ttf");
DrawSetFontSize($drawing_wand,100);
DrawSetGravity($drawing_wand,MW_CenterGravity);
$pixel_wand=NewPixelWand();
PixelSetColor($pixel_wand,"red");
DrawSetFillColor($drawing_wand,$pixel_wand);

if (MagickAnnotateImage($magick_wand,$drawing_wand,0,0,0,"Red Roses") != 0)
{

MagickEchoImageBlob( $magick_wand );
}
else
{
echo MagickGetExceptionString($magick_wand);
}.


In this case "Red Roses" is my text and it is working fine. But when I am working with long text, it will coming out of the image. I need to wrap up the text enterd. Is there any way to wrap the text or limit the input text in restricable area (give x1,y1,x2 and y2 to output text that is embed in image )? Please help me.

Thanking in advance.

ali

Re: How can I wrap text in an Image using magickwand?

Post by ali »

this thread is a dupe...

viewtopic.php?f=10&t=9444

ericilavia

Re: How can I wrap text in an Image using magickwand?

Post by ericilavia »

Hi Vinod,this is Eric i also looking out for the same solution did u get anything to wrap a text.

vinodkalpaka

Re: How can I wrap text in an Image using magickwand?

Post by vinodkalpaka »

I did not get a direct solution.
I have used the following code. Please note the code is not completed one.

$txtm1 = mb_wordwrap($text,$fontface,$fontsize,$width)
// code for creating magickwand, setfont, fontsize, font alignment etc.

foreach($txtm1 as $textm)
{
if($textm!="")
{
MagickAnnotateImage($magick_wand,$drawing_wand,$message_x_position,$message_y_position,0,$textm);
$message_y_position =$message_y_position+$fontsize;
}
}
// code for show/save the image.



function mb_wordwrap($txt,$font,$size,$width) {
$pointer = 0; // Current character position pointer
$this_line_start = 0; // Starting character position of current line
$this_line_strlen = 1; // How long is the current line
$single_byte_stack = ""; // Variable for storing single byte word
$sbs_line_width = 0; // Pixel width of the Single byte word
$this_is_cr = FALSE; // Check if the character is new line code (ASCII=10)
$result_lines = array(); // Array for storing the return result

while ($pointer < mb_strlen($txt)) {
$this_char = mb_substr($txt,$pointer,1);
if (ord($this_char[0])==10) $this_is_cr = TRUE; // Check if it is a new line
// Check current line width
$tmp_line = mb_substr($txt, $this_line_start, $this_line_strlen);
$tmp_line_bbox = imagettfbbox($size,0,$font,$tmp_line);
$this_line_width = $tmp_line_bbox[2]-$tmp_line_bbox[0];

// Prevent to cut off english word at the end of line
// if this character is a alphanumeric character or open bracket, put it into stack
if (is_alphanumeric($this_char, $single_byte_stack)) $single_byte_stack .= $this_char;
// Check the width of single byte words
if ($single_byte_stack != "") {
$tmp_line_bbox = imagettfbbox($size,0,$font,$single_byte_stack);
$sbs_line_width = $tmp_line_bbox[2]-$tmp_line_bbox[0];
}

if ($this_is_cr || $this_line_width > $width || $sbs_line_width >= $width) {
// If last word is alphanumeric, put it to next line rather then cut it off
if ($single_byte_stack != "" && is_alphanumeric($this_char, $single_byte_stack) && $sbs_line_width < $width) {
$stack_len = mb_strlen($single_byte_stack);
$this_line_strlen = $this_line_strlen - $stack_len + 1;
$pointer = $pointer - $stack_len + 1;
}
// Move the current line to result array and reset all counter
$result_lines[] = mb_substr($txt, $this_line_start, $this_line_strlen-1);
if ($this_is_cr) { $pointer++; $this_is_cr=FALSE; }
if ($sbs_line_width >= $width) $sbs_line_width = 0;
$this_line_start = $pointer;
$this_line_strlen = 1;
$single_byte_stack = "";
} else {
if (!is_alphanumeric($this_char, $single_byte_stack)) {
$single_byte_stack = ""; // Clear stack if met multibyte character and not line end
}
$this_line_strlen++;
$pointer++;
}
}
// Move remained word to result
$result_lines[] = mb_substr($txt, $this_line_start);

return $result_lines;
}

function is_alphanumeric($character, $stack) {
if (
(ord($character)>=48 && ord($character)<=57) ||
(ord($character)>=65 && ord($character)<=91) ||
(ord($character)>=97 && ord($character)<=123) ||
ord($character)==40 ||
ord($character)==60 ||
($stack=="" && (ord($character)==34 || ord($character)==39))
) return TRUE;
else return FALSE;
}


The above code will not work accurately. I am using this function to embed texts enterd by a textarea to a png file.

vinodkalpaka

Re: How can I wrap text in an Image using magickwand?

Post by vinodkalpaka »

Otherwise please try the methods DrawSetClipPath, DrawSetClipRule or DrawSetClipUnits. This may help to set a clipping region to the image. I did not tested these functions.

ncm123

Re: How can I wrap text in an Image using magickwand?

Post by ncm123 »

Try using http://us.php.net/manual/en/function.wordwrap.php for a rudimentary solution.

Locked