Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Created March 7, 2019 17:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Pamblam/84dcf5be94850743c2d19ff5f33afb63 to your computer and use it in GitHub Desktop.
Save Pamblam/84dcf5be94850743c2d19ff5f33afb63 to your computer and use it in GitHub Desktop.
left, right, center and justify text in an image usign native php functions
<?php
$text = "Your Text Here
this is a test of the things and stuff and things";
$font_size = "12";
$color = "#000000";
$font_file = "/Applications/XAMPP/xamppfiles/htdocs/otcb/app/fonts/OldStreetSigns.ttf";
$background = "#FFFFFF";
$wrap_width = "250";
$alpha = 0;
$padding = 5;
// right, left, center, or justified
$align = "justified";
// wrap word first
$text = wrapText($font_size, $font_file, $text, $wrap_width);
// Retrieve bounding box:
$bbox = imagettfbbox($font_size, 0, $font_file, $text);
// Determine image width and height, plus padding:
$baseline = abs($bbox[7]);
$descent = abs($bbox[1]);
$image_width = abs($bbox[0])+abs($bbox[2]) + ($padding*2) + 5;
$image_height = $baseline+$descent + ($padding*2);
// Create image
$image = imagecreatetruecolor($image_width, $image_height);
$rgb = hexToRGB($background);
$bgcolor = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]);
// create a solid background first
imagefill($image, 0, 0, $bgcolor);
imagealphablending($image, false);
imagesavealpha($image, true);
// determine text color
$rgb = hexToRGB($color);
$textcolor = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]);
// Fix starting x and y coordinates for the text:
$x = $bbox[0] + $padding;
$y = $baseline + $padding;
// Add TrueType text to image:
imagealphablending($image, true);
// draw the image
switch($align){
case "center":
$z = imagettftextcenter($image, $font_size, $x, $y, $textcolor, $font_file, $text);
break;
case "justified":
$z = imagettftextjustified($image, $font_size, $x, $y, $textcolor, $font_file, $text);
break;
case "right":
$z = imagettftextright($image, $font_size, $x, $y, $textcolor, $font_file, $text);
break;
case "left":
default:
$z = imagettftext($image, $font_size, 0, $x, $y, $textcolor, $font_file, $text);
}
// output the image
header('Content-Type: image/png');
imagepng($image, null, 9);
function imagettftextjustified($image, $size, $x, $y, $color, $fontfile, $text){
// Get height of single line
$rect = imagettfbbox($size, 0, $fontfile, "Tq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h1 = $maxY - $minY;
// Get height of two lines
$rect = imagettfbbox($size, 0, $fontfile, "Tq\nTq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h2 = $maxY - $minY;
// amount of padding that should be between each line
$vpadding = $h2 - $h1 - $h1;
// calculate the dimensions of the text itself
$frect = imagettfbbox($size, 0, $fontfile, $text);
$minX = min(array($frect[0],$frect[2],$frect[4],$frect[6]));
$maxX = max(array($frect[0],$frect[2],$frect[4],$frect[6]));
$text_width = $maxX - $minX;
// left align any line whose whitespace accounts
// for this much (percent) of the the line.
$max_whitespace_pct = 70;
$threshold = $text_width / 100 * $max_whitespace_pct;
$lines = explode("\n", $text);
foreach($lines as $line){
$rect = imagettfbbox($size, 0, $fontfile, $line);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$line_width = $maxX - $minX;
$line_height = $maxY - $minY;
$words = explode(" ", $line);
$word_width = 0;
$word_data = array();
foreach($words as $word){
$rect = imagettfbbox($size, 0, $fontfile, $word);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$width = $maxX - $minX;
$word_width += $width;
$word_data[] = array(
"width" => $width,
"word" => $word
);
}
$available_space = $text_width - $word_width;
$_x = $x;
if($threshold > $available_space && count($words) > 1){
$total_spaces = count($words) - 1;
$space_size = $available_space / $total_spaces;
foreach($word_data as $word){
imagettftext($image, $size, 0, $_x, $y, $color, $fontfile, $word['word']);
$_x += ($space_size + $word['width']);
}
}else{
imagettftext($image, $size, 0, $_x, $y, $color, $fontfile, $line);
}
$y += ($line_height + $vpadding);
}
return $rect;
}
function imagettftextcenter($image, $size, $x, $y, $color, $fontfile, $text){
// Get height of single line
$rect = imagettfbbox($size, 0, $fontfile, "Tq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h1 = $maxY - $minY;
// Get height of two lines
$rect = imagettfbbox($size, 0, $fontfile, "Tq\nTq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h2 = $maxY - $minY;
// amount of padding that should be between each line
$vpadding = $h2 - $h1 - $h1;
// calculate the dimensions of the text itself
$frect = imagettfbbox($size, 0, $fontfile, $text);
$minX = min(array($frect[0],$frect[2],$frect[4],$frect[6]));
$maxX = max(array($frect[0],$frect[2],$frect[4],$frect[6]));
$text_width = $maxX - $minX;
$text = explode("\n", $text);
foreach($text as $txt){
$rect = imagettfbbox($size, 0, $fontfile, $txt);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$width = $maxX - $minX;
$height = $maxY - $minY;
$_x = $x + (($text_width - $width) / 2);
imagettftext($image, $size, 0, $_x, $y, $color, $fontfile, $txt);
$y += ($height + $vpadding);
}
return $rect;
}
function imagettftextright($image, $size, $x, $y, $color, $fontfile, $text){
// Get height of single line
$rect = imagettfbbox($size, 0, $fontfile, "Tq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h1 = $maxY - $minY;
// Get height of two lines
$rect = imagettfbbox($size, 0, $fontfile, "Tq\nTq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h2 = $maxY - $minY;
// amount of padding that should be between each line
$vpadding = $h2 - $h1 - $h1;
// calculate the dimensions of the text itself
$frect = imagettfbbox($size, 0, $fontfile, $text);
$minX = min(array($frect[0],$frect[2],$frect[4],$frect[6]));
$maxX = max(array($frect[0],$frect[2],$frect[4],$frect[6]));
$text_width = $maxX - $minX;
$text = explode("\n", $text);
foreach($text as $txt){
$rect = imagettfbbox($size, 0, $fontfile, $txt);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$width = $maxX - $minX;
$height = $maxY - $minY;
$_x = ($x + $text_width) - $width;
imagettftext($image, $size, 0, $_x, $y, $color, $fontfile, $txt);
$y += ($height + $vpadding);
}
return $rect;
}
function hexToRGB($hex){
// Remove the # if there is one
$hex = str_replace("#", "", $hex);
// Convert the hex to rgb
if(strlen($hex) == 3){
$r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
$g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
$b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
}else{
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
}
return array($r, $g, $b);
}
function wrapText($fontSize, $fontFace, $string, $width){
$ret = "";
$arr = explode(" ", $string);
foreach($arr as $word){
$testboxWord = imagettfbbox($fontSize, 0, $fontFace, $word);
// huge word larger than $width, we need to cut it internally until it fits the width
$len = strlen($word);
while($testboxWord[2] > $width && $len > 0){
$word = substr($word, 0, $len);
$len--;
$testboxWord = imagettfbbox($fontSize, 0, $fontFace, $word);
}
$teststring = $ret . ' ' . $word;
$testboxString = imagettfbbox($fontSize, 0, $fontFace, $teststring);
if($testboxString[2] > $width){
$ret.=($ret == "" ? "" : "\n") . $word;
}else{
$ret.=($ret == "" ? "" : ' ') . $word;
}
}
return $ret;
}
@AjmalPraveeN
Copy link

❤ Thank you, If you have improvised the code kindly update it if possible..

@EngMohammedKhateb
Copy link

thank you profissional work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment