bulrush Posted May 19, 2010 Share Posted May 19, 2010 Earlier this week and last week I posted a question. I needed to display a single letter with a gray background, so I used the CSS background-color style. This displayed fine on the screen but didn't print because browsers, by default, do not print backgrounds. My newest idea is to generate dynamic images with a single black letter on a gray background, but I'm having problems. The image needs to be returned by a function, and when I return the image via the header() function (inside my custom function) I get an error "Cannot modify header data." So there must be another way to return this image via my function. Here is my function so far in my included file called crutil.php: //==================================================== function crGraybox1($txt) /* Create and display gray box in img tag. Text can only be 1 character. Returns: nothing */ { $width=50; //pixels $height=50; $txt=str_replace('[','',$txt); $txt=str_replace(']','',$txt); if (strlen($txt)==0) { $msg='Text must be one character. Text='.$txt; crError(basename($_SERVER['PHP_SELF']).' line '.__LINE__,$msg,true); } if (strlen($txt)>1) { $msg='Text cannot be more than one character. Text='.$txt; crError(basename($_SERVER['PHP_SELF']).' line '.__LINE__,$msg,true); } $msg='crGraybox1: letter is '.$txt; crDebug($msg); $img=imagecreatetruecolor($width,$height); //Set gray background with black text. $bg_color=imagecolorallocate($img,120,120,120); //Gray $text_color=imagecolorallocate($img,0,0,0); //Black //Fill background. imagefilledrectangle($img,0,0,$width,$height,$bg_color); //Draw the letter. imagettftext($img,18,0,5,$height-5,$textcolor,'timesbd.ttf',$txt); header("Content-type: image/png"); imagepng($img); //Output image. //Clean up. imagedestroy($img); return; //crGraybox1 } In my main program code, the user will indicate a gray box by using an ASCII string that looks like this "[N]". This indicates a capital N in a gray box. I need to replace that with an image tag. The string I am modifying is stored in $ot. It might have a [N] string in it. If it does, it needs to display an image of that letter in a gray box. if (ereg('\[([A-Z0-9])\]',$ot,$regs)) { $ltr=$regs[0]; $msg='ltr='.$ltr; //DEBUG crDebug($msg); $new='<img src="'.crGraybox1($ltr).'" alt="graybox1">'; $ot=preg_replace('/\[[A-Z0-9]\]/',$new,$ot); //gray box } Any ideas? p.s. crDebug($s) simply prints a debugging message in a different color. It' doesn't do anything else. Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2010 Share Posted May 19, 2010 The src="..." attribute in the <img ...> tag is a URL The URL you use would need to be to your .php script that dynamically produces and outputs the correct image (a content type header followed by the image data) with the expected character on it. You would typically use a GET parameter on the end of the URL to specify what you want the dynamically produced image to be. Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/#findComment-1060547 Share on other sites More sharing options...
bulrush Posted May 19, 2010 Author Share Posted May 19, 2010 I don't use GET. Could you help me with the format of the src attribute? Would it look like this? $new='<img src="graybox1.php?txt='.$ltr.'" alt="graybox1">'; Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/#findComment-1060548 Share on other sites More sharing options...
bulrush Posted May 19, 2010 Author Share Posted May 19, 2010 Well I tried your suggestion and this is what I got in the <img> tag. <span class="indent2order" ><img src="graybox1.php?txt=[C]" alt="graybox1"> Option C, add $19 list</span><br/> Here's my graybox1.php file: <?php //require_once('connectvars.php'); /* Create and display gray box in img tag. Text can only be 1 character. Return True on success, false on failure. */ { $width=50; //pixels $height=50; $txt=$_GET['txt']; $txt=str_replace('[','',$txt); $txt=str_replace(']','',$txt); if (strlen($txt)==0) { $msg='Text must be one character.'; crError(basename($_SERVER['PHP_SELF']).' line '.__LINE__,$msg,true); } if (strlen($txt)>1) { $msg='Text can only be one character.'; crError(basename($_SERVER['PHP_SELF']).' line '.__LINE__,$msg,true); } $img=imagecreatetruecolor($width,$height); //Set gray background with black text. $bg_color=imagecolorallocate($img,120,120,120); //Gray $text_color=imagecolorallocate($img,0,0,0); //Black //Fill background. imagefilledrectangle($img,0,0,$width,$height,$bg_color); //Draw the letter. imagettftext($img,18,0,5,$height-5,$textcolor,'timesbd.ttf',$txt); header("Content-type: image/png"); imagepng($img); //Output image. //Clean up. imagedestroy($img); ?> I noticed the <img> tag is being generated, but the actual image is not, so the alt text is displayed instead. Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/#findComment-1060552 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2010 Share Posted May 19, 2010 The code you posted for graybox1.php produces a fatal parse error because of the { near the start of the code. It will also produce an error for the crError() function calls because there is no function definition in that code and you commented out the include that might have defined it. Did you even test that before you posted it here? Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/#findComment-1060571 Share on other sites More sharing options...
bulrush Posted May 19, 2010 Author Share Posted May 19, 2010 Hm. I see what you mean. graybox1.php may not be executed (though I don't get errors when I run my main script which calls it), so how do I get this working? My <img> tag is created, but no image is displayed. Instead I get the alt text being displayed. crError simply displays an error. Here is it's function. //==================================================== function crError($func, $myerr, $bdie=false) /* Display error, set $b to true to die. $func=function name, must be passed in from calling program. $myerr=error message $bdie=true to die after the error. */ { $msg=mysql_error(); echo '<p style="font-weight:bold;color:#FF0000">ERROR in '.$func.':<br/>'.$myerr.'</p>'; if ($bdie) { die(); } return; } Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/#findComment-1060583 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2010 Share Posted May 19, 2010 You can test the dynamically produced image by browsing to: graybox1.php?txt=[C] I'm also assuming that you are developing and debugging php code on a system with error_reporting set to at least E_ALL and display_errors set to ON in your master php.ini so that all the errors php detects will be reported and displayed. You also have a $text_color v.s. $textcolor naming error in the code that prevented it from ever working. Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/#findComment-1060585 Share on other sites More sharing options...
bulrush Posted May 19, 2010 Author Share Posted May 19, 2010 Thank you. 1) I do not have access to the PHP.INI file, we rent space on a commercial PHP server. 2) I will do some testing by going directly to graybox1.php?txt=[N]. So I have that syntax right, at least? Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/#findComment-1060587 Share on other sites More sharing options...
bulrush Posted May 19, 2010 Author Share Posted May 19, 2010 Thank you! Thank you! It works! I keep noticing that when I upload my new php file to the server, then click Refresh on my browser, that the new PHP file is not always executed. I have to actually go back to a different PHP file (like the main menu) then enter my changed php file. That may have been one of my problems. Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/#findComment-1060590 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2010 Share Posted May 19, 2010 You should be developing and debugging php code on a local development system, for a couple of reasons. You waste a huge amount of time constantly uploading code changes just to see what they do and until code is completely tested, it often has security holes that would allow a hacker to exploit your live server. You can set the error_reporting/display_errors settings in a .htaccess file (when php is running as an Apache Module), in a local php.ini (when php is running as a CGI application), or even in your script (fatal parse errors won't be displayed because your code is never executed when there is a parse error and the two settings won't get set.) Quote Link to comment https://forums.phpfreaks.com/topic/202255-function-to-return-a-dynamic-image/#findComment-1060591 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.