Bold text using imagefttext() in PHP - php

I have the below code and I want to bold the output.
Example:
This code;
$info['users_online']);
Outputs the users online count from my database. I want the number that is being output on the image to be bold.
Full code:
<?php
/db info all here/
$getInfo = mysql_query("SELECT users_online FROM server_status");
$info = mysql_fetch_array($getInfo);
{
$image = imagecreatefrompng("banner.png");
$black = imagecolorallocate($image, 1, 1, 1);
imagefttext($image, 6.5, 1, 293, 68, $black, 'volter.ttf', $info['users_online']);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
?>
Don't mind the "/db info all here/" part. I just need the users_online to be bold when it is outputted on the image. Right now, it's not bold.
Thanks.

Use a bold font. For example a bold version of volter.ttf.

You'll want to replace the font file you're currently using (volter.ttf) with one which contains the same font, but in bold weight. If you're unable to do so, you might be able to produce a "pseudo-bold" effect by writing the string several times with a slight X-coordinate offset each time, but frankly at that point it's a lot easier just to change the font to something you can find in bold.

Related

Create dynamic name avatar/icon like Google

I want to create a logo or profile avatar from first letters of name, like Google. Is there any method or service to do it?
I am tried to learn the code about make image with php but it's to hard. One time I found a website about this dynamic image text but I don't find.
Most easy examples you will find online are going to use PHP's imagecreate and imagestring functions, such as this one:
https://phppot.com/php/how-to-convert-text-to-image-using-php/
Here is a quick example-code I've put together based on the above link, that creates an image similar to the Google avatars:
$img = imagecreate(250, 250);
$textbgcolor = imagecolorallocate($img, 52, 152, 219);
$textcolor = imagecolorallocate($img, 255, 255, 255);
$txt = "AB";
$fontfile = "/arial.ttf";
imagettftext($img, 100, 0, 35, 170, $textcolor , $fontfile, $txt);
ob_start();
imagepng($img);
printf('<img src="data:image/png;base64,%s"/ width="100">', base64_encode(ob_get_clean()));
You will need place the arial.ttf fontfile in the same directory as your PHP file for this to work.
However, in most fonts that are aesthetically pleasing, letters do not have the same width. So you will find it difficult to center the text, since you can't use the same X value for the anagram "MM" and "II". I would advise you to use a library that has extended functions like aligning text to the middle, and my bet would be on gd-text.

Unicode font broken when merge images & text using php

I tried to merge text with images, but my text not rendering correctly after merge.
See my screenshot:
The Red mark ( আমি তোমায় ভালবাসি ) text format is correct while I normally show the variable. Font Link: http://www.fonts2u.com/solaimanlipi.font
But while I merge text with image, its broken or not render correctly.
Here is my full code:
<?php
mb_language('uni');
mb_internal_encoding('UTF-8');
$username="";
if(isset($_POST['user'])){
$username = $_POST['user'];
$username = mb_convert_encoding($username, 'HTML-ENTITIES',"UTF-8");
$username = html_entity_decode($username,ENT_NOQUOTES, "ISO-8859-1");
}
$im = imagecreatefromjpeg('image.jpg');
//The numbers are the RGB values of the color you want to use
$black = ImageColorAllocate($im, 255, 255, 255);
//The canvas's (0,0) position is the upper left corner
//So this is how far down and to the right the text should start
$start_x = 50;
$start_y = 80;
$font = 'SolaimanLipi.ttf';
Imagettftext($im, 50, 0, $start_x, $start_y, $black, $font, "$username");
//Creates the jpeg image and sends it to the browser
//100 is the jpeg quality percentage
Imagejpeg($im, 'result.jpg', 100);
echo "<div style='color:red;font-size:60px;'>".$username."</font><br /><img src='result.jpg' height='500' />"
?>
Note 1: SolaimanLipi.ttf is Bangla (Unicode) Font.
Note 2: Bangla (Unicode) font not rendering correctly in tcpdf This Stack & Stack solution will help you to understand my problem & give me a solution.
Please someone help me to get out this problem. Thanks in advance.
[[Previously I asked the same question and no one answer. So I re-post this and deleted the old one. I hope this time someone help me.]]
Bengali is one of a number of scripts that cannot be rendered simply by putting one character after the next. The shapes of characters change and merge depending on their neighbours.
To render this correctly, you need a set of features known as Complex Text Layout. On text rendering systems that do not support CTL, you'll get the equivalent of each letter being rendered alone, so আমি comes out like আ ম ি.
The GD library used by PHP's imagettftext function does not support Complex Text Layout. You'd have to use some higher-level rendering library such as Pango. This is a bit of a pain; if you can achieve your aims by rendering the text over the image as part of HTML/CSS/SVG that would certainly be easier.
(BTW writing $username out to the HTML without doing htmlspecialchars() is an XSS security hole, and writing-then-serving the JPEG file is unsafe as it will break if two requests come in at the same time. You should also serve your page/form as UTF-8 by adding <meta charset="utf-8"/> so you don't have to do the crazy dance with mb_convert_encoding/html_entity_decode).

Include PHP code in html error

Am working with an small php project . in creating an text generating image. took some sample codes and tired.
<?php
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
?>
i made some changes and it works good . when i tried to insert this php file in other html file using it results broken image. how can i solve this why it happens.
Please make sure that you don't have any whitespace before your <?php statment. In your example above there are 4 spaced before the <?php. Note that this also is important for empty lines.
You can't have the same page being both HTML and an image. The way you do what you want is by having the HTML file contain an IMG tag that references this script.
<img src="yourscript.php">
An HTML file cannot have multiple header sections. Once you have rendered content on the page the call to header() will generate an error.

PHP Dynamic Image Has Undesired Border

I'm having some problems with a project I'm doing using Google Maps.
I have the map functionality all figured out, and it works great. I want to add dynamically generated icons to the map, and I've figured out how to do that as well using PHP to dynamically add the text I want to the icon image.
However, the icon is changed beyond the text I want added. The border around the original is made much thicker when there is nothing in the PHP code that should be doing anything like that. The image I've attached to this post shows the two icons. The top icon is the original, and the bottom one has the text added to it by my PHP script. Notice the thicker border.
Here's my PHP code:
<?php
// GETS THE NUMBER TO ADD TO THE ICON
$number = $_GET['number'];
// THE SOURCE OF THE ICON THAT I WANT TO ADD THE TEXT TO
$src = $_GET['src'];
header ("Content-type: image/png");
$font = 4;
$im = imagecreatefrompng($src);
// POSITION THE TEXT TO THE PREFERRED LOCATION
$x = 5 ;
$y = 2;
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $number, $textColor);
imagepng($im);
?>
Keep in mind that all of this is working, except the image is changed beyond just adding the text. The icons look much nicer without the really thick border.
I don't know for sure if this is the case but something like this can be caused if your initial image used transparency at the margin to get an effect of rounded corners. if you want to keep the transparency from the initial image you need to research other functions also. something like this should help:
imagealphablending( $im, false );
imagesavealpha( $im, true );
*right after imagecreatefrompng

What is a good easy class to change text to images

As many of you may already know I have been having issues with another site copying text from my site. The data is sports results that at first are not fully accurate because data is submitted at the event and people have to type in people's hand writing that can be difficult to read as well as other issues related to transferring data. Currently I am stuffing the text with fake text. It is working in part very well. They are still copying the text, but they are removing the fake text manually (which takes them about 2 hours longer hehe). I want to make it even more difficult for them. I would like to change some of the text automatically to images. This will require them to type these portions out. What is a good premade class for this?
I suppose you could use the GD library, that is bundled with PHP, to generate images from text.
Something like this, for instance, might do (adapted from the example given on the page of imagettftext) :
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 = 'Hello, World!';
// Replace path by your own font path
$font = '/usr/share/fonts/truetype/msttcorefonts/verdana.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// 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);
die;
But note that using images instead of text has some drawbacks :
on the server side, it requires more work
on the client side :
it'll take more time to load
it will not necessarily use the same fonts as the rest of the site (possibly depending of the brower's configuration)
fonts will not necessarily have the same size as the rest of the site (same note)
using images might not be good for people who zoom -- especially people who don't see well.
How about adding some form of disclaimer to the text ("this text created by xyz.com, on dd/mm/yyyy, for more up-to-date information visit our site")? Use their use of your text as a promotional tool for your own site. Also, get in touch with them, see if they'd be interested in using your site via some form of API, this reduces their workload and allows you a little more control over their use (at least in theory).
My argument is that if the stuff you're writing up is factual there's no copyright infringement (so far as I can tell), and they may appeal to a different audience than your own, so you may as well use them as a promotional tool to broaden your own audience.

Categories