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);
}
Related
I'm trying to write a code for making captcha images with GD in PHP. everything works fine but there is a problem that when I output the image with imagejpeg() function in addition to photos, it also shows some content including the strange Unicodes that I don't know what are they.
The file that the image is making in it (captcha.php):
<?php
$captcha_code=strtoupper(substr(sha1(random_bytes(64)),13,6));
$image = imagecreatetruecolor(200, 50);
imageantialias($image, true);
$colors = [];
$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);
$black = imagecolorallocate($image, 30, 30, 30);
$white = imagecolorallocate($image, 230, 230, 230);
$textcolors = [$black, $white];
for($i = 0; $i < 5; $i++) {
$colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}
imagefill($image, 0, 0, $colors[0]);
for($i = 0; $i < 10; $i++) {
imagesetthickness($image, rand(2, 10));
$rect_color = $colors[rand(1, 4)];
imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $rect_color);
}
$size = 24; $x = 5; $y = 30; $angle = 0; $quality = 100;
$color = imagecolorallocate($image, 255, 255, 255);
for($i = 0; $i < 6; $i++) {
$letter_space = 170/6;
$initial = 15;
imagettftext($image, 25, rand(-15, 15), $initial + $i*$letter_space, rand(20, 40), $textcolors[rand(0, 1)], __DIR__."\Acme.ttf", $captcha_code[$i]);
}
imagejpeg($image);
And in this part I try to show the image:
<div class="form-group col-6">
<label>Captcha Code</label>
<img src="scripts/captcha.php" alt="PHP Captcha">
</div>
The captcha image works fine but on the page that I try to show that there is a long content .
The content above the captcha image
I also tried to put header with image content type but another problem is that it causes just a black page with a small blank image in the middle of it although I don't have any errors.
I searched about this problem very much but I didn't find the exact way.
I'll be very happy if you help me :)
I need to iterate through all pixels in an image to replace all but one color with black so that I can define all regions of a particular color. The code below is not working, the image remains unchanged. What am I doing wrong?
header('Content-Type: image/jpeg');
// open an image
$im = imagecreatefromjpeg('sunset.jpg');
$x_val = imagesx($im);
$y_val = imagesy($im);
$black = imagecolorallocate($im, 0, 0, 0);
for ($i = 0; $i < $x_val; $i++) {
$color_index = imagecolorat($im, $i, $i);
$colourarray = imagecolorsforindex($im, $color_index);
if ($colourarray[0] != 170 && $colourarray[1] != 0 && $colourarray[2] != 0) {
$color_index = imagecolorat($im, $i, $i);
imagecolorset($im, $color_index, 0, 0, 0);
} else {
$color_index = imagecolorat($im, $i, $i);
imagecolorset($im, $color_index, 170, 0, 0);
}
}
imagejpeg($im);
imagedestroy($im);
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.
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
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.