Okay i have a site wich makes signatures. But because some header problems i had to put it in another file and use echo. this is the code:
<h1>Create your European Trucking signatures</h1>
<?php
echo '<img src="SigGen.php?player=$Playername&score=$Score&money=$Money" />';
?>
</div>
But it shows a broken image thingy. But when i go to the SigGen.php itself it does show up like here:
http://european-trucking.com/SigGen.php?player_name=Thimo
This is the code of SigGen:
<?
/*
***Made by: Nodroz***
*** Enjoy your signatures! ***
*/
$username="10528_Thimo"; //Your MySQL Username.
$password="*********"; // Your MySQL Pass.
$database="10528_Thimo"; // Your MySQL database.
$host="95.211.***.***"; // Your MySQL host. This is "localhost" or the IP specified by your hosting company.
$player_name=$_GET['player_name']; // This gets the player his name from the previous page.
/* Next, we will make a connection to the mysql.
If it can't connect, it'll print on the screen: Unable to select database. Be sure the databasename exists and online is. */
mysql_connect($host,$username,$password); // Connection to the database.
#mysql_select_db($database) or die( "Unable to select database. Be sure the databasename exists and online is."); //Selection of the database. If it can't read the database, it'll give an error.
/* To protect MySQL injection. */
$player_name = stripslashes($player_name);
$player_name = mysql_real_escape_string($player_name);
$query="SELECT * FROM Users WHERE Name='$player_name'"; // Gets all the information about the player.
$result=mysql_query($query);
$i=mysql_num_rows($result); // Here we are counting how many rows this result gives us.
/* We will now put the player's information into variables so we can use them more easily. */
/* DON'T FORGET: The names should be exact the same as in your mysql db.*/
if ($i == 1) // If the user has been correct, then it'll give us 1 row. If its 1 row, then it'll proceed with the code.
{
$Playername=mysql_result($result,0,"Name"); // Gets the username of the player and put it in the variable $Playername.
$Money=mysql_result($result,0,"Money"); // Gets the money of the player and put it in the variable $Money.
$Score=mysql_result($result,0,"Score"); // Gets the score of the player and put it in the variable $Score.
// Creating of the .png image.
header('Content-Type: image/png;'); // Don't touch this. We use this to tell the script we're working with an image.
$im = #imagecreatefrompng('PlayerSig.png') or die("Cannot select the correct image. Please contact the webmaster."); // Don't forget to put your picture there. Eg: playersig.png
$text_color = imagecolorallocate($im, 000,000,000); // RED, GREEN, BLUE --> Go to www.colorpicker.com, select a nice color, copy the R/G/B letters provided by colorpicker and put them here.
$text_username = "$Playername"; // This gets the information: player name to be showed in the picture.
$text_score = "$Score"; // Same as above but with score.
$text_money = "$Money"; // Same as above but with money.
$font = 'myfont.ttf'; //Upload your custom font to the directory where this file is placed. Then change the name here.
/* USAGE OF THE imagettftext: First ($im) shouldn't be changed. (16) is the text-size. (0) is the angle of your text. Change it, and you'll see what's going on. (20) is de X-coordinate of the text.
(36) is the Y-coordinate of the text. */
imagettftext($im, 16, 0, 5, 20, $text_color, $font, $text_username); // Prints the username in the picture.
imagettftext($im, 16, 0, 5, 40, $text_color, $font, "Score:");
imagettftext($im, 16, 0, 72, 40, $text_color, $font, $text_score); // Prints the score in the picture.
imagettftext($im, 16, 0, 5, 60, $text_color, $font, "Money:");
imagettftext($im, 16, 0, 72, 60, $text_color, $font, $text_money); // Prints the money in the picture.
imagepng($im);
imagedestroy($im);
} else echo('Username is not in our database. Please try again.'); // If the username doesn't exist (so the row is 0) then it'll give en error.
mysql_close();
?>
Anyone knows why it doesnt show up as an image using echo?
i think the problem is that you cant use variable names in string like this :
THIS WONT WORK
echo 'Hi $userName';
THIS WILL WORK
echo "Hi $userName";
so your line :
echo '<img src="SigGen.php?player=$Playername&score=$Score&money=$Money" />';
needs to be changed to :
echo "<img src='SigGen.php?player=$Playername&score=$Score&money=$Money' />";
try this...
<?php echo '<img src="SigGen.php?player='.$Playername.'&score='.$Score.'&money='.$Money.'" />'; ?>
use the concatenation features of the echo to properly append the variables to the image link
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>";
}
}
im building a website indianskincare.co.in which is coming out to what i want. the problem is i want to turn all text for each product into pictures as my client wants the text not to be so easily copied. i see how this is done using PHP by calling the PHP in the image source and the relevant code in the php.
it works on normal text wonderfully but when i include mysql row or any complex code it shows a broken image.
this works displays time and text
Header( "Content-type: image/png");
$image = imagecreate(320,130);
$date = date ("F dS Y h:i a");
$ip = "ip";
$fullip = "Your IP address is $ip.";
$black = ImageColorAllocate($image,0,0,0);
$red = ImageColorAllocate($image,204,0,0);
$white = ImageColorAllocate($image,255,255,255);
ImageFilledRectangle($image,0,0,320,130,$white);
ImageString($image, 14, 0, 0, "$fullip", $red);
ImageString($image, 12, 0, 24, "Time is $date", $black);
ImagePNG($image);
ImageDestroy($image);
I tried to add my mysql code between one of the variables (i didn't think it would matter between which) which works perfectly otherwise on another page but can't seem to change the text into picture.
mysql code
if(isset($_GET['id']))
{
$id=$_GET['id'];
$qry=mysql_query("SELECT * FROM Product WHERE id='$id'", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
/* Fetching data from the field "title" */
while($row=mysql_fetch_array($qry))
{
echo "<div style='overflow-x:hidden;width:20%;margin:0.5em;text-align:center;float:left;'>";
echo "<h3>".$row['name']."</h3>";
echo "<img style='width:100%;margin:0;' src='".$row['image']."' /><br>";
echo "<h1><img style='height:4%;' src='images/rs.png' />".$row['price']."</h1><br><a href='product.php?cat=".$row['short']."'>Back</a><br>";
echo "</div><div style='width:50%;float:left;'>";
echo "<h5 style='font-weight:normal;'>".$row['description']."</h5><br>";
so my question is how to modify the text from mysql array into image.
ps: i know mysql is being deprecated not preferred but ive already done the website using this and i will not be able to modify it to use the other methods
a time ago I use this class - and it works perfectly:
http://image.intervention.io/
Intervention Image requires the following components to work correctly.
PHP >= 5.3
Fileinfo Extension
And one of the following image libraries.
GD Library (>=2.0) … or …
Imagick PHP extension (>=6.5.7)
A good start is to install the composer:
https://packagist.org/
With the composer you can easily install PHP Packages / Modules for almost all issue.
Example from the page (how to convert text to an image)
// create Image from file
$img = Image::make('public/foo.jpg');
// write text
$img->text('The quick brown fox jumps over the lazy dog.');
// write text at position
$img->text('The quick brown fox jumps over the lazy dog.', 120, 100);
// use callback to define details
$img->text('foo', 0, 0, function($font) {
$font->file('foo/bar.ttf');
$font->size(24);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('top');
$font->angle(45);
});
// draw transparent text
$img->text('foo', 0, 0, function($font) {
$font->color(array(255, 255, 255, 0.5))
});
So an example for your code could be (if intervention image class is working):
$result = array();
while($row=mysql_fetch_array($qry))
{
$result[] = array(
'name' => convertToImage($row['name'], $row['id'].'_name'),
'price' => convertToImage($row['price'], $row['id'].'_price')
)
}
function convertToImage($text, $name) {
$img = Image::make('images/transparent.png');
$img->text($text);
$img->save('images/'.$name.'.png');
return '<img src="images/'.$name.'.png">';
}
?>
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 am trying to print users personal data like name, email, phone number on screen, using separate images, rather than printing out in clear text on page, thus getting cache possibly by Google. Trying to print like below:
Name - image with name text created on fly
Email - image with email text created on fly
Phoneno - image with number text created on fly
The code I have provided merges text into an image ie name, but only allows me to create one image to send back to browser, how can I try to get my script to send more than one image back to browser?
I have tried adding more parameters to my function function 'create_image($name,$email,$number)' but only prints one field to browser, perhaps something to do with header()?
<?php
//Send a generated image to the browser
$name="Bob";$email="bob#email.co.uk";$number="12345678901";
create_image($name);
function create_image($value)
{
//Set the image width and height
$width = 250;
$height = 20;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
//Add randomly generated string in white to the image
//imagestring ( resource $image , int $font-(font size) , int $x-(from left) , int $y-(from right) , string $string , int $color-(font-colour) )
ImageString($image, 5, 10, 3, $value, $white);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
}
?>
Thanks for any replies
$name="Bob";$email="bob#email.co.uk";$number="12345678901";
switch ($_GET['option']) {
case 'name':
create_image($name);
break;
case 'email':
create_image($email);
break;
case 'number':
create_image($number);
break;
}
And call your script with scriptname.php?option=name
its not really a best practice, but I i would try to change your function, that it saves the images to a file, and returns the file path, when theres is a valid file.
So you don't have to create every call a new image (which is slow), and you get a valid filepath, which means you don't have to care about headers and so on