...

View Full Version : parse error



ippo
07-29-2012, 07:32 AM
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 two. I am currently attempting to create an image on the fly, like a button. I kept receiving the message:
Warning: imagecreatefrompng(red-button.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory.
But the error was gone when I commented a few lines to check then removed it ,pls check in your editor most probably -- line no. 15
putenv('GDFONTPATH=C:\WINDOWS\Fonts') which I later removed the comment from and then line no. 43 and added the line 44:header("content-type:image/png.jpeg.");

But now I get the follwing error :
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in I:\xampp\htdocs\web\button\make_button.php on line 21

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 complete code:


<form name ="button" action ="make_button.php" method="post">
<input type ="text" name = "button_text">

<p>
<label>
<input type="radio" name="color" value="red" id="red">
Red</label>

<label>
<input type="radio" name="color" value="green" id="green">
Green</label>

<input type="radio" name="color" id="blue" value="blue">
Blue</label>
</p>

<input type ="submit" name ="submit" value ="CREATE!">
</form>

<?php
$button_text=$_POST['button_text'];
$color=$_POST['color'];

if(empty($button_text)||empty($color))
{
echo 'Could not create image - form not filled out correctly';
exit;
}

$im=imagecreatefrompng('\button'.$color.'-button.png');

$width_image=imagesx($im);
$height_image=imagesx($im);

$width_image_wo_margins=$width_image-(2*18);
$height_image_wo_margins=$height_image-(2*18);

$font_size=33;

putenv('GDFONTPATH=C:\WINDOWS\Fonts');
$fontname='arial';
do
{
$font_size--;

$bbox=imagettfbbox($font_size,0,$fontname,$button_text);
$right_text=$bbox[2];
$left_text=$bbox[0];
$width_text=$right_text - $left_text;
$height_text=abs($bbox[7]-$bbox[1]);
}
while($font_size>8 && ($height_text>$height_image_wo_margins||$widht_text>$width_image_wo_margins));
if($height_text>$height_image_wo_margins||$widht_text>$width_image_wo_margins)
{
echo 'Text given will not fit on button.<br>';
}
else
{
$text_x=$width_image/2.0-$width_text/2.0;
$text_y=$height_image/2.0-$height_text/2.0;

if($left_text<0)
$text_x+=abs($left_text);
$above_line_text=abs($bbox[7]);
$text_y +=$above_line_text;
$text_y -=2;
$white=imagecolorallocate($im,255,255,255);
imagettftext($im,$font_size,0,$text_x,$text_y,$white,$fontname,$button_text);
//header('Content-type:image/png');
header("content-type:image/png.jpeg.");

imagepng($im);
}
imagedestroy($im);
?>

In fact it is this line -->

putenv('GDFONTPATH=C:\WINDOWS\Fonts'); that is giving the parrse error. Is the syntax wrong?

DrDOS
07-29-2012, 03:34 PM
$fpath="C:\WINDOWS\Fonts";
putenv('GDFONTPATH=$fpath');
It seems to like that syntax, so try that.

ippo
07-29-2012, 05:41 PM
Hi DrDOS,
I tried that already still I did as per your suggestion again and I get a parse on this line :
Parse error: syntax error, unexpected T_STRING on line 17.

Line 17 reads :
$fpath="C:\WINDOWS\Fonts";

AndrewGSW
07-29-2012, 06:32 PM
Try


$fpath="C:\\WINDOWS\\Fonts";

ippo
07-29-2012, 06:48 PM
Hi Andrew,
Still the same error:
Parse error: syntax error, unexpected T_STRING in line 17( $fpath="C:\\WINDOWS\\Fonts";
)

AndrewGSW
07-29-2012, 07:21 PM
The error is probably earlier in the code. This


<input type="radio" name="color" id="blue" value="blue">
Blue</label>

has no opening label tag.

ippo
07-29-2012, 07:30 PM
Hi Andrew,
Sorry your are wrong again!! I forgot to mention that the first part in the above code is written in a different html page named "design_button.html"(Here I mentioned the 2 codes together for the sake of convenience.).
Actually I have made 2 files inside my button folder viz.-- design_button.html & make_button.php.Also I have created 3 blank .png images in the same folder.

DrDOS
07-29-2012, 07:58 PM
Try escaping the semicolon, that's what the backslash does. If you run into trouble like that, try escaping different parts of the string. We all have to do that from time to time. The backslash escapes itself.

AndrewGSW
07-29-2012, 07:59 PM
.. I still think you need to look earlier in the code for an unescaped character or a missing $ sign ;)

ippo
07-30-2012, 04:05 AM
Hi AndrewGSW,
Do you mean this-
$fpath="C:\\WINDOWS\\Fonts"\;

But the semicolon is outside the double-quotes!!so is there any need to escape it?
So i think that is wrong!! :rolleyes:
Still,again i tried your suggestion & it was in vain!
The same error I am getting as before in the same line number-
Parse error: syntax error, unexpected T_STRING in line 17.
line 17 reads -
$fpath="C:\\WINDOWS\\Fonts"\;

ippo
07-30-2012, 05:43 AM
I have tried all sorts of ways but in vain!!Again I post here my complete code for everyone's reference-


<?php
$button_text=$_POST['button_text'];
$color=$_POST['color'];
if(empty($button_text)||empty($color))
{
echo 'Could not create image - form not filled out correctly';
exit;
}
// $imgname="file:\\I:\xampp\htdocs\\test\\button\\";

$imgname="I:\\xampp\\htdocs\\web\\button\\".$color."-button.png";
$im=imagecreatefrompng($imgname);
//$im=imagecreatefrompng("$imgname".$color."-button.png");
$width_image=imagesx($im);
$height_image=imagesx($im);
$width_image_wo_margins=$width_image-(2*18);
$height_image_wo_margins=$height_image-(2*18);
$font_size=33;
$fpath="C:\WINDOWS\Fonts";
putenv("GDFONTPATH=$fpath");
$font_name="arial";
do
{
$font_size--;
$bbox=imagettfbbox($font_size,0,$font_name,$button_text);
$right_text=$bbox[2];
$left_text=$bbox[0];
$width_text=$right_text - $left_text;
$height_text=abs($bbox[7]-$bbox[1]);
}while($font_size>8 && ($height_text>$height_image_wo_margins||$width_text>$width_image_wo_margins));

if($height_text>$height_image_wo_margins||$width_text>$width_image_wo_margins)
{
echo "Text given will not fit on button.<br>";
}
else
{
$text_x=$width_image/2.0 - $width_text/2.0;
$text_y=$height_image/2.0 - $height_text/2.0;

if($left_text<0)
$text_x+=abs($left_text);
$above_line_text=abs($bbox[7]);
$text_y +=$above_line_text;
$text_y -=2;
$white=imagecolorallocate($im,255,255,255);
imagettftext($im,$font_size,0,$text_x,$text_y,$white,$fontname,$button_text);
header("content-type:image/png");
imagepng($im);
}
imagedestroy($im);
?>
And now I am getting this errors
Warning: imagecreatefrompng() [function.imagecreatefrompng (http://localhost/test/button/function.imagecreatefrompng)]: 'I:\xampp\htdocs\test\button\blue-button.png' is not a valid PNG file in I:\xampp\htdocs\web\button\make_button.php on line 12
Here are other errors that are coming up after it:
Warning: imagesx() expects parameter 1 to be resource, boolean given in I:\xampp\htdocs\web\button\make_button.php on line 14

Warning: imagesx() expects parameter 1 to be resource, boolean given in I:\xampp\htdocs\web\button\make_button.php on line 15

Warning: imagettfbbox() [function.imagettfbbox (http://localhost/test/button/function.imagettfbbox)]: Invalid font filename in I:\xampp\htdocs\web\button\make_button.php on line 25

Please can anyone tell me where the error is?

p.s:
Line 12 is this :$im=imagecreatefrompng($imgname);
Line 14 is this :$width_image=imagesx($im);
Line 15 is this :$height_image=imagesx($im);
Line 25 is this:$bbox=imagettfbbox($font_size,0,$font_name,$button_text);

AndrewGSW
07-30-2012, 12:08 PM
Those warnings are all the same: 'blue-button.png' is not a valid .png file.

Try using a different .png image to see if it is your image that is causing the problem.

If so, try opening 'blue-button.png' in an image editor and save it explicitly as a .png file.

ippo
07-31-2012, 03:00 AM
Andrew,
Can you please tell me how do I create the .png images?
If I use paint say,then do i keep any button in the image file or the program will draw the button itself and I just need to create a blank .png file?

p.s.:
1)I did create 3 buttons called "red-button.png","blue-button.png" and "green-button.png" ,the red-button being 1x1 pixels height and width. Is the error due to its too small a size?
2)Should I delete all three buttons?
3)I thought that the buttons will be drawn by the program itself!!

DrDOS
07-31-2012, 03:25 AM
Is this site on a web server? If so you probably have some kind of image software available that will make any kind of blank file format you want, GD and Imagemagick are the two most common. If it's on you own server just install one of those and use it to make any image you want anytime.

ippo
07-31-2012, 04:50 AM
Why is it not possible to use Windows Paint Program to draw the buttons?
Obviously I am using my own XAMPP server.
Also I have installed Imagemagick 6.7.8(latest version) from their site and it has installed in the D:\Programs directory.Now I dont know which file i need to run & also where to run it from(I mean should I use cmd in windows run?)
Please guide me as to how to run Imagemagick,or at least tell me which file I should Double click?

p.s. : It has lots of folder and sub-folders like "coders","config","magick","magick++","www","utilities","script","m4","Wand" etc etc. There are lots of text-files as well!! I am confused even more !!
OMG !! Just to create a simple button in PHP, do I have to take so much trouble!! :(
BTW,I use Windows XP Pro and not linux!!Is it possible to run it in Winodws?

ippo
07-31-2012, 05:36 AM
Well I have created the button images with Paint,it is a very nice program.But now I am getting error :
Warning: imagettfbbox() [function.imagettfbbox (http://localhost/test/button/function.imagettfbbox)]: Invalid font filename in ....\button\make_button.php on line 24

Warning: imagettftext() [function.imagettftext (http://localhost/test/button/function.imagettftext)]: Invalid font filename in ....\button\make_button.php on line 46

Is this ok-

$font_name='arial';
? Is the error becuase wrong font name?
Line 24 reads
$bbox=imagettfbbox($font_size,0,$font_name,$button_text);
Line 46 reads
imagettftext($im,$font_size,0,$text_x,$text_y,$white,$font_name,$button_text);
Now a blank browser window with a black background showing ".../button/make_button.php" cannot be displayed because it contains errors.".
Any explanations to that?
Please Help
Thanks in advance

DrDOS
07-31-2012, 02:23 PM
It may require the whole path to the file.

ippo
07-31-2012, 06:52 PM
DrDOS,
I have also supplied the full path but still not working!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum