I would like to do something like http://en.nametests.com/test/what-does-your-name-say-about-you/641/
Although, I couldn't get it to work. What html code do I need? Guide me please.
the code I have right now is this
The problem is it won't give me an option to enter a name.
When the user enters in their name in the text field, it will generate an image with their own name on the image. The text will merge into the image. I don't know a better way to phrase this.
Please any tips, sugguestions, pros, cons?
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('sunset.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'font.TTF';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
First, you have to learn how HTML forms work.
You will need to create HTML page with form like this:
<form method="post" action="your_script.php">
<input type="text" name="your_name" />
<input type="submit" name="submit" value="Generate image!" />
</form>
And then, in your_script.php, you put your prepared code and change line where the text is set to
$text = $_POST['your_name'];
Hope this is sufficient for you - if not, you should probably learn some HTML and PHP basics or find some pre-programmed solution.
Related
So, yeah, I am a newbie and currently I am spending my whole morning trying to find a way to get a value from a php file into another one.
More specifically, I am trying to create a simple captcha system using mt_rand command. So I put, for example, the following code in the first php file:
form.php
<?php
$captcha = mt_rand(10000,99999);?>
and including the form.php in the other .php file which is designed to be the image in my form page. In other words, the $captcha variable needs to be used in the form.php file so that the code in the image is the same as the one it is going to be checked in form.php using if statements:
image.php (image.php is an IMAGE using imagepng() and other commands)
<?php
include('form.php');?>
So, the whole story is about: the $captcha include a random number ranging from 10000-99999 and after the form submission it is checked in the form.php but it is also included (must be* inlcuded) in the image.php to generate the image with the code inside it.
Even though, using the include() command didnt work for me.
I am willing to get a solution in PHP but any others are welcome too, of course.
NOTE: Wanna point that I tried to just generate a random number in the image.php to see if it can be generated and it works ok but it can't retrieve the value when I use the include command.
PS: I guess I am typing too much shit also (too big text, etc), that's because Im really confused right now.
So, If anyone has time to check the two PHP files throughly, here they are:
form.php
<?php
$captcha = mt_rand(10000,99999);
if(isset($_POST['submit'])){
if($_POST['response']=$captcha){
echo "Captcha verified successfully.";
}else{
echo "Wrong Captcha Input!!!</br>";
}
}
?>
<!DOCTYPE html>
<head>
<title>Report Cheater</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="" method="post"></textarea><br>
Captcha(*): <img src="image.php">
<br><input type="text" name="response"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
image.php
<?php
//CAPTCHA verification
header ('Content-Type: image/png');
//Image to be converted into captcha
$im = imagecreatetruecolor(180, 40);
//Captch Background
$im2 = 'capback.png';
//Creates an instance of the captcha background to be added to the captcha
$rsc = imagecreatefrompng($im2);
// Copy and merge
imagecopymerge($im, $rsc, 0, 0, 0, 0, 180, 40, 100);
//Colors to be used
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//The captcha random number taken directly from report.php
include("form.php"); //works with $captcha = mt_rand(10000,99999); but not with the include("form.php");
// Font
$font = 'arial.ttf';
$x1 = mt_rand(29,41);
$y1 = mt_rand(42,58);
$x2 = $x1 + 1;
$y2 = $y1 + 1;
// Add some shadow to the text
imagettftext($im, 20, 0, $y2, $x2, $grey, $font, $captcha);
// Add the text
imagettftext($im, 20, 0, $y1, $x1, $black, $font, $captcha);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
imagedestroy($rsc);
?>
You compare two variables with ==
So change
if($_POST['response']=$captcha){
to
if($_POST['response']==$captcha){
First of all you must understand how capatcha system work
when you generate a hash or random numbers to use in capatcha
you must save it in the user session then use it in image and match it with user input.
In your code you generate two different numbers one in the image and then generated another one when you checked user input and this is logical error.
in image.php :
generate random number and you must start your file with session_start();
$capatcha=random(0,1000);
$_SESSION["capatcha"]=$capatcha;
then use $_SESSION["capatcha"] to generate image and then use it to verify user input
if(isset($_POST['submit'])){
if($_POST['response']==$_SESSION["capatcha"]){
echo "Captcha verified successfully.";
}else{
echo "Wrong Captcha Input!!!</br>";
}
}
I want to upload an image with some text on it, but when I'm uploading my PHP script it's not working with an image, it shows error every time. And also i want to add some html in the same file I had tried to put that php In html tag and show that image in the center how to do so
My PHP code is:
result.php
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('image.jpg');
$white = imagecolorallocate($jpg_image, 255, 255, 255);
$font_path = 'font.TTF';
$text = $_GET["name"];
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
?>
$_GET["name"]; is used for getting text form HTML by input tag, and for your information I have used form method to get the text I want an working example.
The image I want to use is http://images.visitcanberra.com.au/images/canberra_hero_image.jpg
So please use that image only while giving answer
this line will describe the name of image $jpg_image = imagecreatefromjpeg('image.jpg'); and image name is image.jpg
as you mentioned The image I want to use is http://images.visitcanberra.com.au/images/canberra_hero_image.jpg
this is image name canberra_hero_image.jpg right, how come php will work, first rename and use single name
better rename you php
this is the reason you see only text and not image
Change your code from '$jpg_image = imagecreatefromjpeg('image.jpg');' to this code
$image_path = ''http://images.visitcanberra.com.au/images/canberra_hero_image.jpg'';
$jpg_image = imagecreatefromjpeg($image_path);
Hello I am using a function that I found in Internet to display a barCode using a TrueType font, here is the code:
//For displaying barcodes
//Arguments are:
// code Number you want outputted as a barcode
//You can use this script in two ways:
// From a webpage/PHP script <img src='/images/barcode.php?code=12345'/>
// Directly in your web browser http://www.example.com/images/barcode.php?code=12345
//Outputs the code as a barcode, surrounded by an asterisk (as per standard)
//Will only output numbers, text will appear as gaps
//Image width is dynamic, depending on how much data there is
header("Content-type: image/png");
$file = "barcode.png"; // path to base png image
$im = imagecreatefrompng($file); // open the blank image
$string = "123123123"; // get the code from URL
imagealphablending($im, true); // set alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)
$black = imagecolorallocate($im, 0, 0, 0); // colour of barcode
$font_height=40; // barcode font size. anything smaller and it will appear jumbled and will not be able to be read by scanners
$newwidth=((strlen($string)*20)+41); // allocate width of barcode. each character is 20px across, plus add in the asterisk's
$thumb = imagecreatetruecolor($newwidth, 40); // generate a new image with correct dimensions
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, 40, 10, 10); // copy image to thumb
imagettftext($thumb, $font_height, 0, 1, 40, $black, 'B2FI25HRc.ttf', '*'.$string.'*'); // add text to image
//show the image
imagepng($thumb);
imagedestroy($thumb);
I cannot find the error why the function doesn't display the image. Any ideas? The font is in the same directory with the php function and I tried relative and absolute paths to the font with no results. Any suggestion?
Thank you very much
You need to check for error messages.
For debugging, comment out the header line and add these lines on the top to show all errors:
ini_set('display_errors',true);
error_reporting(E_ALL);
In many cases the error messages will tell you pretty clear whats wrong.
I am using this code to create an image
<?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()
(A)print ('<div class="test">');
imagepng($im);
print ('</div>');
(B)imagedestroy($im);
?>
The code work fines if i comment the line number 'A' and 'B' and it generates the image on the browser with testing written on it. But i want the image to be in a div. so i uncomment the line (A) and (B) but it is not giving right output. The generated html is also strange generated html is
<img src="http://localhost/php/test92.php" alt="The image “http://localhost/php/test92.php” cannot be displayed, because it contains errors.">
Basically, to create dynamic image in HTML, you will need 2 PHP files:
one for the image itself
another one for PHP to display it.
Let's take a look how to do it:
You create image.php that accept parameter, like: image ID or file name. For security reason, you HAVE to filter whatever parameter it get.
Why you have to do this? because, to generate image, you can't mix it with another HTML output. Let alone a single space or return as this will render the image broken.
You do the HTML thing on another PHP, say test92.php. To the HTML logic here, like:
get image data
loop the data
display image => <img src="image.php?imageID=12" alt="" />
If you want a div around your image you have to do that in the html, you can't do that in the image generation code
<div>
<img src="http://localhost/php/test92.php">
</div>
If you are getting errors regarding the image, try browsing the image url http://localhost/php/test92.php and see what it looks like.
Does it show an image like you are expecting?
I have a variable ($output) that is set to a string.
To mekt this string an image I'm using the PHP: GD Library.
In particular the imagestring() function, which I'm using with a very slight modification:
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, $output, $textcolor); #here is my string $output
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
This is working as expected: It turns $output into an image.
So, my problem is (or my best guess at least):
header('Content-type: image/png');
Which, doesn't allow me to output any html after that.
So I read this question, which asks if you can use two headers, which you can't, but the accepted answer recommends, something like this: <img src="my_img.php" />
This of course would be fine, except I don't know how would this solve my issues since, since even if I knew the source to this image (which I don't - so, that would be my first question, what's the path to the generated image*) I't would change the fact that the header is there and is not letting me output text.
So, I how would you approach this issue?
Thanks in advance!!
*I guess that would solve my issues since I could do this on an external file, and the just call the image (but maybe not since I would have to do an include and this would include the header too) as you see I'm a bit confused. Sorry :)
UPDATE:
So I'm seeing my question was confusing so I will add some more code to see if this clarifies my problem a little bit more:
<?php
$x = mt_rand(1,5);
$y = mt_rand(1,5);
function add($x, $y) { return $x + $y; }
function subtract($x, $y) { return $x - $y; }
function multiply($x, $y) { return $x * $y; }
$operators = array(
'add',
'subtract',
'multiply'
);
$rdno = $operators[array_rand($operators)];
$result = call_user_func_array($rdno, array($x, $y));
session_start();
$_SESSION['res'] = $result;
if ($rdno == "add") {
$whato = "+";
}elseif ($rdno == "subtract") {
$whato = "-";
} else {
$whato = "*";
}
$output = $x . $whato . $y . " = ";
?>
<form name="input" action="check.php" method="post">
<input type="text" name="result" />
<input type="submit" value="Check" />
</form>
I want $output to be a image so this got me trying to use the PHP:GD script above, but I can't make put in the same file because of the header.
You need to make a separate PHP script which serves the image, then make an <img> tag that points to this script.
You can send information to the script using the querystring in the image URL.
Morbo says: "Windmills do not work that way! Goodnight!"
Which means that you need to bone up on how this all works. This issue points to you having a basic misunderstanding of your tools. HTML can't contain images, it contains links to images. So your script needs to be included from another page via an image tag. (or CSS)
However, all this is a good thing. It means that this script can have dynamic elements that produce a new image each time it is called. Or that it could password-protect your images so only logged-in users can see it.
There are a host of ways to use this. And once you realize the image is like a page to php, this opens new routes. Php can output anything. Word docs, excel, pdf, css, even JS. all of which can be used to do cool things.
Just think of the image as a separate page. You'll get it. It'll just click into place in your mind. One of those big 'aha' moments.
First, the path.
From the manual :
imagepng ( resource $image [, string $filename [, int $quality [, int $filters ]]] )
It means that if you don't give a second argument (it is the case here), you don't have the path to a file but the data of the file / image ressource. The php file will be understand as a png file by the browser thanks to the header you give.
Second :
In your page (ie index.php), you could add this like that
<img src="myimg.php?output=[...]" />
and in your php script myimg.php you have it like this :
<?php
$output = $_GET['output'] // getting your text
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, $output, $textcolor); #here is my string $output
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
2 separates files.
You can send the image inline with echo '<img src="data:image/png;base64,'. base64_encode(imagepng($im)) .'" /> instead of <img src="my_img.php">.
Do not set a content-type header! Your output is text/html what you don't have to announce as it's the default in most server setups.
HTTP doesn't work that way. When your are serving image/png data, it as if you are serving a png file from the site, not an html file with an image in it. What are you trying to accomplish?
my_img.php is not source to your image. It is a source to the script generating that image, or reading it from file and outputting directly into browser. Obviously the approach with img src would be the best, because you'll hide all "boring" implementation of displaying image string behind browser's mechanisms.