php - Getting boxsize of text and create an image text -
i want create text image newsletter textearea. so, have more 1 line. thinking of using imagettfbbox calculate total width , height (of lines), , use imagettftext write in image. problem that, noticed more font-size is, more padding left adding "imagettftext" text. imagettfbbox telling me nothing padding. returing array values [0] , [1] both allways = -1.
thanks!
you gd or imagemagick, post basic example using gd.
<?php // set content-type header('content-type: image/png'); // create image $im = imagecreatetruecolor(400, 30); // create colors $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 399, 29, $white); // text draw $text = 'a simple text string'; // replace path own font path $font = 'tahoma.ttf'; // add shadow text imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); // add text imagettftext($im, 20, 0, 10, 20, $black, $font, $text); // using imagepng() results in clearer text compared imagejpeg() imagepng($im); imagedestroy($im); ?>
now formatting string can use strlen() return lengths of individual strings , propagate function needed. more stream-lined approach consider using imagemagick because supports textalignment , more.
<?php define("left", 1); define("center", 2); define("right", 3); $w = 400; $h = 200; $gradient = new imagick(); $gradient->newpseudoimage($w, $h, "gradient:red-black"); $draw = new imagickdraw(); $draw->setfontsize(12); $draw->setfillcolor(new imagickpixel("#ffffff")); $draw->settextalignment(left); $draw->annotation(150, 30, "hello world1!"); $draw->settextalignment(center); $draw->annotation(150, 50, "hello world2!"); $draw->settextalignment(right); $draw->annotation(150, 70, "hello world3!"); $draw->setfillcolor(new imagickpixel("#0000aa")); $x1 = 150; $x2 = 150; $y1 = 0; $y2 = 200; $draw->rectangle($x1, $y1, $x2, $y2); $gradient->drawimage($draw); $gradient->setimageformat("png"); header("content-type: image/png"); echo $gradient; ?>
i hope you. can elaborate on questions might have.
Comments
Post a Comment