PHP code:
<?php
ob_start();
session_start();
$_SESSION['secure'];
header('content-type:image/png');
$text="hello";
$font_size=25;
$width=200;
$height=200;
$image=imagecreate($width, $height);
imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
for($x=1; $x <= 30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $black);
}
imagettftext($image, $font_size, 0, 15, 30, $black, 'consolas.ttf', $text);
$imgSrc="out.png";
imagejpeg($image, $imgSrc);
?>
When the image is generated, only lines are drawn but text("hello") is not drawn on it.
The font is also in the same directory, I have also kept it in another directory and added the complete path no luck.
I'm debugging this issue and found that font file is not accessible.
I have checked by turning on debugging as below:
error_reporting(E_ALL);
ini_set("display_errors", 1);
And commented lines below.
header('content-type:image/png');
imagepng($image);
Got error:
Warning: imagettftext(): Could not find/open font in
/var/www/html/index.php on line 24
Soultion:
Path to font file should be realpath on server. So the code should be:
imagettftext($image, $font_size, 0, 15, 30, $black, realpath('consolas.ttf'), $text);
Also change in render image without giving $imgSrc and changing it to
imagepng($image);
instead of
$imgSrc="out.png";
imagejpeg($image, $imgSrc);
Finally complete code will look like:
ob_start();
session_start();
$_SESSION['secure'];
header('content-type:image/png');
$text = "hello";
$font_size = 25;
$width = 200;
$height = 200;
$image = imagecreate($width, $height);
imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
for ($x = 1; $x <= 30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $black);
}
imagettftext($image, $font_size, 0, 15, 30, $black, realpath('Consolas.ttf'), $text);
imagepng($image);
Related
using the code below to ceate image used in captch, it works fine in localhost but when i upload to server no image is rendered.
<?php
require_once 'core/init.php';
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 120;
$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<=299; $x++) {
$x1 = rand(1, 999);
$y1 = rand(1, 999);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text_color);
}
imagettftext($image, $font_size, 0, 15, 33, $text_color, 'fonts/font.TTf', $text);
imagejpeg($image);
This question already has answers here:
imagejpeg(); not generating image on host server , working fine on localhost
(2 answers)
Closed 9 years ago.
Ok, here is the problem. This code works on localhost, but not on a live site
<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 120;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
$line_color = imagecolorallocate($image, 0, 0, 0);
for($x=1; $x<=30; $x++){
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $line_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font2.ttf', $text);
imagejpeg($image);
?>
The localhost and the webhosting are both running the same php version
Take a look at this picture before you post anything, please!:
http://s2.postimg.org/etamdsk95/help_me.png
try to look at error.log (APACHE), it could also be
possible that there is a session problem, you could
try to hide all errors by using "error_reporting(0);
try this:
<?php
#session_start();
error_reporting(0);
#header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 120;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
$line_color = imagecolorallocate($image, 0, 0, 0);
for ($x=1; $x<=30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $line_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font2.ttf', $text);
imagejpeg($image);
?>
but the best, would be to fix the error, so look # the error.log
http://php.net/manual/en/function.error-reporting.php
if you got any error message, you could tell us - so we will try to help you :)
there are some error at your code, but it's not the PHPGD lib error, please notice code below:
<?php
//session_start();
header('Content-Type: image/jpeg');
//$_SESSION['secure']; disabled, this will lead to notice: undefined index
$text = "he123";
$font_size = 30;
$image_width = 120;
$image_height = 40;
$image = imagecreate($image_width, $image_height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255); //add background color
$text_color = imagecolorallocate($image, 255, 0, 0); //red for the text
$line_color = imagecolorallocate($image, 0,255,0); // green for the line
for($x=1; $x<=30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $line_color);
}
// add ./ to the font name
imagettftext($image, $font_size, 0, 15, 30, $text_color, "./font2.ttf", $text);
imagejpeg($image);
imagedestroy($image); // remember to clear the chace :p
?>
here's the output
I used the following code to create Captcha to my form. Captcha creating well. Now I want to change font-size, and Font character spaces. I don't know how to change in the following code.
<?php
session_start();
$code= substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 6);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(150, 35);
$bg = imagecolorallocate($im, 255, 255, 255);
$fg = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 5, 5, $bg);
imagestring($im, 5, 8, 8, $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
You can use the code below to implement a simple captcha using gd library of PHP. As you are beginner, here is a sample code for quick testing, it covers font-sizing also:
<?php
session_start();
header('Content-type: image/jpeg');
$text = rand(1000, 9999);
$font_size = 30;
$image_width = 200;
$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<=40; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'FREESCPT.ttf', $text);
imagejpeg($image);
?>
In imagettftext function:
imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text);
$image is the $imagecreate function
$font_size is the size of font you want.
$angle is the angle of the fonts tilted
$x and $y are coordinates.
$text_color is the imagecolorallocate function
$font-family is the family of font you want to use
$text is the text or random text to be displayed
Here is the good tutorial on how to build captcha in php -> link
you can change font using imagestring function
This Script work on localhost (Windows), but not on the Server (Debian):
I have uploaded the files 1:1. Please help me, Thanks :)
<?php
function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr, $user)
{
// Load image
$img = imagecreatefrompng($img_src);
// Transparent red
$red = imagecolorallocatealpha($img, 0, 0, 0, $tr);
// Draw a white rectangle
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
$white = ImageColorAllocate ($img, 255, 255, 255);
ImageTTFText ($img, 35, 0, 15, 350, $white, "Big.ttf",
$user);
// Don't forget to output a correct header
header('Content-Type: image/jpg');
// Save the image (overwrite)
imagepng($img);
imagedestroy($img);
}
$img_src = 'playerimages/streamnew.png';
$box = imagettfbbox ( 35 , 0 , "Big.ttf" , $_GET['username'] );
$width = abs($box[4] - $box[0]);
$height = abs($box[5] - $box[1]);
$x1= 10;
$y1= 310;
$x2 = 25+$width;
$y2=360;
red_rectangle($img_src,$x1,$y1,$x2,$y2,50, $_GET['username']);
?>
THE ANSWER: $_SERVER['DOCUMENT_ROOT']."/big.ttf"
I tried to change the $font_size but it seems it is not working. It stays the same no matter what. Even if I altered the $font_size up to 130. It stays the same:
<?php
header('Content-type: image/png');
$rand_num = rand(1000, 9999);
$font_size = 130;
$image_width = 110;
$image_height = 20;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$black_color = imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font_size, 0, 0, $rand_num, $black_color);
for($x=0; $x <= 30; $x++){
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $black_color);
}
imagepng($image);
?>
According to PHP Manual, The font parameter
Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where
higher numbers corresponding to larger fonts) or any of your own font
identifiers registered with imageloadfont().
So you have to try 1 2 3 4 5 and not 130.
For built in fonts, font size can be from 1,2,3,4 and 5. Please refer to http://php.net/manual/en/function.imagestring.php
You can change the font size using imagettftext function.
imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text);
$image is the $imagecreate function
$font_size is the size of font you want.
$angle is the angle of the fonts tilted
$x and $y are coordinates.
$text_color is the imagecolorallocate function
$font-family is the family of font you want to use
$text is the text or random text to be displayed