Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-24-2009, 02:02 AM   PM User | #1
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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:
<?php

    error_reporting
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 == ) {
                
                
$array++;
                
$i 0;
                
            }
        
            
$movies['description'] = $description[$array] .= ' ' $word;
            
            
$i++;
          
        }

    }
    
    
$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 03:45 AM..
Element is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:54 PM.


Advertisement
Log in to turn off these ads.