unable to show text on the image - php

I want to show some text on an image. I am using the following code :
session_start();
$name = $_SESSION['name'];
$nameLength = strlen($name); // gets the length of the name
$randomNumber = rand(0, $nameLength - 1); // generates a random number no longer than the name length
$string = ucfirst(substr($name, $randomNumber, 1)); // gets the substring of one letter based on that random number
$im = imagecreatefromjpeg("love.jpg");
$text = " First Letter of Your partner's name is";
$font = "Font.ttf";
$black = imagecolorallocate($im, 0, 0, 0);
$black1 = imagecolorallocate($im, 255, 0, 0);
imagettftext($im, 32, 0, 380, 430, $black, $font, $text);
imagettftext($im, 52, 0, 630, 530, $black1, $font, $string);
imagejpeg($im, null, 90);
but on localhost this code is showing the text on the image but when I am uploading it to server then it is showing only image, text is not shown on this image ? what may be the problem?
$_SESSION['name'] is value when I login to facebook and save the name of the user into $_SESSION
I have uploaded the above code on the server :
http://rohitashvsinghal.com/fb/login.php

The font must exist in the location specified obviously and the fullpath to the font works best in my experience.
<?php
session_start();
$name = $_SESSION['name'];
$nameLength = strlen($name);
$randomNumber = rand(0, $nameLength - 1);
$string = ucfirst(substr($name, $randomNumber, 1));
$im = imagecreatefromjpeg("love.jpg");
$text = " First Letter of Your partner's name is";
/* Either use the fullpath or the method given below from the manual */
$fontpath=$_SERVER['DOCUMENT_ROOT'].'/path/to/fonts/';
putenv('GDFONTPATH='.realpath( $fontpath ) );
$fontname='arial.ttf';
$font = realpath( $fontpath . DIRECTORY_SEPARATOR . $fontname );
$black = imagecolorallocate($im, 0, 0, 0);
$black1 = imagecolorallocate($im, 255, 0, 0);
imagettftext($im, 32, 0, 380, 430, $black, $font, $text);
imagettftext($im, 52, 0, 630, 530, $black1, $font, $string);
imagejpeg($im, null, 90);
?>
However, the manual states:-
Depending on which version of the GD library PHP is using, when
fontfile does not begin with a leading / then .ttf will be appended to
the filename and the library will attempt to search for that filename
along a library-defined font path.
In many cases where a font resides in the same directory as the script
using it the following trick will alleviate any include problems.
<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>

Related

GD Library imagettftext getting problem in Gujarati Language Text

I have used GD Library to create "Text on Image". I am facing one issue that passing some Gujarati text but getting wrong output as below:
I want like this
and getting:
My code is:
$textBox = imagettfbbox($fontSize, $angle, $font, $txt);
$textWidth = abs(max($textBox[2], $textBox[5]));
$textHeight = abs(max($textBox[5], $textBox[7]));
$x = (imagesx($this->img) - $textWidth)/2;
$y = ((imagesy($this->img) + $textHeight)/$h)-($lines-2)*$textHeight;
$lines = $lines-1;
// Added this line from SO answer.
$txt = mb_convert_encoding($txt, "HTML-ENTITIES", "UTF-8");
$txt = preg_replace('~^(&([a-zA-Z0-9]);)~', htmlentities('${1}'), $txt);
//add some shadow to the text
imagettftext($this->img, $fontSize, $angle, $x + 2, $y + 1, $white, $font, $txt);
//add the text
imagettftext($this->img, $fontSize, $angle, $x, $y, $maroon, $font, $txt);
I have already tried this answer in above code but didn't worked.
Can anyone help me please?
I have created sample example for your given text, I have used other font. You need to match the character mapping to display the correct the text:
<?php
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 = 'xF]EZF+L';
// Replace path by your own font path
$font = __DIR__ . '/LMG-Arun.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);
You can add your font and style.
For the character mapping follow the url: https://fonts.webtoolhub.com/font-n48283-lmg-arun.aspx
Output:

imagettftext do not work

I try this code with imagettftext():
// 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 = 'Testing...';
// 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);
But it does not work as it should, I have GD and FreeType installed, as you can see:
Anyone can point me in right direction?
Assign variable $font to the absolute path of the font file.
// Replace path by your own font path
$font = 'arial.ttf';
Your code is looking for "arial.ttf" that does not exist. Please copy the font file (arial.ttf) into the same directory of your code.
Try to replace your path to font
$font = 'arial.ttf';
on
$font = __DIR__ .'\Arial.ttf';
or use absolute path (it's not correct but working), if you using windows system
$font = 'c:\WINDOWS\Fonts\Arial.ttf';

imagettftext not displaying the image

I'm using the following code in the test.php file to generate an image from a text.
<?php
error_reporting(E_ALL);
// 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 = 'Testing...';
// Replace path by your own font path
$font = '/home/axxxxxxx/public_html/font.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);
?>
Then I'm trying to display the image in the test2.php as follows
<?php
echo "<img src=\"/test.php\" />";
?>
All I get is the default broken image icon. The path to the font file and image url is correct. All file permission are at 777. The servers do have the GD library.
What might I be doing wrong?
This is caused by the missing font. Please copy the font file in the test.php directory and change code:
$font = '/home/axxxxxxx/public_html/font.ttf';
to
$font = 'font.ttf';
Hope it helps.
My issue was a wrong offset. The image was showing nothing, no text, no errors in the source code, just a blank file. The paths were correct. I thought there was an error in the ttf font but turns out it was just wrong positioning.
Here's what helped me to see a bit of the text:
imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);
This shows a bit of text on the top right.
Full working code:
putenv('GDFONTPATH=' . dirname(__FILE__));
$font = 'arial'; // located next to the script
imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);
Found the answer. As Danak suggested, I saved the file as UTF-8 Without BOM using the notepad++. Then Is simply started displaying the image correctly.

Move uploaded file php not working

I want to move a file that has been created using imagettftext and saved as a png. as you can see, in the code below, i used the move_uploaded_file but to no avail. please help.
tq
// Set the content-type
header('Content-Type: image/png');
// Create the image from created image
//$im = imagecreatetruecolor(180, 180);
$im = #imagecreatefromjpeg('poloroid.jpg');
// 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 = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = '/uploaded_files';
// Replace path by your own font path
$font = 'verdana.ttf';
//image file name
$name ="$fbid.png";
// Add some shadow to the text
imagettftext($im, 20, 0, 25, 126, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 25, 125, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
move_uploaded_file($name,"$uploads_dir/$name");
imagedestroy($im);
You are not uploading a file, you are generating one!
imagepng has filename parameter so you can save it to your drive:
$uploads_dir = '/uploaded_files/';
$name = $uploads_dir.$fbid.'.png';
imagepng($im,$name,9);
imagedestroy($im);
try to use rename instead of move_uploaded_file

how to generate multiple texts using php

How can i generate multiple texts using the imagecreatetruecolor() method? I have the following code, but this displays either the first font or the second - not both:
<?php
// Set the content-type
header('Content-type: image/png');
// The text to draw
$text = 'Hello Farooqi';
$x = 0;
$y = 0;
$w = 50;
$h = 50;
$s = 13;
// Create the image
$im = imagecreatetruecolor($w , $s);
imagesavealpha($im, true);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im,0,0,0);
$text_color = imagecolorallocate($im, 200,200, 91);
$blue = imagecolorallocate($im,0,0,180);
$alpha = imagecolorallocatealpha($im, 0, 0, 0, 127);
//imagefilledrectangle($im, 0, 0, 150, 25, $black);
imagefill($im, 0, 0, $alpha);
// Replace path by your own font path
$font = 'Calibri Bold.ttf';
// Add the text
$dimensions = imagettftext($im, $s, 0, $x, $y, $black, $font, $text);
$textWidth = ($dimensions[2]);
$imm = imagecreatetruecolor($w , $s);
imagesavealpha($imm, true);
$bluem = imagecolorallocate($imm,50,50,50);
$alpham = imagecolorallocatealpha($imm, 0, 0, 0, 127);
imagefill($imm, 0, 0, $alpham);
imagettftext($imm, $s, 0, $x+3, $y+3, $bluem, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagepng($imm);
imagedestroy($im);
imagedestroy($imm);
?>
Here above in these last 4 lines only one line appears, and that's the first one. How can I have both lines?
Please help. Thanks in advance.
The imagepng($im); will be called and outputted to your HTML code and as the header is set to an image it will display this image. No matter your imagepng($inm) that comes afterwards.
A better way would be to create two different PHP files. One that does your script and ends in imagepng($im); and another one that ends in imagepng($inm);
And then in your master PHP (header = text/html) file you just mention these 2 files in your image source:
<img src="functions/first_image.php" />
<img src="functions/second_image.php" />

Categories