[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

PHP GD imagestring centering help

Hi,

I've got this very simple code below that should center text on an image.

However it does not align the text correctly horizontally and I can't figure out where I am going wrong.

Have attached code and sample output.
<?php
header("Content-type:image/jpeg");
$string=$_GET['label'];
$img=imagecreatefromjpeg("images/ai_button_orange_marked.jpg");
$col = imagecolorallocate($img, 0, 0, 0);
/* IMAGE FONT CALC */
$font_width=imagefontwidth(3);
$font_height=imagefontheight(3);
$string_width=($font_width*strlen($text));
$image_height=imagesy($img);
$image_width=imagesx($img);
$x_val=(int)(($image_width-$string_width)/2);
$y_val=(int)(($image_height-$font_height)/2);
/* END CALC */
imagestring($img, 3, $x_val, $y_val, $string, $col);
imagejpeg($img);
imagedestroy($img);

?>

Open in new window

output.jpeg
0
TwentyFourSeven
Asked:
TwentyFourSeven
1 Solution
 
ludofulopCommented:
this is wrong:
$string_width=($font_width*strlen($text));

you have text in other variable, change it to

$string_width=($font_width*strlen($string));
0
 
TwentyFourSevenAuthor Commented:
OOPS !
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now