Hi, I am trying to learn php and when I feel i understand a concept I like to try it out by writing a script or 2. I am currently attempting to create an image on the fly, like a button. I keep receiving the message:
Warning: imagecreatefrompng(red-button.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory
Could someone please take a look at this for me and tell me what you think I've done wrong?? I would really appreciate it, I've looked at it until my eyes have about gone on strike. Here is the code:
<form name ="button" action ="button.php" method="post">
<input type ="text" name = "btext">
<br>
<p>
<label>
<input type="radio" name="color" value="red" id="red">
Red</label>
<br>
<label>
<input type="radio" name="color" value="green" id="green">
Green</label>
<br>
<input type="radio" name="color" id="blue" value="blue">
Blue</label>
</p>
<br>
<input type ="submit" name ="submit" value ="CREATE!">
</form>
<?php
$btext=$_REQUEST['btext'];
$color =$_REQUEST['color'];
if((empty($btext) || empty($color)) ||(!($color=='red' ||$color=='blue' ||$color =='green')))
{
echo 'could not create image - incomplete info.';
exit;
}
//create image of right background and check size
$im = ImageCreateFromPNG($color. '-button.png');
if(!$im)
{
echo 'Could not create image';
}
$wimage = ImageSX(im);
$himage = ImageSY($im);
//image needs 18 px margin in from edge
$wnomargin = $wimage -(2*18);
$hnomargin = $himage - (2*18);
//work out whether font size fits, resize until it does. Start with largest that fits reasonably
$fsize = 33;
//Tell GD2 where fonts are
putenv('GDFONTPATH=c:\windows\fonts');
$fname = arial;
do
{
$fsize--;
//What is text size @ font size
$bbox=ImageTTFBBox ($fsize, 0, $fname, $btext);
$rtext =$bbox[2]; //cooridnate right
$ltext =$bbox[0]; //coordinate left
$wtext = $rtext - $ltext; //How wide?
$htext = abs($bbox[7] -$bbox[1]); //How tall?
}
while($fsize > 8 &&($htext>$hnomargin || $wtext>$wnomargin));
if($htext>$hnomargin || $wtext>wnomargin)
{
//no readable font size fits
echo 'Text given will not fit';
}
else
{
//font size found that fits, now put it somewhere
$textx =$wimage/2.0 - $wtext/2.0;
$texty = $himage/2.0 - $htext/2.0;
if($ltext <0)
$textx += abs($ltext);
$alinetext = abs($bbox[7]);
$texty += $alinetext;
$texty -=2;
$white =ImageColorAllocate ($im, 255, 255, 255);
ImageTTFText ($im, $fsize, 0, $textx, $texty, $white, $fname, $btext);
Header('Content-type: image/png');
IMagePNG($im);
}
ImageDestroy($im);
?>
.