Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Text in generated image won't wordwrap

    For some reason, this is not wordwrapping after 17 characters like I want it to. Any ideas? (Also, I want the height of the image to be the height of the text, however many lines it ends up being)

    Here's my code:
    PHP Code:
    <?php
    // Path to our font file
    $font 'AvenirLTStd-Heavy.otf';
    $fontsize 27;

    //Text to be written
    $helloworld = isset($_GET['text']) ? $_GET['text'] : "hello World";

    // word wrap
    $text2 wordwrap($helloworld,17,true);

    // create a bounding box for the text
    $dims imagettfbbox($fontsize0$font$text2);

    // make some easy to handle dimension vars from the results of imagettfbbox
    // since positions aren't measures in 1 to whatever, we need to
    // do some math to find out the actual width and height
    $width $dims[4] - $dims[6]; // upper-right x minus upper-left x 
    $height $dims[3] - $dims[5]; // lower-right y minus upper-right y

    // Create image
    $image imagecreatetruecolor(345,$height);

    imagesavealpha($imagetrue);

    $trans_colour imagecolorallocatealpha($image204213218127);
    imagefill($image00$trans_colour);

    // pick color for the text
    $fontcolor imagecolorallocate($image505050);

    // fill in the background with the background color
    imagefilledrectangle($image00$width$height$trans_colour);

    // x,y coords for imagettftext defines the baseline of the text: the lower-left corner
    // so the x coord can stay as 0 but you have to add the font size to the y to simulate
    // top left boundary so we can write the text within the boundary of the image
    $x 0
    $y $fontsize;

    imagettftext($image$fontsize0$x$y$fontcolor$font$text2);

    // tell the browser that the content is an image
    header('Content-type: image/png');
    header("Expires: Mon, 01 Jul 2003 00:00:00 GMT"); // Past date
    header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT"); // Consitnuously modified
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Pragma: no-cache"); // NO CACHE
    // output image to the browser
    imagepng($image);

    // delete the image resource 
    imagedestroy($image);
    ?>
    See link: http://johnmilesart.com/test3.php?te...of%20breaking?

    I'm new to PHP, so this script is basically an amalgamation of some stuff I've found around the web. I'm sure the solution is simple.
    Last edited by Inigoesdr; 09-13-2010 at 03:38 AM.

  • #2
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    PHP Code:
    // Create image
    $image imagecreatetruecolor(345,$height); 
    You've stated here that the image's width is no bigger than 345px, increase this by some means and it will be fixed.

  • Users who have thanked Killermud for this post:

    johnmiles27 (09-12-2010)

  • #3
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    But I want the width to be no bigger than 345px; I need it to be a fixed width.

    I actually just fixed it though after fiddling around with it for a few minutes (guessed I jumped the gun posting for help here). Turns out I didn't need that ",true" after I set the wordwrap to 17 characters (at one point I was also imputting "$helloworld" where I needed "$text2" but I edited that in the original post).

    Thanks for the timely reply though


  •  

    Tags for this Thread

    Posting Permissions

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