I have created a dynamic signature maker for my online game.
You can create the sig manually via
http://pernix-rsps.com/sig/pcard.php?user=usernamehere
I tried to make a userbox and submit , so that people does not have to visit
http://pernix-rsps.com/sig/pcard.php?user=USERNAME
and edit it, i want it to create the link for them upon entering username
my code for the username box
<center>
<form name="sig" id="sig" method="get" action="pcard.php">
<table border="0">
<tr><td colspan="2"><?php echo isset($_GET["user"])?$_GET["user"]:"";?> </td></tr>
<tr><td width="30">Username</td><td width="249"><input name="username" type="text" id="username" width="150px" placeholder="Username" /> </td></tr>
<tr><td></td><td><input name="btnsubmit" type="submit" id="btnsubmit" title="create sig" /></td></tr>
</table>
</form>
</center>
And then pcard.php
<?php
if (isset($_GET['user'])) {
$image_path = "img/saved_cards/".$_GET['user'].".png";
if (file_exists($image_path)) {
if (time() < filemtime($image_path) + 300) { // every 5 minutes ?
pullFromCache($image_path);
exit;
}
}
generate();
}
function pullFromCache($image_path) {
header("Content-type: image/png");
$image = imagecreatefrompng($image_path);
imagepng($image);
}
function generate() {
$DB_HOST = "localhost";
$DB_USER = "";
$DB_PASS = "";
$DB_NAME = "";
$user = clean($_GET['user']);
$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME) or die($con->error);
$res = $con->query("SELECT * FROM hs_users WHERE username='$user'");
if($res->num_rows > 0) {
header("Content-type: image/png");
getSig($res->fetch_assoc());
} else {
header("Content-type: image/png");
$image = imagecreatefrompng('./img/sigbg.png');
$color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 3, 251, 10, 'Invalid User', $color);
imagepng($image);
}
}
function getSig($row) {
$image = imagecreatefrompng('./img/sigbg.png');
$color = imagecolorallocate($image, 255, 255, 255);
$yellow = imagecolorallocate($image, 255, 255, 0);
$total = getTotalLevel($row);
$combat = getCombatLevel($row);
imagestring($image, 5, 250, 10, ''.$row['username'].'', $yellow);
imagestring($image, 2, 250, 26, 'Exp: '.number_format($row['overall_xp']).'', $color);
imagestring($image, 2, 250, 39, 'Total: '.number_format($total).'', $color);
imagestring($image, 2, 250, 51, 'Pernix-Rsps.com ', $color);
$array = array("Attack", "Defence", "Strength", "hitpoints", "Range", "Prayer", "Magic", "Cooking", "Woodcutting", "Fletching", "Fishing", "Firemaking", "Crafting", "Smithing", "Mining", "Herblore", "Agility", "Thieving", "Slayer", "Farming", "Runecrafting", "Hunter", "pk", "Summoning", "Dungeoneering");
$baseX = 28;
$baseY = 4;
foreach ($array as $i => $value) {
imagestring($image, 2, $baseX, $baseY, ''.getRealLevel($row[''.strtolower($array[$i]).'_xp'], strtolower($array[$i])).'', $color);
$baseY += 15;
if ($baseY > 64) {
$baseY = 4;
$baseX += 45;
}
}
ImagePNG($image, "img/saved_cards/".$row['username'].".png");
imagepng($image);
}
function getTotalLevel($row) {
$total = 0;
$array = array("Attack", "Defence", "Strength", "Hitpoints", "Range", "Prayer", "Magic", "Cooking", "Woodcutting", "Fletching", "Fishing", "Firemaking", "Crafting", "Smithing", "Mining", "Herblore", "Agility", "Thieving", "Slayer", "Farming", "Runecrafting", "Hunter", "pk", "Summoning", "Dungeoneering");
foreach ($array as $i => $value) {
$skillName = strtolower($array[$i]);
$total += getRealLevel($row[$skillName.'_xp'], strtolower($skillName));
}
return $total;
}
function getLevel($exp) {
$points = 0;
$output = 0;
for ($lvl = 1; $lvl <= 99; $lvl++) {
$points += floor($lvl + 300.0 * pow(2.0, $lvl / 7.0));
$output = (int) floor($points / 4);
if (($output - 1) >= $exp) {
return $lvl;
}
}
return 99;
}
function getRealLevel($exp, $skill) {
$points = 0;
$output = 0;
$skillId = $skill == "dungeoneering" ? 1 : 0;
for ($lvl = 1; $lvl <= ($skillId == 1 ? 120 : 99); $lvl++) {
$points += floor($lvl + 300.0 * pow(2.0, $lvl / 7.0));
$output = (int) floor($points / 4);
if (($output - 1) >= $exp) {
return $lvl;
}
}
return ($skillId == 1 ? 120 : 99);
}
function getDungLevel($exp) {
$points = 0;
$output = 0;
for ($lvl = 1; $lvl <= 120; $lvl++) {
$points += floor($lvl + 300.0 * pow(2.0, $lvl / 7.0));
$output = (int) floor($points / 4);
if (($output - 1) >= $exp) {
return $lvl;
}
}
return 120;
}
function clean($string) {
return preg_replace('/[^A-Za-z0-9 \-]/', '', $string);
}
function getCombatLevel($row) {
$attack = getLevel($row['attack_xp']);
$defence = getLevel($row['defence_xp']);
$strength = getLevel($row['strength_xp']);
$hp = getLevel($row['hitpoints_xp']);
$prayer = getLevel($row['prayer_xp']);
$ranged = getLevel($row['range_xp']);
$magic = getLevel($row['magic_xp']);
$combatLevel = (int) (($defence + $hp + floor($prayer / 2)) * 0.25) + 1;
$melee = ($attack + $strength) * 0.325;
$ranger = floor($ranged * 1.5) * 0.325;
$mage = floor($magic * 1.5) * 0.325;
if ($melee >= $ranger && $melee >= $mage) {
$combatLevel += $melee;
} else if ($ranger >= $melee && $ranger >= $mage) {
$combatLevel += $ranger;
} else if ($mage >= $melee && $mage >= $ranger) {
$combatLevel += $mage;
}
return (int)$combatLevel;
}
?>
upon entering and submiting the username in the box, it just takes you to the pcard.php without image being made
any ideas
You are using the wrong fieldname in your PHP. In your form you use the fieldname username:
<input name="username" ... />
And in your PHP you try to get GET['user']. Change that in GET['username'] and everything should work (the getting the value part that is ;)).
Related
I have a PHP pie chart which uses data from Mysql to show the chart. However if one data is missing the whole chart turns to one color. For example for grading if the input is A, B, D and F(pay attention C grade input is missing) then the whole pie chart is in one color like Orange or red.
Can you please help me with this? Thanks
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('0000ff', '006600', 'ffff00','DD7C1D', 'FF3300', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = array_filter(explode('*',$data));
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>
So I'm trying to get a generated UPC bar code to display on an index page. but all it does is output the PNG contents instead of displaying the PNG itself.
I'm not quite sure why its doing this. I'm guessing its some silly little thing i need to add but I have no clue what it would be. So any help would be appreciated.
INDEX CODE
<form method="POST">
<head>UPC Barcode and QRcode Generator</head><br>
<label for="">Type 11 Didgits Here => </label>
<input type="text" class="form-control" name="text_code">
<button type="submit" class="btn btn-primary" name="generate">Generate</button>
<hr>
<?php
//QR CODE
if(isset($_POST['generate'])){
$code = $_POST['text_code'];
echo "<img src='https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=$code&choe=UTF-8'>";}
//Barcode
include "generate.php";
$upc = new BC(null,4);
$number = $_POST['text_code'];
$upc->build($number);
echo "*".substr($code, 0,6)."#".substr($code, 6,6)."*";
?>
GENERATE CODE
<?php
class BC{//
private $height;
private $width;
private $barheight;
private $barwidth;
private $lable;
private $border;
private $padding;
private $mapcode;
private $rightmap;
private $code;
function __construct($lable=null,$barwidth=2) {//
$this->set_width($barwidth);
$this->set_lable($lable);
$this->padding = $barwidth*5;
$this->border =2;
$this->mapcode = array
('0' => '0001101', '1' => '0011001', '2' => '0010011', '3' => '0111101', '4' => '0100011',
'5' => '0110001', '6' => '0101111', '7' => '0111011', '8' => '0110111', '9' => '0001011',
'#' => '01010', '*' => '101');
$this->rightmap = array
('0' => '1110010', '1' => '1100110', '2' => '1101100', '3' => '1000010', '4' => '1011100',
'5' => '1001110', '6' => '1010000', '7' => '1000100', '8' => '1001000', '9' => '1110100',
'#' => '01010', '*' => '101');
}
public function set_width($barwidth) {//
if(is_int($barwidth)) {//
$this->barwidth = $barwidth;
}
else{//
$this->barwidth = 2;
}
}
public function set_lable($lable) {//
if(is_null($lable)) {//
$this->lable = "none";
}
else{//
$this->lable = $lable;
}
}
public function build($code = null) {//
if(is_null($code)) {//
$this->code = "00000000000";
}
else{//
$this->code = $code;
}
$this-> code = substr($code, 0, 11);
if(is_numeric($code)){//
$checksum = 0;
for($digit = 0; $digit < strlen($code); $digit++) {
if($digit%2 == 0) {//
$checksum += (int)$code[$digit] * 3;
}
else{//
$checksum += (int) $code[$digit];
}
}
$checkdigit = 10 - $checksum % 10;
$code .= $checkdigit;
$code_disp = $code;
$code = "*".substr($code, 0,6)."#".substr($code, 6,6)."*";
$this->width = $this-> barwidth*95 + $this->padding*2;
$this->barheight = $this->barwidth*95*0.75;
$this->height = $this->barwidth*95*0.75 + $this->padding*2;
$barcode = imagecreatetruecolor($this->width, $this->barheight);
$black = imagecolorallocate($barcode, 0, 0, 0);
$white = imagecolorallocate($barcode, 255, 255, 255);
imagefill($barcode, 0, 0, $black);
imagefilledrectangle($barcode, $this->border, $this->width - $this->border, -1, $this->barheight - $this->border - 1, $white);
$bar_pos = $this->padding;
for($count = 0; $count < 15; $count++) {
$character = $code [$count];
for($subcount = 0; $subcount < strlen($this->mapcode[$character]); $subcount++) {//
if($count < 7) {
$color = ($this->mapcode[$character][$subcount] == 0) ? $white : $black;
}
else{
$color = ($this->rightmap[$character][$subcount] == 0) ? $white : $black;
}
if(strlen($this->mapcode[$character]) == 7) {
$height_offset = $this->height * 0.05;
}
else {
$height_offset = 0;
}
imagefilledrectangle($barcode, $bar_pos, $this->padding, $bar_pos+$this->barwidth - 1, $this->barheight - $height_offset - $this->padding, $color);
$bar_pos += $this->barwidth;
}
imagepng($barcode);
}
}
}
}
?>
So this is the output
after adding the new code
To display an image in an HTML page, you need to use an <img /> tag. To display image contents in an <img /> tag, you need to use a Data URI Scheme.
You'll end up with something like this:
echo '<img src="data:image/png;base64,', base64_encode($the_png_contents), '" />';
I checked out your Q and in the comments you said it still is not working so I decided to have a look with you. I used your code as you posted in and used it on my own little environment to make it work. What I did is the following:
Changed I made in your index.php:
// Check if request method is post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if both Generate and Text_code have been set
if(isset($_POST['generate']) && isset($_POST['text_code'])) {
// Set code used through the if statement
$code = $_POST['text_code'];
echo '<img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl='. $code . '&choe=UTF-8">';
// Include file; generate.php
include "generate.php";
// New instance of class; BC
$upc = new BC(null, 4);
// Set barcode base64 to variable and display
$barcodeBase64 = $upc->build($code);
echo '<img src="data:image/png;base64,' . base64_encode($barcodeBase64) . '" />';
}
}
Furthermore your generate.php needed a little change in the function build:
public function build($code = null) {
$this->code = is_null($code) ? "00000000000" : $code;
$code = substr($code, 0, 11);
$code = str_pad($code, 11, "00000000000");
if(is_numeric($code)){//
$checksum = 0;
for($digit = 0; $digit < strlen($code); $digit++) {
if($digit%2 == 0) {//
$checksum += (int)$code[$digit] * 3;
}
else{//
$checksum += (int) $code[$digit];
}
}
$checkdigit = 10 - $checksum % 10;
$code .= $checkdigit;
$code_disp = $code;
$code = "*".substr($code, 0,6)."#".substr($code, 6,6)."*";
$this->width = $this-> barwidth*95 + $this->padding*2;
$this->barheight = $this->barwidth*95*0.75;
$this->height = $this->barwidth*95*0.75 + $this->padding*2;
$barcode = imagecreatetruecolor($this->width, $this->barheight);
$black = imagecolorallocate($barcode, 0, 0, 0);
$white = imagecolorallocate($barcode, 255, 255, 255);
imagefill($barcode, 0, 0, $black);
imagefilledrectangle($barcode, $this->border, $this->width - $this->border, -1, $this->barheight - $this->border - 1, $white);
$bar_pos = $this->padding;
for($count = 0; $count < 15; $count++) {
$character = $code[$count];
for($subcount = 0; $subcount < strlen($this->mapcode[$character]); $subcount++) {//
if($count < 7) {
$color = ($this->mapcode[$character][$subcount] == 0) ? $white : $black;
}
else{
$color = ($this->rightmap[$character][$subcount] == 0) ? $white : $black;
}
if(strlen($this->mapcode[$character]) == 7) {
$height_offset = $this->height * 0.05;
}
else {
$height_offset = 0;
}
imagefilledrectangle($barcode, $bar_pos, $this->padding, $bar_pos+$this->barwidth - 1, $this->barheight - $height_offset - $this->padding, $color);
$bar_pos += $this->barwidth;
}
}
ob_start();
imagepng($barcode);
$barcodeImage = ob_get_contents();
ob_clean();
return $barcodeImage;
}
}
I also see that you filled the image with black which normally is filled with white as the code is a black on white image. To change this simply replace:
imagefill($barcode, 0, 0, $black); > imagefill($barcode, 0, 0, $white);
How to retrieve data in .htm page using twig.
public function onRun()
{
$captchaImagePath = '/Applications/MAMP/htdocs/install-master/storage/app/uploads/captcha/';
Log::info($captchaImagePath);
$captchaImageUrl = '/Applications/MAMP/htdocs/install-master/storage/app/uploads/captcha/';
$captchaFontPath = '/Applications/MAMP/htdocs/install-master/storage/app/uploads/fonts/verdana.ttf';
$val = array(
'word_length' => 5,
'word' => '',
'img_path' => $captchaImagePath,
'img_url' => $captchaImageUrl,
'font_path' => $captchaFontPath,
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200
);
$img_path=$captchaImagePath;
$img_url=$captchaImageUrl;
$font_path=$captchaFontPath;
$captcha = $this->create_captcha($val,$img_path,$img_url,$font_path);
$url = Request::url();
if (ends_with($url, ['.html', '.htm']))
{
$url = str_replace(['.html', '.htm'], '', $url);
return Redirect::to($url, 301)->with($captcha);
}
Log::info($url);
Log::info($captcha);
}
the function in same file public function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
{
// Log::info($data);
// Log::info($img_path);
// Log::info($img_url);
// Log::info($font_path);
if(!isset($data['word_length']))
{
$length=5;
}
else
{
$length=$data['word_length'];
}
//Log::info($length);
$defaults = array('word' => '', 'word_length' => $length,'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
// Log::info($defaults);
foreach ($defaults as $key => $val)
{
if ( ! is_array($data))
{
if ( ! isset($$key) OR $$key == '')
{
$$key = $val;
// Log::info( $$key);
}
}
else
{
$$key = ( ! isset($data[$key])) ? $val : $data[$key];
}
}
// Log::info($img_path); Log::info($img_url);
if ($img_path == '' OR $img_url == '')
{
return FALSE;
}
if ( ! is_dir($img_path))
{
return FALSE;
}
if ( ! is_writable($img_path))
{
return FALSE;
}
if ( ! extension_loaded('gd'))
{
return FALSE;
}
// -----------------------------------
// Remove old images
// -----------------------------------
list($usec, $sec) = explode(" ", microtime());
$now = ((float)$usec + (float)$sec);
$current_dir = #opendir($img_path);
while($filename = #readdir($current_dir))
{
if ($filename != "." and $filename != ".." and $filename != "index.html")
{
$name = str_replace(".jpg", "", $filename);
if (($name + $expiration) < $now)
{
#unlink($img_path.$filename);
}
}
}
#closedir($current_dir);
// -----------------------------------
// Do we have a "word" yet?
// -----------------------------------
if ($word == '')
{
//$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$pool = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$str = '';
for ($i = 0; $i < $word_length; $i++)
{
$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
}
$word = $str;
}
// -----------------------------------
// Determine angle and position
// -----------------------------------
$length = strlen($word);
$angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
$x_axis = rand(6, (360/$length)-16);
$y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
// -----------------------------------
// Create image
// -----------------------------------
// PHP.net recommends imagecreatetruecolor(), but it isn't always available
if (function_exists('imagecreatetruecolor'))
{
$im = imagecreatetruecolor($img_width, $img_height);
}
else
{
$im = imagecreate($img_width, $img_height);
}
// -----------------------------------
// Assign colors
// -----------------------------------
$bg_color = imagecolorallocate ($im, 255, 255, 255);
$border_color = imagecolorallocate ($im, 232, 244, 252);
$text_color = imagecolorallocate ($im, 57, 136, 190);
$grid_color = imagecolorallocate($im, 220, 239, 253);
$shadow_color = imagecolorallocate($im, 255, 240, 240);
// -----------------------------------
// Create the rectangle
// -----------------------------------
ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
// -----------------------------------
// Create the spiral pattern
// -----------------------------------
$theta = 1;
$thetac = 7;
$radius = 16;
$circles = 20;
$points = 32;
for ($i = 0; $i < ($circles * $points) - 1; $i++)
{
$theta = $theta + $thetac;
$rad = $radius * ($i / $points );
$x = ($rad * cos($theta)) + $x_axis;
$y = ($rad * sin($theta)) + $y_axis;
$theta = $theta + $thetac;
$rad1 = $radius * (($i + 1) / $points);
$x1 = ($rad1 * cos($theta)) + $x_axis;
$y1 = ($rad1 * sin($theta )) + $y_axis;
imageline($im, $x, $y, $x1, $y1, $grid_color);
$theta = $theta - $thetac;
}
// -----------------------------------
// Write the text
// -----------------------------------
$use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
if ($use_font == FALSE)
{
$font_size = 5;
$x = rand(0, $img_width/($length/3));
$y = 0;
}
else
{
$font_size = 16;
$x = rand(0, $img_width/($length/1.5));
$y = $font_size+2;
}
for ($i = 0; $i < strlen($word); $i++)
{
if ($use_font == FALSE)
{
$y = rand(0 , $img_height/2);
imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
$x += ($font_size*2);
}
else
{
$y = rand($img_height/2, $img_height-3);
imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
$x += $font_size;
}
}
// -----------------------------------
// Create the border
// -----------------------------------
imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
// -----------------------------------
// Generate the image
// -----------------------------------
$img_name = $now.'.jpg';
ImageJPEG($im, $img_path.$img_name);
$img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
ImageDestroy($im);
return array('word' => $word, 'time' => $now, 'image' => $img);
}
}
now how to use captcha which is image + word created through above function in
default.htm
<span id="captcha">
<img src="{{captcha.image}}" width="150" height="30" style="border:0;" alt=" " /> </span>
the file created by above function saves in given path bt how to show that image when form appears...................................................................................................................................................
First you need to correct your syntex, to flash session while redirecting you need to use
with('name', 'value')
so you need to use
if (ends_with($url, ['.html', '.htm']))
{
$url = str_replace(['.html', '.htm'], '', $url);
return Redirect::to($url, 301)->with('captcha', $captcha); // <- correct this
// $this->create_captcha() must return string value
// here $captch seems object/image so you should not pass objects in session
}
instead I guess you need to pass some random value
$someRandomValue = 'blabla';
$val = array(
'word_length' => 5,
'word' => '', // <----------------- something here [$someRandomValue]
'img_path' => $captchaImagePath,
'img_url' => $captchaImageUrl,
'font_path' => $captchaFontPath,
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200
);
and then pass it like
with('captcha', $someRandomValue)
and now in other place you can get it by
$captchaValue = session::get('captcha')
so this $captchaValue will be same as $someRandomValue
in short in last you need user input as text/string and compare with $captchaValue (this will be from session) to validate it
if any doubt please comment.
I am trying to extract images from SWF. The following code works, but when extracting png images, the background becomes opaque. Any help would be great; this is what I have:
class SWFextractImages {
private $swf;
private $jpegTables;
private $shipID;
public function doExtractImages($shipID, $b) {
$this->shipID = $shipID;
$this->swf = new SWF($b);
$this->jpegTables = '';
foreach ($this->swf->tags as $tag) {
if($tag['type']==6){
if($this->defineBits($tag)){
continue;
}
}
}
}
private function defineBits($tag) {
$ret = $this->swf->parseTag($tag);
$imageData = $ret['imageData'];
if (strlen($this->jpegTables) > 0) {
$imageData = substr($this->jpegTables, 0, -2) . substr($imageData, 2);
}
if($ret['characterId']==5){
$filename = sprintf('images/'.$this->shipID.'.jpg', $ret['characterId']);
file_put_contents($filename, $imageData);
return true;
}
}
}
Not sure if this will help, but this extracts other data from SWF file too. Here's the source code.
The function defineBits is only for jpg images
For png try this:
private function defineBitsLossless($tag) {
$ret = $this->swf->parseTag($tag);
$bitmapFormat = $ret['bitmapFormat'];
$bitmapWidth = $ret['bitmapWidth'];
$bitmapHeight = $ret['bitmapHeight'];
$pixelData = $ret['pixelData'];
$img = imageCreateTrueColor($bitmapWidth, $bitmapHeight);
if ($bitmapFormat == 3) {
$colorTable = $ret['colorTable'];
// Construct the colormap
$colors = array();
$i = 0;
$len = strlen($colorTable);
while ($i < $len) {
$red = ord($colorTable[$i++]);
$green = ord($colorTable[$i++]);
$blue = ord($colorTable[$i++]);
$colors[] = imageColorAllocate($img, $red, $green, $blue);
}
$bytesPerRow = $this->alignTo4bytes($bitmapWidth * 1); // 1 byte per sample
// Construct the image
for ($row = 0; $row < $bitmapHeight; $row++) {
$off = $bytesPerRow * $row;
for ($col = 0; $col < $bitmapWidth; $col++) {
$idx = ord($pixelData[$off++]);
imageSetPixel($img, $col, $row, $colors[$idx]);
}
}
} else if ($bitmapFormat == 4) {
$bytesPerRow = $this->alignTo4bytes($bitmapWidth * 2); // 2 bytes per sample
// Construct the image
for ($row = 0; $row < $bitmapHeight; $row++) {
$off = $bytesPerRow * $row;
for ($col = 0; $col < $bitmapWidth; $col++) {
$lo = ord($pixelData[$off++]);
$hi = ord($pixelData[$off++]);
$rgb = $lo + $hi * 256;
$red = ($rgb >> 10) & 0x1f; // 5 bits
$green = ($rgb >> 5) & 0x1f; // 5 bits
$blue = ($rgb) & 0x1f; // 5 bits
$color = imageColorAllocate($img, $red, $green, $blue);
imageSetPixel($img, $col, $row, $color);
}
}
} else if ($bitmapFormat == 5) {
$bytesPerRow = $this->alignTo4bytes($bitmapWidth * 4); // 4 bytes per sample
// Construct the image
for ($row = 0; $row < $bitmapHeight; $row++) {
$off = $bytesPerRow * $row;
for ($col = 0; $col < $bitmapWidth; $col++) {
$off++; // Reserved
$red = ord($pixelData[$off++]);
$green = ord($pixelData[$off++]);
$blue = ord($pixelData[$off++]);
$color = imageColorAllocate($img, $red, $green, $blue);
imageSetPixel($img, $col, $row, $color);
}
}
}
imagePNG($img, sprintf('images/img_%d.png', $ret['characterId']));
imageDestroy($img);
}
You can read more, understand more and use more from here
I want to create piechart in my pdf file created using fpdf. already i had created pdf with fpdf . then i want to create pie chart in that using same table data, is there any option to create pie chart using fpdf ?
Please Help.
Thanks in advance
Try this make changes in code as per your requirements:
You can display following view file on your pdf using pdf helper.
you can use dom pdf helper download it from following link.
http://code.google.com/p/dompdf/downloads/detail?name=dompdf_0-6-0_beta3.zip
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('003366', 'CCD6E0', '7F99B2','F7EFC6', 'C6BE8C', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = explode('*',$data);
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label && $data[$i] > 0)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label && $data[$i] > 0)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($data[$i] > 0)
{
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>
Hope this will help you... :)