/    Sign up×
Community /Pin to ProfileBookmark

problem in aligning text on an image

I am using a function that write text on image, the problem is that it sometimes do not center align text and sometimes some characters missed. I use the following logic in the function, i set no of pixels for one character and multiply it with the total no of characters and divide it with the width in pixels of an image, but it is not working accurate at sometime because, i set the character pixels myself if know some way how to get pixels of a character because some characters takes more pixels and some less i.e caps characters. the code is as follows plz suggest a solution

function writeOnImage($filePath, $title, $author, $subtitle, $title_R, $title_G, $title_B, $subtitle_R, $subtitle_G, $subtitle_B, $title_fontsize, $subtitle_fontsize, $author_R, $author_G, $title_B, $author_fontsize,$finalImage)
{
//$existingImage = substr($existingImage, 3);
$im = imagecreatetruecolor(90, 30);
$title_fontColor = imagecolorallocate($im, $title_R, $title_G, $title_?;
$subtitle_fontColor = imagecolorallocate($im, $subtitle_R, $subtitle_G, $subtitle_?;
$author_fontColor = imagecolorallocate($im, $author_R, $author_G, $author_?;
// Create some 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);
// Replace path by your own font path
$font = ‘arial.ttf’;
$im_bg = @imagecreatefromjpeg($filePath);
//imagejpeg($im,”New.jpeg”, 100);
list($width, $height) = getimagesize($filePath);
$pixels_for_one_char = 16;
$available_pixels = (int) $width ;
//echo “ONE: “.pixels_for_one_char;
//echo ” TITLE::”. $title;
$title_length = strlen($title);
$pixels_for_whole_title = $title_length*$pixels_for_one_char;
if($pixels_for_whole_title > $available_pixels)
{
$no_of_lines = ceil($pixels_for_whole_title/$available_pixels);
}
else
{
$no_of_lines =1;
}
//echo “LINES”.$no_of_lines ;
$y = 70;
$from=0;
$chars_in_one_line = round($available_pixels/$pixels_for_one_char);
$limit = $chars_in_one_line;
$white = imagecolorallocate($im_bg, 255, 255, 255);
$str = wordwrap($title,$chars_in_one_line,”#”);
$title_in_lines = explode(“#”,$str);

for($i=0; $i<count($title_in_lines); $i++)
{
//echo “loop”.$title_in_lines[$i];
//$text_to_write = substr($title,$from,$limit);
$total_pixels_for_title = strlen($title_in_lines[$i])*$pixels_for_one_char;
$xPosition = ( $available_pixels – $total_pixels_for_title)/2 ;
//$from = ($i+1)*
$chars_in_one_line;
//$limit = $from + $chars_in_one_line;
imagettftext($im_bg, $title_fontsize, 0, $xPosition, $y, $title_fontColor, $font, $title_in_lines[$i]);
imagettftext($im_bg, $title_fontsize, 0, ($xPosition), $y-1, $title_fontColor, $font, $title_in_lines[$i]);
$y= $y+50;

}
////////////// write subtitle//////////////////////
$y = $y – 18;
$pixels_for_one_char = 8;
$available_pixels = (int) $width ;
//echo “ONE: “.pixels_for_one_char;
//echo ” TITLE::”. $title;
$title_length = strlen($subtitle);
$pixels_for_whole_title = $title_length*$pixels_for_one_char;
if($pixels_for_whole_title > $available_pixels)
{
$no_of_lines = ceil($pixels_for_whole_title/$available_pixels);
}
else
{
$no_of_lines =1;
}
//echo “LINES”.$no_of_lines ;

$chars_in_one_line = round($available_pixels/$pixels_for_one_char);
$limit = $chars_in_one_line;
$white = imagecolorallocate($im_bg, 255, 255, 255);
$str = wordwrap($subtitle,$chars_in_one_line,”#”);
$title_in_lines = explode(“#”,$str);

for($i=0; $i<count($title_in_lines); $i++)
{
//echo “loop”.$title_in_lines[$i];
//$text_to_write = substr($title,$from,$limit);
$total_pixels_for_title = strlen($title_in_lines[$i])*$pixels_for_one_char;
$xPosition = ( $available_pixels – $total_pixels_for_title)/2 ;
//$from = ($i+1)*
$chars_in_one_line;
//$limit = $from + $chars_in_one_line;
imagettftext($im_bg, $subtitle_fontsize, 0, $xPosition, $y, $subtitle_fontColor, $font, $title_in_lines[$i]);
imagettftext($im_bg, $subtitle_fontsize, 0, ($xPosition), $y-1, $subtitle_fontColor, $font, $title_in_lines[$i]);
$y= $y+50;

}

///////////////////////// write author/////////////
//$xPosition = (int) $width / 4;
$pixels_for_one_char = 16;
$available_pixels = (int) $width ;
$author_length = strlen($author);
$total_pixels_for_author = $author_length*$pixels_for_one_char;
$xPosition = ( $available_pixels – $total_pixels_for_author)/2 ;

$yPosition = $height – 10;
$path_parts = pathinfo($filePath);
//echo “<br> Extension”.$path_parts[‘extension’];

imagettftext($im_bg, $author_fontsize, 0, $xPosition, $yPosition-89, $author_fontColor, $font, $author);
imagettftext($im_bg, $author_fontsize, 0, ($xPosition), ($yPosition – 90), $author_fontColor, $font, $author);

//header(“Content-type: image/jpeg”);
imagejpeg($im_bg, $finalImage, 100);
}// end write

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @goodforum spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...