Log in

View Full Version : ImageCreateFromPNG



dcr33
07-29-2012, 06:34 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 and line no. 43 and added the line 44:header("content-type:image/png.jpeg.");

But then I started getting the following error :
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in c:\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 full 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);
?>

I am getting the error from this line -

putenv('GDFONTPATH=C:\WINDOWS\Fonts');

djr33
07-29-2012, 06:42 AM
To start, use file_exists() to see if the filepath you're trying to use exists. You should also echo the path itself so you know what's going on. Once you've confirmed that this is in fact an error, then move on to debugging this script. (It's a lot to go through at the moment, but if you can figure out a few details I can try to take another look later when I have more time.)

Secondly, using PHP's image functions is a terrible way to learn PHP-- they're infuriating even for experienced designers. They're very useful and worth learning at some point, but if this is just an in general exercise for learning PHP, I'd recommend starting something less frustrating and difficult (and perhaps buggy, or at least it often seems that way) :)
If you're learning these for the purpose of learning how to manipulate images in PHP, that's fine, but be prepared for the difficult task-- it's not impossible, but it's always difficult.

dcr33
07-29-2012, 08:00 AM
Hi djr33,
I didn't get you at all.can you please tell me in little detail?As I already said I am learning and it is my choice that I want to learn some graphics programming too!! ;)

djr33
07-29-2012, 08:04 AM
To start, use file_exists() to see if the filepath you're trying to use exists. You should also echo the path itself so you know what's going on.Just do that, and report back what happens. (While testing, comment out the rest of the script, or create a new test page.)

dcr33
07-29-2012, 09:32 AM
Well I tried these-

$pth='C:/WINDOWS/Fonts';
echo $pth;
file_exists($pth);
// putenv('GDFONTPATH='.$pth);

should I comment the last line or you mean to echo it too?
Also should the path be separated with backslassh(\) or slash(/) in the putenv function??
Plus the path to this program file is - c:\xampp\htdocs\web\button\make_button.php
Should I have to put the above full path as it is into putenv function?
p.s.:Also I tried the code above as suggested by you but nothing new except the path "C:\WINDOWS\Fonts" is showing in my latest Mozilla Firefox Browser.

djr33
07-30-2012, 06:12 AM
Here's what I mean:


$color=$_POST['color'];
echo $color."\n";

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

echo '\button'.$color.'-button.png'."\n";

echo file_exists('\button'.$color.'-button.png')?'FILE EXISTS':'File does NOT exist';
//$im=imagecreatefrompng('\button'.$color.'-button.png');
//nothing below this at all!
//either comment it out with /*....*/
//or just delete it