Results 1 to 6 of 6

Thread: ImageCreateFromPNG

  1. #1
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question ImageCreateFromPNG

    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:
    PHP 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>&& ($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 -
    PHP Code:
    putenv('GDFONTPATH=C:\WINDOWS\Fonts'); 
    Last edited by dcr33; 07-29-2012 at 11:31 AM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    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!!
    Last edited by dcr33; 07-29-2012 at 08:26 AM.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Well I tried these-
    PHP Code:
    $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.
    Last edited by dcr33; 07-29-2012 at 10:01 AM.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Here's what I mean:
    PHP Code:
    $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 
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •