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);

										

?>

.

Recommended Answers

All 10 Replies

Hi there,
I'm not too experienced with the gd libraries so forgive me if i'm wrong, but I think the problem is that the libraries can not spawn a picture out of nothing (unlike the php functions for files like fopen())
I would recommend creating the three .png images first, even just as blank 1px by 1px images, so that the code has something to open and manipulate.

The error is simple and clear. The image path specified in the imagecreatefrompng() function does not exist. So just check that your files are in the right locations as they probably are not or that you may have specified the wrong location in the function.

Just gave it a bash and it worked fine. There are a couple of other little errors in your code that you'll need to clean up though.
Good Luck :)

Thanks a lot guys!! I wasn't thinking when I created the .png files about the fact that the script will seek for the file name with -button added to the name...ie red-button.png. I've gotten it so there are no more errors, changed the name of the .png file to red-button.png (blue-button.png and green-button.png) and now...I get a blank page with a broken image icon as though the image can't load for some reason. I have ensured the pic files are in the documents directory, but I am still missing something.

Any thoughts? Thank you again!!

When using functions, try making them all lower case. So don't use capitals for the function name. Below is an example of what 2 lines should look like

header('Content-type: image/png');
imagepng($im);

cwarn23, thanks a lot! I changed the function names to all lower case and they turned blue in color in my editor, I guess that means that it's recognized as a php function? Well, I tried running the script again and all but 2 errors are gone, the two remaining are referring to the same thing...here is what they say:

<b>Warning</b>: imagettfbbox() [<a href='function.imagettfbbox'>function.imagettfbbox</a>]: Invalid font filename.

This is how it's displayed in IE 8, Firefox just shows a broken image and no text and Chrome seems to do nothing at all. Thanks again!

It is because the second last parameter in the imagettftext() function needs to be the path to your font file. You can't just use the name of font's as by default the system has only 1 font. So go into your windows folder to find the font files then upload them and specify the path. Also I saw in your code you didn't use quotes on the following line which could cause an error.

$fname = arial;

I was actually trying to supply the path earlier in the code like this: //Tell GD2 where fonts are
putenv('GDFONTPATH=c:\windows\fonts');
$fname = 'Arial';

That's not correct? I thought I was being clever.

yeah, you got it . you must tell the browser what the type of the file is. so ,here you just use this-- header("content-type:image/png.jpeg."). hope it can help you .

Thanks a lot YC!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.