Why my image created by php is not displaying? - php

I have a problem , I have a software that gets pc tech specs and post them online and we are offering a signature image for each specs, this is created by php , but is not working anymore;
Demo link : http://checkmyspecs.co.uk/button.php?id=646725 or go to any specs page , example : http://www.checkmyspecs.co.uk/display2.php?id=646725 and down on the page is grab button code.
The image must be created by button.php , here is the code :
<?php
include "db.php";
function getdata($viewerid) {
$query = "SELECT * FROM data where viewerid = '$viewerid'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$data = array();
array_push($data,$row['bootmethod'],$row['ComputerCaption'],$row['Infrared'],$row['DayLight'],$row['ManufacturerBox'],$row['Model'],$row['cores'],$row['memory'],$row['monitor'],$row['resolution'],$row['pixels'],$row['cpuvoltage'],$row['clockspeed'],$row['AddressWidth'],$row['SocketDesignation'],$row['cpuname'],$row['loadpercent'],$row['applications'],$row['videocardname'],$row['refreshrate'],$row['videodriver'],$row['installed'],$row['hddata'],$row['directx']);
return $data;
}
function LoadPNG($imgname,$cur)
{
$getvalues = getdata($cur);
/* Attempt to open */
$im = #imagecreatefrompng($imgname);
// Removes white background (made from the original transparency)
$white = imagecolorallocate($im, 255, 255, 255);
// Make the background transparent
//Generate the text to write onto the image (the php Version).
//Don't know much about this
// Add some shadow to the text
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$blue = imagecolorallocate($im, 0, 173, 238);
$darkblue = imagecolorallocate($im, 54, 154, 191);
$font = '/fonts/arialbd.ttf';
$ramo = $getvalues[7];
$cpuo = $getvalues[15];
$gpuo = $getvalues[18];
$screeno = $getvalues[9];
$ram = "$ramo MB";
$cpu = "$cpuo";
$gpu = "$gpuo ";
$screen = "$screeno";
imagettftext($im, 11, 0, 40, 23, $black, $font, $cpu);
imagettftext($im, 11, 0, 40, 53, $black, $font, $gpu);
imagettftext($im, 11, 0, 40, 83, $black, $font, $screen);
imagettftext($im, 11, 0, 40, 113, $black, $font, $ram);
// write text
$textcolor = imagecolorallocate($im, 0, 3, 0);
return $im;
}
header('Content-Type: image/png');
$button = '/images/button.png';
$img = LoadPNG($button,$_GET['id']);
imagepng($img);
?>

Your image isn't an image. Here's what is really being output:
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 24
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 33
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 34
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 35
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 36
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 37
Fatal error: Call to undefined function imagettftext() in /home/checkmys/public_html/button.php on line 53
You can use tools such as Fiddler to see this, even when your content-type is set as an image. If you would remove that # symbol, we could see the error describing why your image could not be created.
Also, you are currently wide open to SQL injection. You should use prepared queries with PDO to avoid this problem.

Related

php GD library sometimes generate blank image

I trying to generate a signature from a name using the GD library, and return the png image in base64 format.
With the following code, the function correctly returns the base64 image with the signature:
<?php
function genSignature($name){
// The text to draw
$tamano = 15 * strlen($name);
$fuentes = [
'../fuentesFirma/HomemadeApple-Regular.ttf',
'../fuentesFirma/Allura-Regular.ttf',
'../fuentesFirma/DawningofaNewDay-Regular.ttf',
'../fuentesFirma/HerrVonMuellerhoff-Regular.ttf',
'../fuentesFirma/LaBelleAurore-Regular.ttf',
];
$fuente = $fuentes[array_rand($fuentes, 1)];
// Create the image
$im = imagecreatetruecolor($tamano, 60);
// 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, $tamano, 60, $white);
// Add the text
imagettftext($im, 20, 0, 0, 30, $black, $fuente, $name);
ob_start();
imagepng($im);
$buffer = ob_get_clean();
ob_end_clean();
return base64_encode($buffer);
}
echo genSignature('María'); //Commented line when calling from another script, for testing only.
But if I include the file with the function, and call it from another script, it always returns the same image, a white square (base64). I don't understant where is the problem..
Other script:
<?php
include_once('generarFirma.php'); //in same folder
//....
//some operations...
$signature = genSignature('María');

Create image from included PHP file

I would like to create an image from an included PHP file, there is an error of undefined variable:
index.php
<?php
// get servers and domains
$serSel = 'SELECT * FROM servers';
$serReq = $connexion->query($serSel);
$serRes = $serReq->fetchAll();
$totalSer = count($serRes);
foreach($serRes as $Serv)
{
$s_global_size = $Serv['s_global_size'];
$s_used_size = $Serv['s_used_size'];
$s_creation = $Serv['s_creation'];
$s_expiry = $Serv['s_expiry'];
$diskSpace = ($e_name == 'domain') ? '-' : format_bytes($s_global_size);
echo $diskSpace;
$diskUsage = ($e_name == 'domain') ? '-' : format_bytes($s_used_size);
echo $diskUsage;
$s_left_size = ($s_global_size - $s_used_size);
$sLeftSize = ($e_name == 'domain') ? '-' : format_bytes($s_left_size);
echo $sLeftSize;
}
echo '<img src="Idara/Statistics/webdata_circle.php">';
?>
webdata_circle.php
<?php
// create image
$image = imagecreatetruecolor(180, 180);
$usagePerc = ($diskUsage * 360)/$diskSpace;
// allocate some colors
$red = imagecolorallocate($image, 192, 0, 0);
$red1 = imagecolorallocate($image, 192, 20, 0);
$green = imagecolorallocate($image, 0, 133 ,62);
$green1 = imagecolorallocate($image, 0, 140 ,62);
$trans = imagecolorallocate($image, 0, 0, 0); // transparent background
// Make the background transparent
imagecolortransparent($image, $trans);
imagefilledarc($image, 80, 80, 150, 150, 0, 360, $red, IMG_ARC_PIE);
imagefilledarc($image, 80, 80, 150, 150, 0, $usagePerc, $green, IMG_ARC_PIE);
// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
The error returned is:
PHP Notice: Undefined variable: diskUsage in webdata_circle.php on line 8
PHP Notice: Undefined variable: diskSpace in webdata_circle.php on line 8
PHP Warning: Division by zero in webdata_circle.php on line 8
Thanks in advance
NB: This is the full code, I hope it will help
You need to add the following lines to webdata_circle.php, on the 2nd line:
$diskPath = dirname(__FILE__);
$diskUsage = disk_total_space ($diskPath);
$diskUsage = disk_free_space ($diskPath);

How Can I Load My Custom Font in imagestring()?

My problem is Q title.
I tried http://www.php.net/manual/en/function.imageloadfont.php and:
imageloadfont();
but i do not see any change and i get error:
Warning: imageloadfont(): gd warning: product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully in E:\xampp\htdocs\test\texts\text01.php on line 3
Warning: imageloadfont(): Error reading font, invalid font header in E:\xampp\htdocs\test\texts\text01.php on line 3
Edit:
My font.
To quote myself, you should be using imagettftext(), not imagestring().
Example usage, abbreviated/adapted from the manual page:
$im = imagecreatetruecolor(400, 30);
// Create some colors and set background to white.
$white = imagecolorallocate($im, 255, 255, 255);
$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 = 'btitr.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im, 'path_to_file.png');
imagedestroy($im);

Weird image glitch when using imagepng()

I am trying to make a gamecard for a RSPS. This is my first time using PHP to generate text on images and when trying to apply this weird glitch happens:
But, when the original photo looks like this:
As you can see the corners are not curved like they should be. But, in the original it comes out fine. I am not talking about the location of the numbers.
Here is the code I am using:
<?php
header('Content-Type: image/png');
// Image Creation
$image_file = "SoulSplitCard.png";
$im = imagecreatefrompng($image_file);
//Colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$rscol = imagecolorallocate($im, 23, 113, 183);
// Levels (for now)
$text = '99';
$text2 = '87';
// Font
$font = 'arial.ttf';
//Text Applications
imagettftext($im, 15, 0, 150, 35, $rscol, $font, $text);
imagettftext($im, 15, 0, 150, 81, $rscol, $font, $text2);
// Using imagepng() instead of imagejpeg()
imagepng($im);
imagedestroy($im);
?>
What is causing this to happen?

imagettftext wont work; text doesn't show up

I'm trying to use the imagettftext() to add text to an image via PHP. So far I've gone through about 6 different tutorials, trying to get it to work, and I have had no success. I'm currently trying this code from the php documentation page.
<?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()
imagepng($im);
imagedestroy($im);
?>
However, nothing shows up. I have the GD library installed, as well as freetype enabled. I have arial.ttf in the same directory as the php file, and I cannot figure out why it wont work. All I get is a blank image.
EDIT: This is the error message "[19-Jan-2014 16:31:07] PHP Warning: imagettftext(): Could not find/open font in /var/www/php/bb/test.php on line 21"
I was about about to tell you to type the real path of the ttf instead of the relative path.
putenv('GDFONTPATH=' . realpath('.'));
or
$font = '/home/user/arial.ttf';

Categories