Only integer should be added from random in php - 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.

Related

PHP - imagettftext not working

I am trying to generate images containing data from my database, and I want to use a custom font for it, so I looked up the imagettftext. I am following its syntax, but it's not printing anything. What am I doing wrong?
When I use imagestring it works fine. But that does not use any custom font.
I have placed my font.tff file in the same folder as my script. Here is my code:
header('Content-Type: image/png');
$image = #imagecreate(400, 110);
$backgrund = imagecolorallocate($image, 70, 130, 180);
$textcolor = imagecolorallocate($image, 255, 255, 255);
$font = 'font.ttf';
// TTF
imagettftext($image, 10, 0, 15, 15, $textcolor, $font, 'Test TTF');
// Normal text
imagestring($image, 2, 15, 30, 'Test Normal Font', $textcolor);
imagepng($image, 'images/test.png');
imagedestroy($image);
Am I missing something? The first line is completely blank. I also tried adjusting the font size (it maybe is different than regular imagestring?) but still nothing. The font is a .ttf font file.

How to get value from combo box and print on an image with php

I want to get the value from a combo box which I have created using html
My html code is:
<html>
<body>
<input type="text" name="name">
<select name="gender" id="gender" class="form-control input-lg" maxlength="12">
<input type="button" name="next1">
<option value="male">male</option>
<option value="female">female</option>
</select>
and to print my combo box text in an image my php code is
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('Desert.jpg');
$white = imagecolorallocate($jpg_image, 73, 41, 236);
$font_path = 'OpenSans-Italic.TTF';
$text= $_GET['name'];
$text2= $_GET['gender'];
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text2);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
As you can see, in my php code I have tried to get the value with the $_GET['gender']; also but that is of no use it only shows a plain image without a text on it. What am I doing wrong? and i have used java script for subbmitting the values.
It looks like you have your variables mixed up.
$text2= $_GET['gender'];
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
You are using $text in the imagettftext but you assigned $_GET['gender'] to $text2. You might try:
$gender= $_GET['gender'];
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $gender);
Keeping the $_GET and variable names the same should help you from getting confused.
If you are using a form to post this then you should most likely be using:
$gender= $_POST['gender'];
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $gender);
but, without your <form> code we don't know how this information is getting submitted.
(You can also drop the maxlength property from your select object)

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.

php gd - multicolor imagettftext

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

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