Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1

Thread: Word Wrapping

  1. #1
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Lynnwood, Washington, US
    Thanked 2 Times in 2 Posts

    Word Wrapping

    Alright, I'll try to explain this as easily as I can. So, I have a sig/ad generator that displays some RSS information, right? Well, all is well with simple display

    ( The description would obviously be one line below the date. )

    However now I am making another, which simply shows one random movie, and then displays a description about the movie as well. Problem is, I only have imagestring() on my server, and I would like to correctly word wrap the long descriptions down to two lines, and then substr it with '...' of course.

    I can't seem to find a good way to do this. If some one could point out my technique flaws with the current code, or show me a better way to do it, It would be much appreciated.

    As you can see from the demo, the image has a 'body' so-to-speak, and the text needs to be restricted to it, as well as cutting out short for long descriptions.

    PHP Code:

    E_ALL & ~E_NOTICE );
    header 'Content-type: image/png' );

    //$font = imageloadfont( '../latest_movies.png/tvslms.gdf' );
    $source './tvs_new_movies_background.png';

    $image imagecreatefrompng$source );
    imagealphablending$imagefalse );
    imagesavealpha$imagetrue );

    $blue imagecolorallocate$image115140173 );
    $shadow_blue imagecolorallocate$image1676104 );
    $green imagecolorallocate$image11616080 );
    $shadow_green imagecolorallocate$image7410251 );

    $tvs_feed 'http://tvshack.net/rss/movies.xml';

    $dom = new DOMDocument();
    $dom->load$tvs_feed );
    $items $dom->getElementsByTagName'item' );
    $movies = array();
        foreach ( 
    $items as $item ) {
    $title_tag $item->getElementsByTagName'title' );
    $description_tag $item->getElementsByTagName'description' );
    $date_tag $item->getElementsByTagName'pubDate' );

    $movies[] = array( 
    'title' => $title_tag->item(0)->nodeValue,
    'date' => date'F jS\, Y H:i:s'strtotime$date_tag->item(0)->nodeValue ) )
    $words explode' '$description_tag->item(0)->nodeValue );
    $movies['description'] = array( => ' '=> ' '=> ' ' );
    $array 0;  
    $i 0;
            foreach ( 
    $words as $word ) {
                if ( 
    $i == ) {
    $i 0;
    $movies['description'] = $description[$array] .= ' ' $word;

    $random_movie $movies[array_rand$movies)];
    // Movie Title
    imagestring$image29342$random_movie['title'], $blue );
    // Movie Date
    imagestring$image29356$random_movie['date'], $green );
    // Movie Description
    imagestring$image29370$random_movie['description'][0], $shadow_blue );
        if ( 
    $random_movie['description'][1] != ' ' ) {
    imagestring$image29384$random_movie['description'][1] . ' ...'$shadow_blue );

    imagepng$image );
    imagedestroy$image );

    The problem with that code, is I cannot get it to break down the description correctly, and then display one line, and then the second, if necessary.

    Probably a bad way to go about it, I am sure...?

    Help is much appreciated, and you should know by my snippets, if I ever release it for open-source, credit is much given.
    Last edited by Element; 08-24-2009 at 04:45 AM.


Posting Permissions

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