I am trying to make a generator to output user data from my gaming website and I've never really looked into the conversion to images in PHP, however I've managed to get the basics of it working now from other questions on this site and elsewhere, anyway I was wondering whether it is possible to make my PHP file that generates the image, parse HTML tags in its' output? Here is my script:
<?php
$username = $_GET['username'];
$guild = $_GET['guild'];
$create = "Username: " . $username . " Guild: " . $guild . "";
$im = imagecreate(300, 30);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
imagestring($im, 5, 0, 0, $create, $textcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
In my $create variable, I wish to use HTML tags but they aren't parsed as html and it outputs the tags in plain text.
Sorry, but PHP can't parse HTML before rendering it on an image. However, it does include various functions to change fonts and apply filters to the text. See http://php.net/manual/en/ref.image.php.
Is there a specific reason you need the information to be output as an image? Why not simply output the information using HTML?
Your browser parses HTML, php does nothing with it. You'll have to use stuff like \n in your string.
Related
I created an uploader for myself, now what I am trying is adding text on it too. I already created it outside the uploader but I just can't combine them two, already tried multiple sequences but failed :( need help here's the code.!
Here is the upload code.
$file_name = $_FILES[attach][name];
$caption=$_POST["caption"];
$uploaddir = "../photos";
$up_path=$uploaddir."/".$file_name;
move_uploaded_file($_FILES['attach']['tmp_name'], $up_path);
Here's the text on img code:
$get_image = imagecreatefromjpeg('name.jpg');
$white = imagecolorallocate($get_image, 255, 255, 255);
$txt = "Hello World";
$font = "arial.ttf";
imagettftext($get_image, 24, 0, 5, 24, $white, $font, $txt);
header('Content-type: image/jpeg');
imagejpeg($get_image, "name.jpg", 100);
imagedestroy($get_image);
You can achieve watermarking using image libraries.
You can find examples here.
Add 'Watermark' to images with php
An alternative way is to use FFMPEG Library which can be executed through php exec() method. See the link below.
How to add transparent watermark in center of a video with ffmpeg?
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);
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.