Merge two images with PHP and GD

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





















ProntoTec 7 Android 4.2 Tablet PC, Cortex A8 1.2 Ghz Dual Core Processor,512MB / 4GB,Dual Camera,HDMI,G-Sensor (Black)


ProntoTec 7 Android 4.2 Tablet PC, Cortex A8 1.2 Ghz Dual Core Processor,512MB / 4GB,Dual Camera,HDMI,G-Sensor (Black)


$50.98


Product Name

- ProntoTec Q8 7 inch Dual Core Tablet PC

CPU

- Cortex A8 Dual Core 1.2 GHz

Operating System

- Android 4.2.2 OS

Touch Screen

- 7-Inch Capacitive Display
- 800 x 480 Pixels

Internal Storage

- 4GB GB

RAM

- 512MB SDRM DDR3

Sensors

- G-Sensor

Wireless and Cellular

- Wi-Fi (802.11 BGN)
- Support for 3G Dongles and Ethernet with Adapter

Dual Cameras

- Front 0.3M/Rear 2M …


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]


$57.99


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 GPU
Internal Memory: 4 GB Memory Expansion: MicroSD card (up to 32
GB in size)
RAM: 512MB DDR3
Camera: Front facing plus rear camer…

Tags: , ,