Center/middle text in php image -


i want create image php. have backgroundimage. on it, want write text. text should center , in middle (vertical align). text should break, if comes out of border.

how can that?

if have string:

$text = "this sample text. second sample text."; 

this should result:

hope understand need.


edit:

this made: in center , middle. text makes no break. how can this?

<?php  header("content-type: image/png");  //settings $text = 'this sample text. second sample text.'; $width = 200; $height = 200;  //create image $im = imagecreate($width, $height);  //colors $colorwhite = imagecolorallocate($im, 255, 255, 255); $colorblack = imagecolorallocate($im, 0, 0, 0); $colorgrey = imagecolorallocate($im, 207, 199, 199);  //border imagerectangle($im, 0, 0, $width - 1, $height - 1, $colorgrey);  //fontsize $fontsize = 3; $font_width = imagefontwidth($fontsize); $font_height = imagefontheight($fontsize);  //text size $text_width = $font_width * strlen($text); $text_height = $font_height;  //align: center  $position_center = ceil(($width - $text_width) / 2);  //valign: middle $position_middle = ceil(($height - $text_height) / 2);  imagestring($im, $fontsize, $position_center, $position_middle, $text, $colorblack); imagepng($im);  ?> 

this result of code:

result

i made it:

<?php  header("content-type: image/png");  //settings $fontsize = 35; $backgroundpath = "./back.png"; $font = "./myriadwebpro-bold.ttf"; $text = "this sample text. second sample text."; $padding = 50; //from edges  //create image $im = imagecreatefrompng($backgroundpath); $imagesize = getimagesize($backgroundpath); $width = $imagesize[0]; $height = $imagesize[1];  //get textrows $textrows = gettextrowsfromtext($fontsize, $font, $text, $width - ($padding * 2));  //colors $colorwhite = imagecolorallocate($im, 255, 255, 255); $colorblack = imagecolorallocate($im, 0, 0, 0); $colorgrey = imagecolorallocate($im, 130, 130, 130);  //border //imagerectangle($im, 0, 0, $width - 1, $height - 1, $colorgrey);  for($i = 0; $i < count($textrows); $i++) {     //text size     $line_box = imagettfbbox ($fontsize, 0, $font, $textrows[$i]);     $text_width = gettextwidth($fontsize, $font, $textrows[$i]);      $text_height = getmaxtextheight($fontsize, $font, $textrows) * 3;      //align: center      $position_center = ceil(($width - $text_width) / 2);      //valign: middle     $test = (count($textrows) - $i) - ceil(count($textrows) / 2);     $position_middle = ceil(($height - ($text_height * $test)) / 2);      imagettfstroketext($im, $fontsize, 0, $position_center, $position_middle, $colorwhite, $colorgrey, $font, $textrows[$i], 2); } imagepng($im);  function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {      for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)         for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)             $bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);     return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text); }  function gettextwidth($fontsize, $font, $text) {     $line_box = imagettfbbox ($fontsize, 0, $font, $text);     return ceil($line_box[0]+$line_box[2]);  }  function gettextheight($fontsize, $font, $text) {     $line_box = imagettfbbox ($fontsize, 0, $font, $text);     return ceil($line_box[1]-$line_box[7]);  }  function getmaxtextheight($fontsize, $font, $textarray) {     $maxheight = 0;     for($i = 0; $i < count($textarray); $i++)     {                $height = gettextheight($fontsize, $font, $textarray[$i]);         if($height > $maxheight)             $maxheight = $height;      }      return $maxheight; }  function gettextrowsfromtext($fontsize, $font, $text, $maxwidth) {        $text = str_replace("\n", "\n ", $text); $text = str_replace("\\n", "\n ", $text);     $words = explode(" ", $text);      $rows = array();     $tmprow = "";     for($i = 0; $i < count($words); $i++)     {          //last word         if($i == count($words) -1)         {             $rows[] = $tmprow.$words[$i];             break;;         }           if(gettextwidth($fontsize, $font, $tmprow.$words[$i]) > $maxwidth) //break         {             $rows[] = $tmprow;             $tmprow = "";         }         else if(stringendswith($tmprow, "\n ")) //break in text         {             $tmprow = str_replace("\n ", "", $tmprow);             $rows[] = $tmprow;             $tmprow = "";         }          //add new word row            $tmprow .= $words[$i]." ";      }      return $rows; }  function stringendswith($haystack, $needle) {     return $needle === "" || substr($haystack, -strlen($needle)) === $needle; }  ?> 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -