Text Centering

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

  1. #1
    I am using a script to write text into an image. Users can select their font size, color, image size, etc. I am having trouble centering the text in the image. Can anyone help me out with this?
     
    Pudge1, Aug 17, 2009 IP
  2. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    And also when using PHP image how do you make it so you can right click Save As...?
     
    Pudge1, Aug 17, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Are you using GD or ImageMagick (what is your code thus far?)

    also the "save as" thats dependent on the browser, if you were to use mod_rewrite in an .htaccess you could make the url to the php look like a jpeg that might make it easier, but the browser should do such automatically noticing a image header.
     
    kblessinggr, Aug 17, 2009 IP
  4. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    <?php
    header('Content-type: image/png');

    // Start The Image
    $im = @imagecreate($_POST['width'] , $_POST['height']);
    $center2 = $_POST['width'] - $_POST['size'];
    $center2 = $center2 / 2;
    $num = strlen($_POST['word'] * $_POST['size']);
    $center1 = $_POST['height']; - $num;
    $center1 = $center1 / 2;

    // Set Text Color
    function hexrgb($hexstr) {
    $int = hexdec($hexstr);
    return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int);
    }
    $colorarr = hexrgb($_POST['bgcolor']);
    $background_color = imagecolorallocate($im, $colorarr['red'], $colorarr['green'] , $colorarr['blue']);
    $colorarr2 = hexrgb($_POST['color']);
    $text_color = imagecolorallocate($im, $colorarr2['red'], $colorarr2['green'] , $colorarr2['blue']);

    // Size
    $_POST['size'] = $size;

    if($size == '0' || $size > 99 || strlen($size) == 0)
    {
    $size = '12';
    }
    else
    {
    }

    // Words/Font
    if(strlen($_POST['word']) < 1 && strlen($_POST['word']) > 0)
    {
    $Word = $_POST['word'];
    }
    elseif(strlen($_POST['word']) > 0)
    {
    $Word = $_POST['word'];
    }
    else
    {
    $Word = 'Preview';
    }

    if(strlen($_POST['sel']) < 1 && strlen($_POST['font']) > 0)
    {
    $Font = $_POST['font'];
    }
    elseif(strlen($_POST['font']) > 0)
    {
    $Font = $_POST['font'];
    }
    else
    {
    $Font = 'Pretendo';
    }

    if(file_exists('./fonts' . $_POST['sel'] . '.ttf'))
    {
    $Font = './fonts' . $_POST['sel'] . '.ttf';
    }
    elseif(file_exists('/fonts' . $_POST['sel'] . '.ttf'))
    {
    $Font = '/fonts' . $_POST['sel'] . '.ttf';
    }
    elseif(file_exists('./' . $_POST['sel'] . '.ttf'))
    {
    $Font = './' . $_POST['sel'] . '.ttf';
    }
    else
    {
    $Font = '/home/nintendo/public_html/pbfun/fonts/' . $_POST['sel'] . '.ttf';
    }


    // Write Text
    imagefttext($im, $size, 0, $center2, $center1, $text_color, $Font, $_POST['word']);

    // Clearer Text
    imagepng($im);
    imagedestroy($im);
    ?>

    As you can see I already tried to center it but it is unsucessful.
     
    Pudge1, Aug 17, 2009 IP
  5. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    Is there any way to center text in the exact center of the image?
     
    Pudge1, Aug 17, 2009 IP