Merge two images with PHP and GD

Posted by on Apr 4, 2009 in Programming, Web Development20 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.





















Dragon Touch A1X 10.1 Quad Core Google Android 4.4 KitKat Tablet PC, 1GB RAM, 16GB Nand Flash, Bluetooth, Dual Camera, HDMI, Google Play Pre-installed, 3D Game Supported, 2014 Newest Model [ by TabletExpress ]


Dragon Touch A1X 10.1 Quad Core Google Android 4.4 KitKat Tablet PC, 1GB RAM, 16GB Nand Flash, Bluetooth, Dual Camera, HDMI, Google Play Pre-installed, 3D Game Supported, 2014 Newest Model [ by TabletExpress ]


$109.99


Need a keyboard case ? Please search “B00EZAX2LA” on Amazon Brilliant PerformanceEquipped with a Quad-Core CPU and Octa Core GPU, this tablet ensures everything running smoothly. Enjoy yourself with stream videos, smooth-flowing gaming, web surfing and more!Newest Android 4.4 KitKatThe newly released system enables faster multitasking, optimizes user interface with more convenience and simplity a…

Alldaymall® 7 Inch Android 4.4 KitKat Tablet PC MID with Capacitive Touchscreen (512MB + 8G, Dual Core CPU, 1.5GHz, Wi-Fi, Dual Camera) Black


Alldaymall® 7 Inch Android 4.4 KitKat Tablet PC MID with Capacitive Touchscreen (512MB + 8G, Dual Core CPU, 1.5GHz, Wi-Fi, Dual Camera) Black


$47.99


Alldaymall owns this trademark exclusively. Summary: Operating System: Android 4.4 KitKat Screen Size: 7 inches Max Screen Resolution: 800 x 480 pixels Processor: Up to 1.5GHz Allwinner A23 Dual Core RAM: 512MB Hard Drive Capacity: 8GB NAND Fast Flash Extend Card: Supports up to 32GB TF card Display Technology: 5-point capacitive touchscreen Battery Power: Battery Type: 2200mAh rechargeable ba…

Tags: , ,