The generate.php is working, however if I try to retrieve the image on my index.php using <img src="generate.php"/> I get a blank screen.
generate.php
<?php
header('Content-type: image/png');
if(isset($_GET['email'])){
$email = $_GET['email'];
}else{
$email = 'No email specified';
}
$email_length = strlen($email);
$font_size = 4;
$image_height = ImageFontHeight($font_size);
$image_width = ImageFontWidth($font_size) * $email_length;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$font_color = imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font_size, 0, 0, $email, $font_color);
imagepng($image);
?>
index.php
Name:<br>
Dave
<br><br>
Email: <br>
<img src="generate.php"/>
Related
hi i have form that have captcha and i want number of captcha show arabic or persian but when i change font of code this is not work. how can i make that?
<?php
session_start();
$string = '';
for ($i = 1; $i < 8; $i++) {
// this numbers refer to numbers of the ascii table (lower case)
$string .= rand(0,6);
}
$_SESSION['random_code'] = $string;
$dir = 'style/font/';
$image = imagecreatetruecolor(170, 60);
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, $dir."Yekan.ttf",
$_SESSION['random_code']);
header("Content-type: image/png");
imagepng($image);
?>
I need help in my PHP image code. I'm trying to make Captcha, but all I get is broken image icon ?
This is my code:
<?php
header('Content-type: image/jpeg');
$text = "1111";
$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);
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpg($image);
?>
"font.ttf" is next to my php file, and names are matching
Use
imagejpeg($image);
Instead of
imagejpg($image);
As imagejpg() doesn't exist.
When debugging you should comment out the header to see the errors.
When I check the index page it does not show anything but a broken image.
These are the files I am using.
index.php
<?php
session_start();
$_SESSION['secure'] = rand(1000, 9999);
?>
<img src="./generate.php" />
generate.php
<?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);
imagettftext($image, $fon_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);
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);
How to make Like This Image?
I just want to learn how do i get output like this using GD library,
Thanks for your help guys..
Here is your code, you could easily find it on google tho.
<?php
header ("Content-type: image/png");
$string = "your text"; // Change this text
$font = 4; // try changing this as well
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/path/to/yourimagefile");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
?>
Here's an example:
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
imagepng($image); // outputting the image
For more see imagettftext().
EDIT: An example of using multiple imagettftext():
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
$text = "Text 2";
imagettftext($image, 15, 0, 25, 45, $color, $font, $code); // adding the text
$text = "Text 3";
imagettftext($image, 15, 0, 30, 50, $color, $font, $code); // adding the text
imagepng($image); // outputting the image