Thanks ClarkF1.
The code has evolved to a bit of a monster so pasting it all here would be a nightmare. To simplify things, lets say the main script is called index.php.
When index.php runs the user inputs values through various means (POST and GET). These are set into their session. The user then clicks through to a preview page - lets call it preview.php. Preview.php contains mostly HTML and an HTML image tag which calls the main dynamic image creation script (lets call this image.php). Image.php is called from preview.php in an IMG tag like this:
(not the actual code so don't look for errors ;-))
This is how I am calling the image creation script (in preview.php):
<img src="/image.php?pid=<?php echo $image_id;?>&sid=
<?php echo session_id();?>&s=p&csq=
<?php echo rand();?>" alt="<?php echo $info;?> Preview - <?php echo $image_name;?>"/>
<?php
//Attempt 1: Global variable. Does not work
global $oversize_flag;
if ($oversize_flag) echo 'Image oversize!';
//Attempt 2: Write flag to database
$sql = "SELECT oversize_flag FROM oversize_temp
WHERE sesskey = '".session_id()."'
AND image_id = '".$image_id."'";
$oversize_flag_result = mysql_query($sql);
$oversize_flag_array = mysql_fetch_array($oversize_flag_result,MYSQL_ASSOC);
$oversize = $oversize_flag_array['oversize_flag'];
if ($oversize == '1'){
echo 'Image over maximum allowed size!!';
}
?>
image.php pulls all the values entered earlier (like colour, size, watermark file name, etc) from the unserialized session data which it is pulling out of the database and creates the image (using the GD libraries). This all works well. The problem is that I want to get information back from this process in a realtime fashion. If the image created by image.php is (for example) larger than allowed I would like to be able to display a warning message on preview.php right after calling image.php.
As mentioned earlier I am passing on the session ID as a GET variable as it is not being done by PHP (which is to be expected as it is not being included in preview.php in a standard require or include).
The main problem with trying to get anything returned by image.php is that because it is being called in an HTML IMG tag PHP is not "passing on" any of the super globals or global variables which would be my only real way of retrieving realtime feedback from the image creation process.
image.php code looks like this (summarised - not the real code):
<?php
header("Content-type: image/png");
//Connect to DB
//get session data
//unserialise session array
//set local variables according to values from session data
//create image according to variable
$source_image = imagecreatefrompng($image_path);
$watermark = imagecreatefromjpeg($watermark_path);
//get the sizes of both original images
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$source_image_width = imagesx($source_image);
$source_image_height = imagesy($source_image);
$image_out = imagecreatetruecolor($source_image_width,$source_image_height);
imagecopy($image_out,$watermark,0,0,0,0,$watermark_width,$watermark_height);
//get size of output image
$output_width = imagesx($image_out);
$output_height = imagesy($image_out);
if ($output_width > $maximum_width || $output_height > $maximum_height){
//set the oversize flag to warn the user
//Attempt 1: global variable flag
global $oversize_flag; // DOES not work as no global variables ever
//reach the calling script
$oversize_flag = TRUE;
//Attempt 2: Database flag - Sort of works but flag is one click behind.
//What that means is that as the image is generated no warning is
//given on the calling page (preview.php) until the page is refreshed
//again. Not sure why this happens...
$sql = "INSERT INTO oversize_temp (sesskey,image_id,oversize_flag)
values('".addslashes($_GET['sid'])."','".(int)$_GET['pid']."','1')
";
mysql_query($sql);
}
?>
I guess my question really is "is there a better way of doing this?" or "is there a workaround for what I am trying to do?". Is this a bug with PHP or is it handling the variables the way it should? I always thought a global variable really was GLOBAL? I guess not...