Merge two images with PHP and GD

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





















Alldaymall® 9 Inch Android 4.4 KitKat Tablet PC MID (A23 Processor, Dual Core 1.5GHz, WiFi, 8GB, 512DDR3, Dual Camera, Supports Skype Video Chatting, YouTube, Google Play)


Alldaymall® 9 Inch Android 4.4 KitKat Tablet PC MID (A23 Processor, Dual Core 1.5GHz, WiFi, 8GB, 512DDR3, Dual Camera, Supports Skype Video Chatting, YouTube, Google Play)


$59.99


Alldaymall owns this trademark exclusively. Summary: Operating System: Android 4.4 KitKat Screen Size: 9 inches Max Screen Resolution: 800 x 480 pixels Processor: Allwinner A23 Dual Core processor 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: 4000mAh rechargeable batte…

Dragon Touch® 7'' Black Dual Core Y88 Google Android 4.1 Tablet PC, Dual Camera, HD 1024x600, 4GB, Google Play Pre-load, HDMI, 3D Game Supported (enhanced version of A13) [By TabletExpress]


Dragon Touch® 7” Black Dual Core Y88 Google Android 4.1 Tablet PC, Dual Camera, HD 1024×600, 4GB, Google Play Pre-load, HDMI, 3D Game Supported (enhanced version of A13) [By TabletExpress]


$47.00


Trademark Dragon Touch products are marketed exclusively by TabletExpress. Specification: Operating System: Google Android 4.1 (Jelly Bean)Display: 7 Inch Multi-touch screen (1024×600)CPU: iMAP X15 Dual Core, up to 1.2 GHz processor;GPU: Quad core ARM Mali 400 MP GPUInternal Memory: 4 GBMemory Expansion: MicroSD card (up to 32 GB in size)RAM: 512MB DDR3Camera: Front facing plus rear camera enhanc…

Tags: , ,