imagefttext() Problem

Discussion in 'PHP' started by Pudge1, Aug 7, 2009.

  1. #1
    
    <?php
    header('Content-type: image/png');
    
    // Start The Image
    $im = imagecreatetruecolor(400, 200);
    
    // Palette
    $white = imagecolorallocate($im, 255, 255, 255);
    $gray = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    
    // Set Color
    $color = $white;
    
    // Background
    imagefilledrectangle($im, 0, 0, 399, 199, $color);
    
    // Words/Font
    $Font = './fonts/' . $_POST['sel'] . '.ttf';
    
    // Write Text
    imagefttext($im, 10, 0, 50, 50, $black, $Font,$_POST['word']);
    
    // Clearer Text
    imagepng($im);
    imagedestroy($im);
    ?>
    
    Code (markup):
    For some reason whenever I try to go through and make it, it makes the image but it says this: The image "create_img.php" cannot be displayed, because it contains errors.

    I looked at the PHP manual and there is nothing wrong with mine so I took their example and ran it and got the same error.

    Is this a PHP version problem or what. How can I fix this answers appreciated.
     
    Pudge1, Aug 7, 2009 IP
  2. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    comment out the header() and see hwat the error is then lol !
     
    oop, Aug 7, 2009 IP
  3. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #3
    Try this out

    
    <?php
    ob_start();
    header('Content-type: image/png');
    
    // Start The Image
    $im = imagecreatetruecolor(400, 200);
    
    // Palette
    $white = imagecolorallocate($im, 255, 255, 255);
    $gray = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    
    // Set Color
    $color = $white;
    
    // Background
    imagefilledrectangle($im, 0, 0, 399, 199, $color);
    
    // Words/Font
    $Font = './fonts/' . $_POST['sel'] . '.ttf';
    
    // Write Text
    imagefttext($im, 10, 0, 50, 50, $black, $Font,$_POST['word']);
    
    // Clearer Text
    imagepng($im);
    imagedestroy($im);
    ob_end_flush();
    ?>
    
    Code (markup):
     
    Bohra, Aug 7, 2009 IP
  4. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    Used both of your ideas. I think the original error was that it couldn't get to the font file. Works fine now though thanks!
     
    Pudge1, Aug 7, 2009 IP
  5. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #5
    post whats the error here
     
    Bohra, Aug 7, 2009 IP
  6. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    <?php
    header('Content-type: image/png');
    
    // Start The Image
    $im = imagecreatetruecolor(400, 200);
    
    // Palette
    $white = imagecolorallocate($im, 255, 255, 255);
    $gray = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    
    // Set Color
    $color = $white;
    
    // Background
    imagefilledrectangle($im, 0, 0, 399, 199, $color);
    
    /**
    * This will check see if the user has selected a font and the fact that it exists.. if all tests are past it will use
    * the selected font by the user, or else it will use a defualt font that you know exists.
    *
    * Simples .
    */
    $Font = (isset($_POST['sel'])&&file_exists('./fonts/'.$_POST['sel'].'.ttf')?'./fonts/'.$_POST['sel'].'.ttf':'defualt.ttf'); 
    
    // Write Text
    imagefttext($im, 10, 0, 50, 50, $black, $Font,$_POST['word']);
    
    // Clearer Text
    imagepng($im);
    imagedestroy($im);
    ?>
    
    PHP:
     
    oop, Aug 7, 2009 IP
  7. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #7
    It is fixed. Check it out here nintendousland.com/pbfun
     
    Pudge1, Aug 12, 2009 IP