imagettftext not showing numbers only letters - php

this is my code:
header('Content-Type: image/jpeg');
$image_width = 400;
$image_height = 30;
$im = imagecreatetruecolor($image_width, $image_height);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = utf8_encode("test 123 abc");
$font = 'myfont.ttf';
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagejpeg($im);
imagedestroy($im);
can anyone help me find where i am wrong.

Related

Making captcha image in PHP

I'm trying to use the GD graphics library to make this captcha image, and the box shows up how Id like it to, but the text isn't being displayed over the image at all. I can't figure out why it's not showing up.
<?php
session_start();
putenv('GDFONTPATH=' . realpath('.') );
$font = 'Molle';
header('Content-Type: image/png');
$im = imagecreatetruecolor(260, 40);
$white = imagecolorallocate($im, 255, 255,255);
$grey = imagecolorallocate($im, 215, 215, 215);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 3, 3, 255, 34, $grey);
$text = 'Testing';
$_SESSION["captcha"] = $text;
imagettftext( $im, 20, 0, 16, 26, $white, $font, $text );
imagettftext( $im, 20, 0, 15, 25, $black, $font, $text );
imagepng($im);
imagedestroy($im);
?>

php - fit text on text-to-image

I have this 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 = 'Testing... a very long text';
// Replace path by your own font path
$font = 'arial.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);
?>
the problem here is, when I have a very long text, the other won't show.
I've searched that it can be done by
imagettfbbox()
but I don't know how to apply it here.
Any help?
try this code:
<?php
// Set the content-type
header('Content-type: image/png');
$img_width = 400;
$img_height = 30;
// Create the image
$im = imagecreatetruecolor($img_width, $img_height);
// 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, $img_width, $img_height, $white);
// The text to draw
$text = 'Testing... a very long text bla.. ';
// Replace path by your own font path
$font = 'arial.ttf';
$fontSize = $img_width / strlen($text) * 1.8;
// Add some shadow to the text
imagettftext($im, $fontSize, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, $fontSize, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
New update code :
<?php
// Set the content-type
header('Content-type: image/png');
$img_width = 400;
$img_height = 30;
$font = 'arial.ttf';
// Create the image
$text = 'Testing... a very long text.. Testing... a very long text.. Testing... a very long text.. Testing... a very long text..';
$curTextLen = strlen($text);
$limit = 35;
$totalLine = ceil($curTextLen / $limit);
$img_height = $img_height * $totalLine;
$im = imagecreatetruecolor($img_width, $img_height);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $img_width, $img_height, $white);
for($i = 1; $i <= $totalLine; $i++){
$y = $i * 27;
$textN = substr($text,($limit * ($i-1)),$limit);
imagettftext($im, 20, 0, 11, $y, $grey, $font, $textN);
// Add the text
imagettftext($im, 20, 0, 10, $y, $black, $font, $textN);
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

imagettftext function not working in php

I have the following code for my captcha. The lines in the image are generated but the 4 digit number is not displayed. I assume there is some problem in
imagettftext($image,$font_size,0,15,30,$text_color,'font.TTF',$text);
function of code. I tried setting the path to the font file using $_server['DOCUMENT_ROOT']; yet didn't work.
here's the code:
<?php
session_start();
header('Content-type: image/jpeg');
$text= $_SESSION['secure'];
$font_size =30;
$image_width= 110;
$image_height=40;
$image=imagecreate($image_width,$image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
for($x=1; $x<=30; $x++){
$x1 = rand(1,100);
$x2 = rand(1,100);
$y1 = rand(1,100);
$y2 = rand(1,100);
imageline($image, $x1, $x2, $y1, $y2, $text_color);
}
imagettftext($image,$font_size,0,15,30,$text_color,'font.TTF',$text);
imagejpeg($image);
?>
Below code Perfectly working in my pc
Remember font path should be correct.
output-
<?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 = 'Testing...';
// Replace path by your own font path
$font = 'arial.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);

PHP GD imagecolorallocatealpha only makes grey text

Is there any reason why imagecolorallocatealpha() would only be making the text grey?
<?php
header('Content-Type: image/png');
function checkImg($imgname) {
$im = #imagecreatefrompng($imgname);
if(!$im) {
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
$hr = 48;
$tOne = "VALID FOR";
$tTwo = $hr." HOURS";
$img = checkImg('img.png');
$font = 'helr67w.ttf';
$size = 9;
$red = imagecolorallocatealpha($img, 255, 0, 0, 75);
imagettftext($img, $size, 0, 225, 132, $red, $font, $tOne);
imagettftext($img, $size, 0, 225, 144, $red, $font, $tTwo);
imagepng($img);
imagedestroy($img);
?>
In your code you don't set the image to support an alpha channel. I can imagine that is causing the issue:
function checkImg($imgname) {
$im = #imagecreatefrompng($imgname);
if(!$im) {
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
// Turn off alpha blending and set alpha flag
imagealphablending($im, true);
imagesavealpha($im, true);
return $im;
}
See imagesavealpha PHP Manual and imagealphablending PHP Manual

String to image only produces a black background

I'm really having a problem finding out how to fix this. I cannot seem to change the background from black. How is it possible?
$string = "foo";
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
$im = #imagecreatetruecolor ($width,$height);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 0, 0, $string, $textcolor);
imagegif($im, 'somefile.gif', 8);
imagedestroy($im);
Use:
imagefill($im, 0, 0, $bg);
Incorporating user279470's answer:
$string = "foo";
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
$im = #imagecreatetruecolor ($width,$height);
$bg = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bg);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 0, 0, $string, $textcolor);
imagepng($im, 'somefile.png', 8);
imagedestroy($im);

Categories