php gd - multicolor imagettftext - php

I'm trying to put multicolor text through imagettftext.
I tried drawing letter by letter but the spacing is horrible.
Here's my code:
$usrname_split = str_split("MarioErmando");
$onecharwidth = imagefontwidth((int)$font)*(12/8);
foreach($usrname_split as $key=>$letter){
if($key == 0){
// first letter
imagettftext($im, 12, 0, $xusrname, 15, $blue, $font, $letter);
$oldletters = "$letter";
}else{
$posarr=imageftbbox(12, 0 ,$font, $oldletters);
$posx = $posarr["2"];
imagefttext($im, 12, 0, $posx, 15, $red, $font, $letter);
$oldletters .= "$letter";
}
}
The output:
Note that the text is dynamic.
Is it possible to achieve multicolor text through imagettftext without horrible spacing?
Regards, MarioErmando.

in order to test, replace your code by this one:
$string="Mario Ermando";
$last_string= substr( $string, 1 );
imagettftext($im, 12, 0, $xusrname, 20, $blue, $font, $string[0]); //first letter "M"
imagefttext($im, 12, 0, 12, 20, $red, $font, $last_string);
i think the problem is when you write every letter separately

Related

Only integer should be added from random in php

I'm working out with php and now I'm facing a problem
My php code is:
<?php
$random= rand (1,5)."Boys";
$random2= rand (1,5)."Girls";
$random3=$random.$random2;
imagettftext($jpg_image, 25, 0, 10,20, $white, $font_path, $random);
imagettftext($jpg_image, 25, 0, 100,200, $white, $font_path, $random2);
imagettftext($jpg_image, 25, 0, 275, 50, $white, $font_path,$random3);
?>
I have not given the full coding but I'll explain you the concept that I'm getting random number and a fixed text with it like boys or girls now In $random3 I only want to show sum of the integer which is randomly given by the $random and $random2 so help me out.
some editing is needed in your php file.
<?php
$random= rand (1,5)."Boys";
$random2= rand (1,5)."Girls";
$random3=($random+$random2);
imagettftext($jpg_image, 25, 0, 10,20, $white, $font_path, $random);
imagettftext($jpg_image, 25, 0, 100,200, $white, $font_path, $random2);
imagettftext($jpg_image, 25, 0, 275, 50, $white, $font_path,$random3);
?>
Now it will work fine. I have corrected it.

How to add text shadow to overlay text using PHP

I want to add an overlay text with text shadow to an image. So far I have been able to achieve this
// top text
imagettftext($im, $font_size+2, 0,
$text1_params['centered_start'],
$font_size+$margin,
$black, $font, $text1_params['text']
);
imagettftext($im, $font_size, 0,
$text1_params['centered_start'],
$font_size+$margin,
$white, $font, $text1_params['text']
);
// bottom text
imagettftext($im, $font_size, 0,
$text2_params['centered_start'] ,
$image_height-$text2_params['height']+$font_size+$margin,
$black, $font, $text2_params['text']
);
imagettftext($im, $font_size, 0,
$text2_params['centered_start']-2,
$image_height-$text2_params['height']+$font_size+$margin-2,
$white, $font, $text2_params['text']
);
This is what the result looks like-
But I want it to look like this(Notice the subtle difference in both the text shadows)-
A hackish way is to render the black text 8 times. First time with an offset to the top left, then top center, then top right, then left, and so on. It may leave an outline that's missing some pixels in a few places but it likely won't be noticeable.

Split a string according to First substring and 2nd another substring

I have a HTML file code in a string that is like given below :
--- few lines of code ---------
<SCRIPT language="javascript" type="text/javascript">
var statList = new Array(
0, "192.168.1.179", "88-53-95-28-2B-AF", 194280, 101141053, 0, 0, 0, 0, 0, 0, 0, 0,
1, "192.168.1.170", "60-C5-47-10-37-FD", 132316, 65860791, 0, 0, 0, 0, 0, 0, 0, 0,
2, "192.168.1.151", "68-5D-43-21-76-95", 9887, 3898646, 0, 0, 0, 0, 0, 0, 0, 0,
3, "192.168.1.134", "6C-3E-6D-8C-FF-62", 26405, 7521875, 0, 0, 0, 0, 0, 0, 0, 0,
4, "192.168.1.124", "04-F7-E4-78-D3-D0", 6189, 1791672, 0, 0, 0, 0, 0, 0, 0, 0,
5, "192.168.1.121", "60-C5-47-10-40-92", 122657, 91113301, 0, 0, 0, 0, 0, 0, 0, 0);
</SCRIPT>
---- Few other lines of code ---------
I want to extract the <script> block containing the Array declaration.
Can I do this with any PHP function?
If you can find a clear delimiter that appears only once at the beginning of your string and another one that appears only once at the end, you can easily use preg_match. Otherwise, you can still use preg_match, but will have a much harder time writing a good RegExp. If you want to extract the IP/MAC data, you may want to start with a RegExp directly.
What about this piece of code ?
$content = file_get_contents('____URL_of_your_html____');
if(preg_match('/(var statList = new Array\(.*?\);)/si',$content,$matches)){
echo $matches[1];
}

Using PHP GD to create image form text with different fonts

I have been using this simple script to generate images from text:
<?php
header('Content-type: image/png');
$color = RgbfromHex($_GET['color']);
$text = urldecode($_GET['text']);
$font = 'arial.ttf';
$im = imagecreatetruecolor(400, 30);
$bg_color = imagecolorallocate($im, 255, 255, 255);
$font_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);
imagefilledrectangle($im, 0, 0, 399, 29, $bg_color);
imagettftext($im, 20, 0, 10, 20, $font_color, $font, $text);
imagepng($im);
imagedestroy($im);
function RgbfromHex($hexValue) {
if(strlen(trim($hexValue))==6) {
return array(
hexdec(substr($hexValue,0,2)), // R
hexdec(substr($hexValue,2,2)), // G
hexdec(substr($hexValue,4,2)) // B
);
}
else return array(0, 0, 0);
}
?>
I call the script with file.php?text=testing script&color=000000
Now I'd like to know how could I generate text with normal and bold fonts mixed in the same image, something like file.php?text=testing <b>script</b>&color=000000
Thanks to dqhendricks for helping me figure this out.
Here's a quick script I wrote, still needs lot of improvements but for the basic functionality it seems to be working fine:
<?php
header('Content-type: image/png');
$color = RgbfromHex($_GET['color']);
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$tmp = $_GET['text'];
$words = explode(" ", $tmp);
$x = array(0,0,10); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD
$addSpace = 0;
foreach($words as $word)
{
if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE
if(stristr($word, "<b>"))
{
$font = 'arialbd.ttf'; // BOLD FONT
$x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, str_replace(array("<b>","</b>"), "", $word));
}
else
{
$font = 'arial.ttf'; // NORMAL FONT
$x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, $word);
}
$addSpace = 1;
}
imagepng($im);
imagedestroy($im);
function RgbfromHex($hexValue) {
if(strlen(trim($hexValue))==6) {
return array(
hexdec(substr($hexValue,0,2)), // R
hexdec(substr($hexValue,2,2)), // G
hexdec(substr($hexValue,4,2)) // B
);
}
else return array(0, 0, 0);
}
?>
Note: This will only work for "bolding" single words separated by spaces and not for bolding part of a word.
Call the script with file.php?text=testing+<b>script</b>&color=000000
you will need to load an arial-bold font file, and do two separate imagettftext() calls, one with each font you want to use. as for parsing the string to find out which parts you would like to be bold, and where you should print each section of text, this sounds like it will become very complicated code. what are you even using this for? there may be better solutions for accomplishing the same thing.
Addition
Use the return value from the imagettftext() function to determine where the next text print should start.
From documentation:
Returns an array with 8 elements representing four points making the bounding box of the text. The order of the points is lower left, lower right, upper right, upper left. The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner when you see the text horizontally. Returns FALSE on error.
Response for:
The parsing wouldn't be a problem, what I don't have clear is how I would find the X position for the second wordr in the example ?text=testing script. I mean how do I know where the first word ends so I can place the second one there with another imagettftext() and a bold font. – Meredith Jan 9 '11 at 2:43
funny how someone took the time to say "see edits for the answer to that" when they coulda just said to explode the string at the spaces, then each word is in an array..
<?PHP
$sentence = "I LOVE giving retarded answers that don't amount to jack!";
$sentence = explode(" ",$sentence );
for($s=0;$s<count($sentence);$s++){
#THERES YOUR INDIVIDUAL WORDS!
$word = $sentence[$s];
}
?>
Your X position would be simply $s for your logic. To get the second word you can do this:
<?PHP
$word1 = $sentence[0];
$word2 = $sentence[1];
$word3 = $sentence[2];
$word4 = $sentence[3];
?>
Yes, i named the $words just for a mental visual effect.
$sentence[1] would be word 2

PHP Accurate Font Width

I am trying to generate a text image.
So I create a new font:
$font = '../fonts/Arial.ttf';
Then I add some background for the text
imagefilledrectangle($image, 0, 0, ?, 12, $green);
And add text
imagettftext($image, 12, 0, 0, 12, $white, $font, $text);
The problem is right now I don't know how to get the width of my font with size 12.
Thanks.
Using ImageTTFBBox()

Categories