I have a function which takes in a font (ttf or otf file) and generates an image of text in different fonts.
The problem I have is trying to work out how I can make the text fit in the image regardless of the font-size, type of font, and amount of text.
I have tried to make the image rectangle variable so that it contains the text of different fonts without cutting a bit of the text since the image is not long or wide enough.
Here is the function that I currently have, I have tried using the number of characters to determine the width of the image, but in some cases for some fonts and sizes, it still gets cut off.
function generate_image($save_path, $text, $font_path){
$length = strlen($text) * 15;
// Create the image
$im = imagecreatetruecolor($length, 40);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $length, 40, $white);
$font = $font_path;
imagettftext($im, 30, 0, 0, 25, $black, $font, $text);
if(imagepng($im, $save_path)){
$status = true;
}else{
$status = false;
}
imagedestroy($im);
return $status;
}
Thank you all for any help
imagettfbbox()
Related
I am trying to print unicode characters in image using GD library
I have following code
$text = "கைவிடப்பட்ட";
$font = "arial.ttf";
$im = imagecreatetruecolor(2500, 500);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 2500, 500, $white);
imagettftext($im, 16, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
Original text here is கைவிடப்பட்ட , but it prints as following , you can see that first letter is replaced.
Edit : I have tried 5 different fonts All results in same(online font preview is showing characters correctly) . whereas when I echo the string via php(from database) to browser, output is exactly same(கைவிடப்பட்ட), but why imagettftext is switching characters..
I am not sure about GD library and environment but I guess the issue is due to rendering. கை = combination of க + ை and the environment doesn't support the rendering for unicode while displaying. so either check any rendering solution or use non unicode fonts or do below workaround to tackle it in php.
$text = "கைவிடப்பட்ட";
$imgtext = tamilrender($text);
$font = "arial.ttf";
$im = imagecreatetruecolor(2500, 500);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 2500, 500, $white);
imagettftext($im, 16, 0, 10, 20, $black, $font, $imgtext);
imagepng($im);
imagedestroy($im);
function tamilrender($input)
{$input= preg_replace("/([க-ஹ])([ைேெ])/u", "$2$1", $input);
$input= preg_replace("/([க-ஹ])ொ/u", "ெ$1ா", $input);
$input= preg_replace("/([க-ஹ])ோ/u", "ே$1ா", $input);
return $input;}
Limitations of Unicode
The Unicode standard policy is to encode only characters, not glyphs. கை, includes a combination character onto க. of Tamil is treated a complex script that requires complex processing, and many software components don't properly handle it.
The best approach would be to find, or create, a php extension library which was written specifically for Tamil.
I'm having issues with text lining up where it should. Every tutorial I can find tells me to use imagettfbox to find the dimensions of the text. I do this, but the text doesn't fit within those dimensions.
See the example below:
<?php
// Create image and colours
$im = imagecreatetruecolor(200, 100);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);
//Set the background to be white
imagefilledrectangle($im, 0, 0, 200, 100, $white);
//font file
$font = "ARIAL.TTF";
//set sample text and fontsize
$text="102";
$size = 25;
//calculate height and width of text1
$bbox = imagettfbbox($size, 0, $font, $text);
$width = $bbox[2] - $bbox[0];
$height = $bbox[5] - $bbox[3];
//draw rectangle that should house the text
imagefilledrectangle($im, 50, 40, 50+$width, 40+$height, $red);
//draw text at same startpoint as rectangle
imagettftext($im, $size, 0, 50, 40, $black, $font, $text);
// Output to browser
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
This should output a red rectangle, containing the text. It doesn't - the text is slightly to the right of where it should be. This only happens with some strings - ones that contain a "1" tend not to work, while some others work perfectly. Here is the output for 2 strings:
Does anyone know what is happening, and what I need to do to make the text fit correctly within the bounding box? I've tested this with multiple fonts, including monospace fonts - the same thing always happens.
I am trying to generate image with some text and I have wrote following code:
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// 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);
// The text to draw
$text = '준수';
// Replace path by your own font path
$font = 'fonts/Walkway Black RevOblique.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Now when I add text with English characters it works but when I used Korean characters is not working and getting this image.
Any idea how to display Korean text?
Thanks
Looks like Walkway Black RevOblique.ttf is not Korean font. Try to download and use Korean one.
i want to add a white box around some text i add to an image via GD-Lib.
but i don't know how to do this best.
Here is my current code:
<?php
$textImg = imagecreatefromjpeg($tempImage);
$black = imagecolorallocate($textImg, 0, 0, 0);
$font = 'lib/verdana.ttf';
// Add the text
imagettftext($textImg, 20, 0, imagesx($textImg)*$textData['x']/100, imagesy($textImg)*$textData['y']/100, $black, $font, $textData['text']);
imagejpeg($textImg,$tempImage,$jpegQuality);
?>
I hope you can help me out.
You can use imagettfbbox() to get the coordinates of the bounding box by passing the same settings you use for the text itself (same text, font and size etc).
Once you have these coordinates you can use imagerectangle() to draw a border around the text, or you can use imagefilledrectangle() to draw a solid rectangle. Be sure to call it before you render the text with imagettftext()
A basic example is below but will need some tweaking as most of it is from memory and I suspect the $x and $y calculation could be done better as it probably doesn't work with varying canvas sizes as it is now. However, it demonstrates the principle.
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $black);
// The text to draw
$text = 'Testing';
// Replace path by your own font path
$font = 'verdana.ttf';
// Add the text
$bbox = imagettfbbox(20, 0, $font, $text);
$x = $bbox[1] + (imagesx($im) / 2) - ($bbox[4]);
$y = $bbox[3] + (imagesy($im) / 2) - ($bbox[5]);
imagerectangle($im, 0, 0, $x, $y, $white);
imagettftext($im, 20, 0, 0, 20, $white, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im);
imagedestroy($im);
What is the best way to display underlined text and output the result as image with GD or any other library?
You can try using the Unicode underline combining character U+0332.
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = "̲U̲d̲e̲r̲l̲i̲n̲e";
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
There are lots of FREE PHP CAPTCHA out there that come with a lot of customization, download one and see what exactly happens behind the scene. Also have a look at this link
HTH
I am using this...
$font = imageloadfont($font_file);
$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);
$str_width = strlen($text)*$font_width;
ImageLine($image, $left, $top+$font_height, $left+$str_width, $top+$font_height, $color);