Element
08-24-2009, 02:02 AM
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
http://jordan.rave5.com/tvshack/latest_movie.png
( 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
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( $image, false );
imagesavealpha( $image, true );
$blue = imagecolorallocate( $image, 115, 140, 173 );
$shadow_blue = imagecolorallocate( $image, 16, 76, 104 );
$green = imagecolorallocate( $image, 116, 160, 80 );
$shadow_green = imagecolorallocate( $image, 74, 102, 51 );
$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( 0 => ' ', 1 => ' ', 2 => ' ' );
$array = 0;
$i = 0;
foreach ( $words as $word ) {
if ( $i == 5 ) {
$array++;
$i = 0;
}
$movies['description'] = $description[$array] .= ' ' . $word;
$i++;
}
}
$random_movie = $movies[array_rand( $movies, 1 )];
// Movie Title
imagestring( $image, 2, 93, 42, $random_movie['title'], $blue );
// Movie Date
imagestring( $image, 2, 93, 56, $random_movie['date'], $green );
// Movie Description
imagestring( $image, 2, 93, 70, $random_movie['description'][0], $shadow_blue );
if ( $random_movie['description'][1] != ' ' ) {
imagestring( $image, 2, 93, 84, $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...?
:thumbsup:
Help is much appreciated, and you should know by my snippets, if I ever release it for open-source, credit is much given. :)
http://jordan.rave5.com/tvshack/latest_movie.png
( 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
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( $image, false );
imagesavealpha( $image, true );
$blue = imagecolorallocate( $image, 115, 140, 173 );
$shadow_blue = imagecolorallocate( $image, 16, 76, 104 );
$green = imagecolorallocate( $image, 116, 160, 80 );
$shadow_green = imagecolorallocate( $image, 74, 102, 51 );
$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( 0 => ' ', 1 => ' ', 2 => ' ' );
$array = 0;
$i = 0;
foreach ( $words as $word ) {
if ( $i == 5 ) {
$array++;
$i = 0;
}
$movies['description'] = $description[$array] .= ' ' . $word;
$i++;
}
}
$random_movie = $movies[array_rand( $movies, 1 )];
// Movie Title
imagestring( $image, 2, 93, 42, $random_movie['title'], $blue );
// Movie Date
imagestring( $image, 2, 93, 56, $random_movie['date'], $green );
// Movie Description
imagestring( $image, 2, 93, 70, $random_movie['description'][0], $shadow_blue );
if ( $random_movie['description'][1] != ' ' ) {
imagestring( $image, 2, 93, 84, $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...?
:thumbsup:
Help is much appreciated, and you should know by my snippets, if I ever release it for open-source, credit is much given. :)