Does any of you know how to add random letters aswel as random numbers.
thanks for you help.
<?php
session_start();
$image = imagecreate(, );
$bgcolor = imagecolorallocate($image, , ,);
$textcolor = imagecolorallocate($image, , , );
$code = rand(00, 99);
$_SESSION['code'] = ($code);
imagestring($image, , , , $code, $textcolor);
header ("Content-type: image/png");
imagepng($image);
?>
I always like to do this for random letters and numbers using md5. its not completely random, but it still returns a random string with both numbers and letters.
$i = rand(100000000, 9999999999)
$i = md5($i); //creates a hashed string with 32 characters
$i = str_split($i, 10); //10 is the amount of characters of your string max 32
$i = $i[0];
i have a function for it
function createRandomString() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$randomstring = '' ;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$randomstring = $randomstring . $tmp;
$i++;
}
return $randomstring;
}
echo createRandomString();
You could do something like this.
<?php
$code = substr(md5(microtime()),rand(0,26),10);
$im = imagecreate(100, 30);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 0, 0, $code, $textcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Sources used:
https://stackoverflow.com/a/5438778/1978142
http://www.php.net//manual/en/function.imagestring.php
Related
I want to implement a simple captcha in ci4, I try to search a sample code in google and then copas it. But when run the code get result like this. Please help what wrong in my code ?
My controller is :
private function captcha()
{
$i = 0;
$imgHeight = 80;
$imgWidth = 250;
$randTotal = 7;
$randomDots = 50;
$randomLines = 25;
$font = realpath('./fonts/monofont.ttf');
$random = '';
$captTextColor = "0x142864";
$noiseColor = "0x142864";
$fontSize = $imgHeight * 0.65;
$random = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0,
$randTotal);
$image = #imagecreate($imgWidth, $imgHeight);
$arrTextColor = $this->hexToRGB($captTextColor);
$arrNoiseColor = $this->hexToRGB($noiseColor);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$captTextColor = imagecolorallocate($image, $arrTextColor['red'], $arrTextColor['green'], $arrTextColor['blue']);
$imgNoiseColor = imagecolorallocate($image, $arrNoiseColor['red'], $arrNoiseColor['green'], $arrNoiseColor['blue']);
// Cetak titik acak di latar belakang gambar
for ($i = 0; $i < $randomDots; $i++) {
imagefilledellipse($image, mt_rand(0, $imgWidth), mt_rand(0, $imgHeight), 2, 3, $imgNoiseColor);
}
// Cetak garis acak di latar belakang gambar
for ($i = 0; $i < $randomLines; $i++) {
imageline(
$image,
mt_rand(0, $imgWidth),
mt_rand(0, $imgHeight),
mt_rand(0, $imgWidth),
mt_rand(0, $imgHeight),
$imgNoiseColor
);
}
// Cetak kotak teks dan tambahkan 6 kode huruf captcha
$textBox = imagettfbbox($fontSize, 0, $font, $random);
$x = ($imgWidth - $textBox[4]) / 2;
$y = ($imgHeight - $textBox[5]) / 2;
$_SESSION['captcha'] = $random;
// imagettftext($image, $fontSize, 0, $x, $y, $captTextColor, $font, $random);
imagettftext($image, $fontSize, 0, $x, $y, $captTextColor, $font, $random);
header('Content-Type: image/jpeg');
imagejpeg($image, null, null);
imagedestroy($image);
}
Thank you
Thank you.
Try adding the exit; command after the imagedestroy($image); line
I have this captcha.php file that I load on the page I want the captcha to appear on using jQuery load.
In the captcha.php file I start a new session and I set a variable (theCaptchaCode) which contains the code you see on the image, but when I try to get the session variable with $theCaptchaCode = $_SESSION['theCaptchaCode']. It says
undefined index: theCaptchaCode.
Captcha.php code:
<?php
//Killing previous session
session_start();
session_unset();
session_destroy();
?>
<?php
session_start();
$theCaptchaCode = "";
$thisCaptchaImg = imagecreatetruecolor(200, 50);
$color = imagecolorallocate($thisCaptchaImg, 0, 0, 0);
$dotColor = imagecolorallocate($thisCaptchaImg, 46, 46, 46);
$backgroundColor = imagecolorallocate($thisCaptchaImg, 255, 255, 255);
imagefilledrectangle($thisCaptchaImg, 0, 0, 200, 70, $backgroundColor);
$characters = '123456789QWERTYUIOPASDFGHJKLZXCVBNM';
$length = strlen($characters);
for ($l = 0; $l < 4; $l++)
{
imageline($thisCaptchaImg, 0, rand() % 50, 200, rand() % 50, $color);
}
for ($d = 0; $d < 1500; $d++)
{
imagesetpixel($thisCaptchaImg, rand() % 200, rand() % 50, $dotColor);
}
for ($c = 0; $c < 7; $c++)
{
$selCharacter = $characters[rand(0, $length - 1)];
imagestring($thisCaptchaImg, 5, 5 + ($c * 30), 20, $selCharacter, $color);
$theCaptchaCode .= $selCharacter;
}
imagepng($thisCaptchaImg, 'thisCaptchaImage.png');
$_SESSION['theCaptchaCode'] = $theCaptchaCode;
session_destroy();
?>
At the end of your code remove the session_destroy() method.
Edited:
Use the below code:
<?php session_start();
$theCaptchaCode = "";
$thisCaptchaImg = imagecreatetruecolor(200, 50);
$color = imagecolorallocate($thisCaptchaImg, 0, 0, 0);
$dotColor = imagecolorallocate($thisCaptchaImg, 46, 46, 46);
$backgroundColor = imagecolorallocate($thisCaptchaImg, 255, 255, 255);
imagefilledrectangle($thisCaptchaImg, 0, 0, 200, 70, $backgroundColor);
$characters = '123456789QWERTYUIOPASDFGHJKLZXCVBNM';
$length = strlen($characters);
for($l=0;$l < 4;$l++)
{
imageline($thisCaptchaImg, 0, rand()%50, 200, rand()%50, $color);
}
for($d=0;$d < 1500;$d++)
{
imagesetpixel($thisCaptchaImg, rand()%200, rand()%50, $dotColor);
}
for($c=0;$c < 7;$c++)
{
$selCharacter = $characters[rand(0, $length-1)];
imagestring($thisCaptchaImg, 5, 5+($c*30), 20, $selCharacter, $color);
$theCaptchaCode .= $selCharacter;
}
imagepng($thisCaptchaImg, 'thisCaptchaImage.png');
$_SESSION['theCaptchaCode'] = $theCaptchaCode;
?>
I was hoping to get in touch with someone on a situation that I cannot find the solution to anywhere.
I am trying to create a captcha on my website using php and although I was able to create an image and create the random captcha text.
I am unable to over lay the two. Here is my code:
<?PHP
session_start();
function generateRandomString($length = 10) {
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$len = strlen($letters);
$letter = $letters[rand(0, $len - 1)];
$text_color = imagecolorallocate($image, 0, 0, 0);
$word = "";
for ($i = 0; $i < 6; $i++) {
$letter = $letters[rand(0, $len - 1)];
imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
$word .= $letter;
}
$_SESSION['captcha_string'] = $word;
}
function security_image(){
// $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
//$font = 'content/fonts/comic.ttf';
$width = '110';
$height = '20';
$font_size = $height * 0.75;
// $image = #imagecreate($width, $height) or die('GD not installed');
global $image;
$image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,200,50,$background_color);
$line_color = imagecolorallocate($image, 64,64,64);
for($i=0;$i<10;$i++) {
imageline($image,0,rand()%50,200,rand()%50,$line_color);
}
$pixel_color = imagecolorallocate($image, 0,0,255);
for($i=0;$i<1000;$i++) {
imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
}
//$textbox = imagettfbbox($font_size, 0, $font, $code);
//$textbox = imagettfbbox($font_size, 0, $randomString);
$x = ($width - $textbox[4]) / 2;
$y = ($height - $textbox[5]) / 2;
// imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
imagettftext($image, $font_size, 0, $x, $y, $text_color , $word);
$images = glob("*.png");
foreach ($images as $image_to_delete) {
#unlink($image_to_delete);
}
imagepng($image, "image" . $_SESSION['count'] . ".png");
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
security_image();
?>
I have no idea what I’m doing wrong. I’ve spent over 10 hours on this “display text” issue. I don’t understand and I am desperate for help. I even downloaded working captcha version from other resources that break once I upload it to my server. I have no idea whats going on. At first I thought there was something wrong with my server but the fact that I can even create the pixels, lines image means that it is at least working.
Please help!!!!
UPDATE---------------------------------------------
Thank you for your suggestions. Here is the edited code. I'm still getting the same issue.
<?PHP
session_start();
function security_image(){
global $image;
// $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
$font = 'content/fonts/comic.ttf';
$width = '110';
$height = '20';
$font_size = $height * 0.75;
$image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,200,50,$background_color);
$line_color = imagecolorallocate($image, 64,64,64);
for($i=0;$i<10;$i++) {
imageline($image,0,rand()%50,200,rand()%50,$line_color);
}
$pixel_color = imagecolorallocate($image, 0,0,255);
for($i=0;$i<1000;$i++) {
imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
}
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$len = strlen($letters);
$letter = $letters[rand(0, $len - 1)];
$text_color = imagecolorallocate($image, 0, 0, 0);
$word = "";
for ($i = 0; $i < 6; $i++) {
$letter = $letters[rand(0, $len - 1)];
imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
$word .= $letter;
}
$_SESSION['captcha_string'] = $word;
/*texbox unitinitialized (removed for the sake of just showing the word size doesnt matter)
$x = ($width - $textbox[4]) / 2;
$y = ($height - $textbox[5]) / 2;
*/
$x = ($width) / 2;
$y = ($height) / 2;
imagettftext($image,$font, 4, $x, $y, $word);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
security_image();?>
i made some suggested changes but the code seems to still do the same thing. Just display the lines and pixels as expected but the text still is missing... ?
There are some several "errors" in your functions, let's fix them:
In generateRandomString()
generateRandomString($length = 10)
$lenght is not used its scope.
$text_color = imagecolorallocate($image, 0, 0, 0);
$image is uninitialized
In security_image() function:
$textbox is uninitialized
$text_color and $word is uninitialized.
And Wrong parameter count for imagettftext() You add 7 parameters, and forget the font file parameter.
found the problem. using this:
http://php.net/manual/en/function.imagettftext.php
i was able to see that the font location was incorrect. using the example on that page and editing it to my needs worked.
i have this php program which takes in an angle as a get parameter and prints a segment of a circle with that angle:
<?php
$size = 600;
$img = imagecreatetruecolor($size, $size);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
imagefilledrectangle($img,0,0,$size,$size,$white);
function Vector($palette,$startx,$starty,$angle,$length,$colour){
$angle = deg2rad($angle);
$endx = $startx+cos($angle)*$length;
$endy = $starty-sin($angle)*$length;
return(imageline($palette,$startx,$starty,$endx,$endy,$colour));
}
$ang = 0;
while($ang <= $_GET['angle']){
Vector($img,$size/2,$size/2,$ang,200,$black);
$ang += 1;
}
header("Content-type: image/png");
imagepng($img);
?>
The function vector basically draws a line with the given parameters. So i loop through a number of times and then each time i loop through i increase the angle by 1. And then i call the vector function which basically draws a sort of circle segment with the specified angle.
But when i wish to draw another sector of the circle starting at the end point of the previous circle, it overlaps! By the way, here's the code:
<?php
$size = 600;
$img = imagecreatetruecolor($size, $size);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
imagefilledrectangle($img,0,0,$size,$size,$white);
function Vector($palette,$startx,$starty,$angle,$length,$colour){
$angle = deg2rad($angle);
$endx = $startx+cos($angle)*$length;
$endy = $starty-sin($angle)*$length;
return(imageline($palette,$startx,$starty,$endx,$endy,$colour));
}
$int = 0;
while($int <= $_GET['angle']){
Vector($img,$size/2,$size/2,$int,200,$black);
$int += 0.01;
}
$int = 0;
while($int <= $_GET['angle']){
Vector($img,$size/2,$size/2,$int,200,$blue);
$int += 0.01;
}
header("Content-type: image/png");
imagepng($img);
?>
In the above code, i expect to draw a circle sector with an angle and then draw another sector with the same angle but in blue color.
I want the 2nd sector to start where the first sector ended, but it overlaps?
So how do i make it start where the previous one stopped?
You say, that you have a function to draw a vector with a certain angle right? Then you can have a loop 360 times and increase the angle by 1 degree per loop and draw the vector. You'll get a circle.
For a pie chart all you have to do is change colours at certain intervals depending upon your segment's angle...
If you're working on a voting system, here's the FULL source code for a PHP image generator which takes in n number of QueryString arguments and makes a pie chart with all those QueryString arguements and puts in a legend for them:
<?php
$size = 600;
$img = imagecreatetruecolor($size, $size);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
imagefilledrectangle($img,0,0,$size,$size,$white);
function Vector($palette,$startx,$starty,$angle,$length,$colour){
$angle = deg2rad($angle);
$endx = $startx+cos($angle)*$length;
$endy = $starty-sin($angle)*$length;
return(imageline($palette,$startx,$starty,$endx,$endy,$colour));
}
$sum__ = array_sum($_GET);
$items = array();
foreach($_GET as $key => $value){
$items[$key] = ($value/$sum__)*360;
}
$items2 = array();
$cur = 0;
foreach($items as $key => $value){
$cur += $value;
$items2[$key]=$cur;
}
$colors = array();
foreach($items as $key => $value){
while(array_key_exists($key,$colors) == False){
$tempcolor = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
if(in_array($tempcolor,$colors) == False){
$colors[$key] = $tempcolor;
}
}
}
$int = 0;
foreach($items2 as $key => $value){
while($int <= $value){
Vector($img,$size/2,$size/2,$int,200,$colors[$key]);
$int += 0.01;
}
}
$container = 10;
foreach($items2 as $key => $value){
imagefilledrectangle($img, 4, $container, 50, $container+15, $colors[$key]);
imagestring($img,5,4+60,$container,$key,$black);
$container += 20;
}
header("Content-type: image/png");
imagepng($img);
?>
hope that helps...
Just FYI, you only need to change two lines of your original code to do what you want. Delete the second $int = 0; and make a change to the next line so that:
$int = 0;
while($int <= $_GET['angle']){
Vector($img,$size/2,$size/2,$int,200,$blue);
$int += 0.01;
}
Becomes:
//delete '$int = 0;'
while($int <= ($_GET['angle']) * 2){
Vector($img,$size/2,$size/2,$int,200,$blue);
$int += 0.01;
}
It's not a general solution but hopefully will allow you to see what you were originally doing wrong.
I want to create an image that displays all the names from a database. So i got the array with all the names in it, but how can i make 1 image that displays all the names on seperate lines using PHP? And please provide an example. I've tried a lot, but they all didn't work.
http://sandbox.phpcode.eu/g/cfb6a
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
$lines = 1;
for($i = 1;$i < 10; $i++){
imagestring($im, 5, 5, $lines * 12, "Hello world $i!", $textcolor);
$lines++;
}
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
It should look something along the lines of the following:
// $gd = <GD resource>
// $names = array('X', 'Y', 'Z');
$font_height = 6;
$colour = imagecolorallocate($gd, 0, 0, 0);
for($i = 0, $count = count($name); $i < $count; $i++)
{
imagestring($gd, 4, 2, $i * $font_height, $names[$i], $colour);
}