Merge two images with PHP and GD

Posted by Hodge on Apr 4, 2009 in Programming, Web Development6 commentsPrint This Post

PHP & GDOne 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.


eBay Logo  

Lot 6 New HP G60 15.6 Laptop Notebook Win 7 HDMI Webcam


Lot 6 New HP G60 15.6 Laptop Notebook Win 7 HDMI Webcam


$3,149.95


Lenovo ThinkPad X201s 5397FFU 5397-FFU Notebook/Laptop


Lenovo ThinkPad X201s 5397FFU 5397-FFU Notebook/Laptop


$2,680.09


Lenovo ThinkPad X201s 5413FFU 5413-FFU Notebook/Laptop


Lenovo ThinkPad X201s 5413FFU 5413-FFU Notebook/Laptop


$2,670.45


New Lenovo ThinkPad W510 43192RU Laptop Notebook


New Lenovo ThinkPad W510 43192RU Laptop Notebook


$2,526.75


Lenovo ThinkPad X200s 7469-5GU 74695GU Notebook/Laptop


Lenovo ThinkPad X200s 7469-5GU 74695GU Notebook/Laptop


$2,415.84


No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: , ,

6 comments

» Comments RSS Feed
  1. Yup,,, it works ….!! gr8

  2. Its very good script, I like it.
    Thank you for doing this. it saves my hour..

  3. Feedburner is really very useful for syndicating feeds from other websites.’`’

  4. 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

  5. i always use feedburner to syndicate my blog posts to other subscribers.*~.

  6. i always use feedburner to syndicate my blog posts to other subscribers..:.

Leave a comment