26 Jul 2011

Latest blog post title in email signatures

I put a PHP image script together that reads the latest title from any RSS feed and creates a .gif image that contains this title. So the image can be placed anywhere once off and it will automatically update with the latest RSS feed title as new content is published. The image could be embedded in an html email signature for instance and the title of your most recent blog post will automatically be appended to all your outgoing email. I'm sure there are other uses for this code as well - It requires PHP and cURL and can be tweaked to accommodate different requirements, just be sure to go through the code and update your RSS feed URL (line 18), copy the font(s) you would like to use (lines 39,40) to the same directory as the script and perhaps change the text colours ~line 90 and anything else as you need to.

Embed the image with this image tag:

<img src="http://yoursite.com/latest-blog-post-rss-image.php" alt="http://yoursite.com/blog" border="0" />

Include a link to the front page of your blog to make the title clickable:

<a href="http://yoursite.com/blog"><img src="http://yoursite.com/latest-blog-post-rss-image.php" alt="http://yoursite.com/blog" border="0" /></a>

Here is the code for latest-blog-post-rss-image.php


//Parse text block for value near search defined by offset and length / end tag
function parse($block,$search,$offset,$length,$endtag="") {
$start = strpos($block,$search) + $offset;
$end = $start + $length;
$value = substr($block, $start, $end - $start );
if ($endtag != "") {
$value = substr($value,0,strpos($value,$endtag)-(strlen($endtag)));
if ($start == 0) {
$value = "";
return $value;

//RSS feed to get title from
$rss = "http://yoursite.com/rss.xml";

//read the RSS feed using cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $rss);
$result = curl_exec ($ch);

//extract the first (and latest blog title)
$title = parse($result,"<item>",0,300,"<link>");

//trim the title and extract html tags
$title = strip_tags($title);
$title = trim($title);

//set image fonts and positioning
$size = 7.5;
$labelsize = 7.5;
$font = 'arialbd.ttf';
$labelfont = 'arial.ttf';
$x = -1;
$y = 10;
$angle = 0;
$label = "Latest blog post: ";

//Get an array of rss title words to facilitate word wrap
$words = explode(" ",$title);

//Force the image to word wrap around this approximate length (pixels)
$labellength = imagettfbbox($labelsize, 0, $labelfont, $label);
$maxlength = 350 - $labellength[4];
$line1length = $labellength;
$line1 = "";
$line2 = "";

foreach ($words as $word) {
if ($line1length[4] < $maxlength) {
$line1 .= $word . " ";
$nextword = $line1 . $word;
$line1length = imagettfbbox($size, 0,$font, $nextword);
} else {$line2 .= $word . " ";}
$line1 = trim($line1);
$line2 = trim($line2);

//determine image dimensions based on rss title length and including the label text
$line1length = imagettfbbox($size, 0, $font, $line1);
$line2length = imagettfbbox($size, 0, $font, $line2);

$totallength = $labellength[4] + $line1length[4];

//Set the image height for 1 line or 2 lines
if ($line2 == "") {
$height = 12;
} else $height = 24;

//set canvas dimensions (based on blog post length) and height
$gif = imagecreatetruecolor($totallength+1, $height);

//set background colour to white
$white = imagecolorallocate($gif, 255, 255, 255);

//fill image with white background and set as transparent
imagefill($gif, 0,0, $white);
imagecolortransparent($gif, $white);

//Print rss title onto image if it's not blank for some reason...
if ($title != "") {

//set text colors R,G,B
$text_color = imagecolorallocate($gif, 255, 149, 6);
$label_color = imagecolorallocate($gif, 0, 5, 85);

//Print blog post label into image
ImageTTFText($gif, $labelsize, $angle, $x, $y, $label_color, $labelfont, $label);

//Print blog post line 1 title into image
ImageTTFText($gif, $size, $angle, $x+$labellength[4], $y, $text_color, $font, $line1);

//print link underline
ImageLine($gif, $totallength, $y+1,$labellength[4]-1,$y+1, $text_color );

if ($line2 != "") {

//Print blog post line 2 title into image
ImageTTFText($gif, $size, $angle, $x+1, $y+12, $text_color, $font, $line2);

//print link underline
ImageLine($gif, 0, $y+13,$line2length[4],$y+13, $text_color );


//output blog post title image
header("Content-type: image/gif");

1 comment:

Bryan said...

This was incredibly helpful - There are a lot of paid apps out there for RSS in signatures but this should work great!