Merge two images with PHP and GD
Posted by Hodge on Apr 4, 2009 in Programming, Web Development • 7 comments •
One of the things I really wanted to incorporate into the new site design template, was a nice big, I-really-want-people-to-click-on-this-and-Subscribe-to-my-RSS-Feed image/link/button (big [what the hell is] “Web 2.0″ buttons seem to be the in thing at the moment – I didn’t want to feel left out…), which is located at the top right of the site header, under the search box (in case you missed it). That’s easy enough, so what’s the point of this article? Well, I use FeedBurner to serve my RSS Feeds, and wanted to display the cool little badge they have, which shows the number of subscribers to my Feeds. I decided I wanted to include the FeedBurner stat counter in the new big “Look! I have a Web 2.0 site too” Subscribe to my RSS Feed button, since placing it elsewhere on the site’s layout, such as one of the sidebars, just didn’t feel right. Now, as with any problem, there are numerous ways this can be done, and no one solution is right. For instance, I could have set the “No, really, it is a Web 2.0 Site” Subscribe button as a background image to an (X)HTML <div> element, and then had the FeedBurner Badge ensconced therein, but since I had my PHP IDE open at the time, I opted for the following method (which also happens to give me something to write about).
So, on to the code itself, which is actually rather embarrassingly simple:
<?php
//Set the Image source variables
$backgroundSource = "http://www.64bitjungle.com/wp-content/themes/openbook22-en/images/rss-subscribe.jpg";
$feedBurnerStatsSource = "http://feeds2.feedburner.com/~fc/64BitJungle?bg=151515&fg=ffffff&anim=0";
//Create new images
$outputImage = imagecreatefromjpeg($backgroundSource);
$feedBurnerStats = imagecreatefromgif($feedBurnerStatsSource);
//Grab width and height of the FeedBurner image
$feedBurnerStatsX = imagesx($feedBurnerStats);
$feedBurnerStatsY = imagesy($feedBurnerStats);
//Merge the two images
imagecopymerge($outputImage,$feedBurnerStats,156,50,0,0,$feedBurnerStatsX,$feedBurnerStatsY,100);
//Output header
header('Content-type: image/png');
//send new image to browser
imagepng($outputImage);
imagedestroy($outputImage);
?>
Now for the breakdown of each line. First, I put the source images into a couple of variables – the background image, located at http://www.64bitjungle.com/wp-content/themes/openbook22-en/images/rss-subscribe.jpg:

and the FeedBurner badge, located at http://feeds2.feedburner.com/~fc/64BitJungle?bg=151515&fg=ffffff&anim=0:
$backgroundSource = "http://www.64bitjungle.com/wp-content/themes/openbook22-en/images/rss-subscribe.jpg";
$feedBurnerStatsSource = "http://feeds2.feedburner.com/~fc/64BitJungle?bg=151515&fg=ffffff&anim=0";
The next significant lines, tell GD to create two new image link resources from the sources above:
$outputImage = imagecreatefromjpeg($backgroundSource);
$feedBurnerStats = imagecreatefromgif($feedBurnerStatsSource);
The FeedBurner badge is delivered as a GIF, hence the imagecreatefromgif function, and the background image is in jpeg format, so we call the imagecreatefromjpeg function. Next, we grab the height and width of the FeedBurner image using the imagesx and imagesy functions, and stick the values in a coupe of variables:
$feedBurnerStatsX = imagesx($feedBurnerStats);
$feedBurnerStatsY = imagesy($feedBurnerStats);
and then run the important function, imagecopymerge, to combine the two images:
imagecopymerge($outputImage,$feedBurnerStats,156,50,0,0,$feedBurnerStatsX,$feedBurnerStatsY,100);
Finally, the image is output to the browser in PNG format:
header('Content-type: image/png');
imagepng($outputImage);
imagedestroy($outputImage);
That’s it – painfully simple, eh? Now all that’s required is to include the new image in some (X)HTML Markup – note that the src of the image, points to the PHP file, not to a traditional JPG etc.:
<img src="http://www.64bitjungle.com/wp-content/themes/openbook22-en/subscribe-image.php" alt="Subscribe to RSS Feed" />
Et voila:
The font, by the way, is “SF Fedora”.
Something not quite right? Inaccuracies or invalid code? Didn’t work for you? Don’t like me using Ss instead of Zs? Add a comment below! All comments are welcome. Except spam, because spam is a bit crap.
|
|
ASUS Lamborghini VX6-PU17-BK 12.1-Inch Eee PC Netbook (Black) $685.70 Asus Lamborghini VX6-PU17-BK 12.1″ LED Netbook – Atom D525 1.80 GHz – Black VX6-PU17-BK 41… |
|
|
T101MT-BU17-BK 10.1 Netbook $599.99 Asus Eee PC T101MT-BU17-BK 10.1″ LED Net-tablet PC – Atom N450 1.66 GHz – Black T101MT-BU17-BK 202… |
|
|
CellularFactory: Laptop Computer Security Cable Lock (Silver) for Asus laptop $7.99 Laptop Computer Security Cable Lock (Silver) for Asus laptop . Laptop Computer Security Cable Lock for you to use to lock your laptop computer in an easy way.Features:Lock down your notebooks laptop computer with this unique, individual keys and locks for each computer. Galvanized steel cable for strong protectionEffective protection, anti-thief design keeps the sneakers away The T-bar mechanism b… |
|
|
Samsung USB 2.0 External Optical Drive SE-S084D/TSBS Super WriteMaster, Slim External DVD Writer (Gloss Black) $33.99 Samsung SE-S084D DVD-Writer – Black – Retail – External SE-S084D/TSBS CD/DVD Combo Drives – External… |
|
|
Asus 24xDVD±RW Serial ATA Internal OEM Drive DRW-24B1ST (Black) $20.25 The ASUS 90-D40FJV-UBN10- is capable of reading and writing data on DVDR/RW; DVDRAM formats. It is a power saving drive and allows you to permanently delete the data on rewritable discs. This provides you with flexibility and efficiency in terms of multim… |
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.





Yup,,, it works ….!! gr8
Its very good script, I like it.
Thank you for doing this. it saves my hour..
Feedburner is really very useful for syndicating feeds from other websites.’`’
I have sent an image from my flash application to php and it’s sent as a byte array.(png format). Is there any way for me to transform my png byte array into one of the ‘php gd image resources’ like the one that createimagefrompng() function returns? If so, then I’d be able to following this tutorial. php GD seems to only work with those php gd image resources and not byte arrays, please help
i always use feedburner to syndicate my blog posts to other subscribers.*~.
i always use feedburner to syndicate my blog posts to other subscribers..:.
i always syndicate feeds on my subcribers and of course feedburner is definitely a great help ;’*