Jump to content

[SOLVED] Passing variables to an image creating php file


Kurrel

Recommended Posts

Hi freaks,

 

I have been working with the gd functions in PHP to render an image.  I was able to solve the (apparently) usual header problems and such like via searching through this page and help documentation, such that I am now able to produce an image by doing the following :

print("<img src='".BASE."/basefunctions/sandbox/mydrawer.php' alt='drawertest'>");

 

which references a .php file with the following :

 

<?php
// get details {
// Set 1 {
	$height			= 400;
	$width			= 750;

	$rows				= 5;

	$barwidth		= 15;
	$bargap			= 10;
	$bar3d			= 1;
	$barshadow	= 0;

	$topval			= 600;
	$bottomval	= 100;
	$text = "My Drawer 1";
// }
// }
// Preparations {
$maxheight	= 0;
foreach ($values as $divkey=>$divval) {
	foreach ($divval as $barkey=>$barval) {
		if($barval > $maxheight) {
			$maxheight = $barval;
		}
	}
}
// }

// create image {
$im = imagecreate($width+1, $height+1);
// }
// set style (colours, fonts, etc) to be used {
// Font {
	$font = "verdana.ttf";
	if($width < 200) {
		$linefontsize		= 6;
		$headerfontsize	= 10;
	} else if($width < 450) {
		$linefontsize		= 8;
		$headerfontsize	= 14;
	} else {
		$linefontsize		= 10;
		$headerfontsize	= 18;
	}
// }

// Colours {
	$white		= imagecolorallocate($im, 255, 255, 255);
	//$bg = imagecolorallocate($im, hexdec('0x' . $colour{0} . $colour{1}), hexdec('0x' . $colour{2} . $colour{3}), hexdec('0x' . $colour{4} . $colour{5}));
	$bg					= imagecolorallocate($im, 200, 200, 200);
	$black			= imagecolorallocate($im, 0x00, 0x00, 0x00);
	$red				= imagecolorallocate($im, 255, 0, 0);
	$yellow			= imagecolorallocate($im, 255, 255, 0);
	$green			= imagecolorallocate($im, 40, 150, 40);
	$blue				= imagecolorallocate($im, 90, 90, 255);
	$colourlist	= array("1"=>$green, "2"=>$yellow, "3"=>$red, "4"=>$blue, "5"=>$black);
	$barcolor		= imagecolorallocate($im, 0xFF, 0x00, 0x00); // Fore colour
// }
// }

// draw backdrop {
//imagerectangle($im, 0,0,$width+1,$height+1,$black);
imageline($im, 5, 0, $width-5, 0, $black); // Top
imageline($im, 0, 5, 0, $height-5, $black); // Right
imageline($im, $width, 5, $width, $height-5, $black); // Left
imageline($im, 5, $height, $width-5, $height, $black); // Bottom

imagearc($im, 4, 4, 10, 10, 181, 269, $black); // Top left
imagearc($im, $width-4, 4, 10, 10, 271, 359, $black); // Top right
imagearc($im, 4, $height-4, 10, 10, 91, 179, $black); // Bottom left
imagearc($im, $width-4, $height-4, 10, 10, 1, 89, $black); // Top left
// imagearc(resource, x, y, height, width, degree start, degree end, colour)

imagefill($im, 20, 20, $bg);
// }
// Title {
// Add show beneath text
imagettftext($im, $headerfontsize, 0, 11, 21, $black, $font, $text);

// Add text
imagettftext($im, $headerfontsize, 0, 10, 20, $blue, $font, $text);
// }

// send image header and png image {
header("content-type: image/png");
imagepng($im);
imagedestroy($im);
// }
?>

 

This all works, the problem comes in now that I want to start expanding this to a useable object, possibly for drawing graphs or presenting data in a single downloadable image.  I can pass data via

print("<img src='".BASE."/basefunctions/sandbox/mydrawer.php&x=500' alt='drawertest'>");

and then using _GET... however, I hope there is a better way than this, as the URL becomes too long too quickly.  I have with just a little expansion on the function already maxed out the maximum length of the URL.

 

One thing I have also used is session variables, but that seems entirely messy... is it the way to go, or is there a better alternative?

 

I don't know whether it is possible to post data to this .php file, but that would seem the best way to pass the number of variables I am planning on working with (x dimension, y dimension, several dozen coordinates, colour lists, etc).

 

Hoping there's a smooth operator way to do this.

Link to comment
Share on other sites

Sessions, cookies, mysql, flat file.. their all the same.. and they wont become messy by them selfs..

as for posting to php.. yep you can do that (lol but kinda messy) without knowing exactly what your doing its hard to say what route i would take but but to sum up.. passing a small amount of into use _GET, if private, temporary or a little larger use _SESSION (or _COOKIE's if not private), if much larger and you dont have SQL and its just a dump of data then flatfile, other than that use SQL

Link to comment
Share on other sites

So using Session extensively is not a bad thing?

 

I've been using it just to store user and 'logged on' details, thus far.  I was taught a dislike for persistent variables.

 

After setting the session variables, how would I destroy those variables without destroing that whole session and thus the user and duration variables previously stored (Apologies, my working knowledge of sessions is limited)?

 

The number of variables could vary dramatically, from 3 or 4 to... width, height, 4 sets of 12 variables, 4 colours... maybe 100 pieces?

 

If I can refine the functioning of these GD tools the range of use in my systems are extensive!  AJAX graphs for our stocks, updating score boards for sales on the office flat overhead screens, etc.

Link to comment
Share on other sites

using Session extensively

not really.. i wouldn't dump a 100mb image resource to one LOL, i kinda look at them like cookies but more secure but remeber the sessions timeout..by the server, cookie timeout by the client

 

i didn't bring up AJAX as your pass the variables just like a _GET..

 

i would probably choose MySQL and use _GET to pass the ID or something refering to a Unique Job

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.